Learning Python- Intermediate course: Day 30, Spinbox and Labelbox

Aatmaj - Sep 22 '21 - - Dev Community

Today let us see how to use the spinbox and listbox widgets widget.


Spin box widget.

The spinbox widget is a type of widget which lets the users choose values in an easy way. The user can navigate through the values using the up and down arrows. You can read more about it here

Making the spinbox widget

The spinbox widget is very similar to the slider widget we learnt in the last part. v all we need to do is replace slider by spinbox

from tkinter import *
frame=Tk()
frame.geometry("200x200")
spinbox=Spinbox(frame,from_=0, to=10)

spinbox.pack()
def show():
    showbutton.config(text=spinbox.get())
showbutton=Button(frame,text="show",command=show)
showbutton.pack()
mainloop()
Enter fullscreen mode Exit fullscreen mode

image

image

image

Here we get the value from the spinbox in the same manner as we did for slider widget, that is, by using the get() method.

Listbox widget

The listbox widget also allows the users to choose values from a given set of string or numerical values. These value sets are determined by the program.

The Listbox widget is used to display a list of items from which a user can select a number of items.

Making a listbox widget

Lb = Listbox(frame) creates a listbox widget. We can add values to the widget using the insert() method.
Here is an example which creates a Listbox

from tkinter import *
frame = Tk()
frame.geometry("200x200")

Lb = Listbox(frame)
Lb.insert(1, "Python")
Lb.insert(2, "R")
Lb.insert(3, "Julia")
Lb.insert(4, "MATLAB")
Lb.insert(5, "Mathematica")
Lb.insert(6, "Haskell")

Lb.pack()
frame.mainloop()
Enter fullscreen mode Exit fullscreen mode

Getting the value from the listbox.

We can get the value using the curselection() method. The curselection returns the position of the selected item.

from tkinter import *
frame = Tk()
frame.geometry("200x200")

Lb = Listbox(frame)
Lb.insert(1, "Python")
Lb.insert(2, "R")
Lb.insert(3, "Julia")
Lb.insert(4, "MATLAB")
Lb.insert(5, "Mathematica")
Lb.insert(6, "Haskell")

Lb.pack()
def show():
    showbutton.config(text=Lb.curselection())
showbutton=Button(frame,text="show",command=show)
showbutton.pack()
mainloop()
frame.mainloop()
Enter fullscreen mode Exit fullscreen mode

image

image

In order to get the value, we can use listbox method get() to return the tuple of values and index the position.showbutton.config(text=Lb.get(Lb.curselection()))
But instead it will be more flexible if we make a tuple of those values themselves and feed them into the program. moreover using a for loop to feed in values enhances flexibility and extensibility.

from tkinter import *
frame = Tk()
frame.geometry("200x200")
items=("Python","R","Julia","MATLAB","Mathematica","Haskell")
Lb = Listbox(frame)
for i in range(0,len(items)):
    Lb.insert(i,items[i])

Lb.pack()
def show():
    showbutton.config(text=items[Lb.curselection()[0]])
showbutton=Button(frame,text="show",command=show)
showbutton.pack()
mainloop()
frame.mainloop()
Enter fullscreen mode Exit fullscreen mode

image

Exercise-
1) Should item be a tuple or a list??
2) What will happen if we remove [0] in items[Lb.curselection()[0]]?
3) Use Lb.get(Lb.curselection()) and rewrite the entire program
4) The above program, lot of lines are wasted. use the height attribute to adjust the number of lines.
5) Does the height attribute take in the number of lines or pixel space? Try to find out using trial and error methods.
6) When you set the height, did you feel the usefulness of flexibility initializing a tuple and setting the values?

Answer in the comments below. Answers will be found in the Learning Python Repository

Types of selections

In the listbox object, there is a attribute called as selectmode. By using this attribute, we can set how we want to select the items.

Selectmode determines how many items can be selected, and how mouse drags affect the selection −

  • BROWSE − Normally, you can only select one line out of a listbox. If you click on an item and then drag to a different line, the selection will follow the mouse. This is the default.
  • SINGLE − You can only select one line, and you can't drag the mouse. Wherever you click button 1, that line is selected.
  • MULTIPLE − You can select any number of lines at once. Clicking on any line toggles whether or not it is selected.
  • EXTENDED − You can select any adjacent group of lines at once by clicking on the first line and dragging to the last line.
from tkinter import *
frame = Tk()
frame.geometry("200x200")
items=("Python","R","Julia","MATLAB","Mathematica","Haskell")
Lb = Listbox(frame,selectmode=MULTIPLE)
for i in range(0,len(items)):
    Lb.insert(i,items[i])

Lb.pack()

mainloop()
frame.mainloop()

Enter fullscreen mode Exit fullscreen mode

image

from tkinter import *
frame = Tk()
frame.geometry("200x200")
items=("Python","R","Julia","MATLAB","Mathematica","Haskell")
Lb = Listbox(frame,selectmode=EXTENDED)
for i in range(0,len(items)):
    Lb.insert(i,items[i])

Lb.pack()

mainloop()
frame.mainloop()
Enter fullscreen mode Exit fullscreen mode

image
In the extended mode, you cannot choose two non-continues values at once. Example, you cannot choose only Haskell and Julia in the above program.

For displaying the contents we will require a better method, like for example textbox. We will need to extract out all values out from the tuple, or convert it out into a string before displaying it.


In tomorrows part, as promised we will make a program with slider and repeat the same with the spinbox widget.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .