25
loading...
This website collects cookies to deliver better user experience
v
all we need to do is replace slider by spinboxfrom 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()
get()
method.The Listbox widget is used to display a list of items from which a user can select a number of items.
Lb = Listbox(frame)
creates a listbox widget. We can add values to the widget using the insert()
method. 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()
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()
get()
to return the tuple of values and index the position.showbutton.config(text=Lb.get(Lb.curselection()))
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()
item
be a tuple or a list??[0]
in items[Lb.curselection()[0]]
?Lb.get(Lb.curselection())
and rewrite the entire programheight
attribute to adjust the number of lines.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 −
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()
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()
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.