Getting started with tkinter -- list box, Scrollbar, Scale

Keywords: Lambda Pycharm

tkinter getting started (4) -- list box, scroll bar, Scale

1. List box, for multiple options

import tkinter as tk
root = tk.Tk()#Generate top window
root.title("Component use!")#Set GUI title

#List box, for multiple options
listbox1 = tk.Listbox(root,selectmode = 'multiple')#Select mode sets the selection mode. There are four options: single, browse, multiple, extended
items = [('apple',0),('bag',1),('cat',2),('dog',3),('apple',0),('bag',1),('cat',2),('dog',3),('apple',0),('bag',1),('cat',2),('dog',3)]
for item,index in items:
    print(index)
    listbox1.insert(index,item)
listbox1.pack()
button = tk.Button(root,text = 'Delete it',\
                   command = lambda x = listbox1:x.delete(x.curselection()))
#lambda expression, parameter before colon, return value after colon
button.pack()
root.mainloop()#Important step, enter the main event cycle, which is monitored by tkinter

                                    Q    The function of the built-in method curselection is the same as the result I want. The way to query the definition of a variable in pycharm is as follows: select a variable, right-click to select go to, and then select Declaration or Usages to jump to the variable definition.

2. When there are many list boxes, the solution

Scheme 1: you can modify the number of displayed rows by modifying the height when defining the list box;
listbox1 = tk.Listbox(root,height = 11). For complete code modification, please go up here and replace the sixth line of the above code with this line

Scheme 2: set scroll bar
There are two things you need to do to install a vertical scroll bar on a component,
a. Set the yscrollbarcommand option of the component to the set() method of the Scrollbar component
b. Set the command option of the Scrollbar component to the yview() method of the component

import tkinter as tk
root = tk.Tk()#Generate top window
root.title("Component use!")#Set GUI title

#Set scroll bar
scrollbar = tk.Scrollbar(root)
scrollbar.pack(side = 'right',fill = 'y')
listbox1 = tk.Listbox(root,yscrollcommand = scrollbar.set)
for i in range(100):
    listbox1.insert((i-1),i)
listbox1.pack(side = 'left',fill = 'both')
scrollbar.config(command = listbox1.yview)
root.mainloop()#Important step, enter the main event cycle, which is monitored by tkinter

Running screenshot:

3. scale component enables users to select a certain range of data

import tkinter as tk
root = tk.Tk()#Generate top window
root.title("Component use!")#Set GUI title
#scale component enables users to select a certain range of data
scale1 = tk.Scale(root,from_ = 0,to = 200,length = 200,tickinterval = 5,resolution = 5)#The length property sets the range component length, the tickenterval property sets the scale, and the resolution sets the sliding step size
scale1.pack()
scale2 = tk.Scale(root,from_ = 0,to = 20,orient = 'horizontal')
scale2.pack()
def show():
    print(scale1.get(),scale2.get())
tk.Button(root,text = 'Get location',command = show).pack()
root.mainloop()#Important step, enter the main event cycle, which is monitored by tkinter

Running screenshot:
Set the data after operation, and click the button to print the data as shown in the following figure:

Posted by xtian on Fri, 26 Jun 2020 20:25:19 -0700