Button:
The button widget is used for creating a button that triggers something when the user clicks on it.
Code: Select all
button = Tkinter.Button(main_window, text="This is a button\nClick Me!", command=main)- main_window: the variable of the main window where the button will be placed
- text: the text to be displayed in the button
- command: the function to call when the button is pressed. Be careful because main and main() are very different, when you give a function value it has to be just the name of the function without brackets, otherwise it will autoexecute.
Checkbutton:
A checkbutton is used to allow the user to select a true/false option, it requires the use of IntVar() to keep track of its state:
Code: Select all
from Tkinter import IntVar
checkbutton_variable = IntVar()
checkbutton = Tkinter.Checkbutton(main_window, text="This is a checkbutton", variable=checkbutton_variable)Code: Select all
checkbutton_variable.get()Code: Select all
if checkbutton_variable.get() == 0: print "Not Ticked"
elif checkbutton_variable.get() == 1: print "Ticked"Entry:
This widget is used to receive a string from the user.
Code: Select all
entry = Tkinter.Entry(main_window)To get the text inputted by the user:
Code: Select all
entry.get()Radiobutton:
Did you take note about IntVar()? Good cause you're gonna need it for this widget. The radiobutton widget is used to allow the user to choose between two or more values:
Code: Select all
radiobutton_var = IntVar()
radiobutton1 = Tkinter.Radiobutton(main_window, text="Option 1", variable=radiobutton_var, value=1)
radiobutton2 = Tkinter.Radiobutton(main_window, text="Option 2", variable=radiobutton_var, value=2)
radiobutton3 = Tkinter.Radiobutton(main_window, text="Option 3", variable=radiobutton_var, value=3)Listbox:
The listbox widget is similar to the radiobutton but the visual is different. This also allows the user to select different values inside a box:
Code: Select all
listbox = Tkinter.Listbox(main_window)
listbox.insert(1, "Option 1")
listbox.insert(2, "Option 2")
listbox.insert(3, "Option 3")
Code: Select all
list_ = ["option 1", "option 2", "option 3"]
listbox = Tkinter.Listbox(main_window)
for i in list_:
listbox.insert(1, i)Code: Select all
listbox.get()Code: Select all
listbox.get(listbox.curselection()[0])Spinbox:
This one is used to select a fix amount (i.e. from 0 to 9, from 10 to 100, etc), it is fairly simple:
Code: Select all
spinbox = Tkinter.Spinbox(main_window, from_=0, to=10)Code: Select all
spinbox.get()Notice how the value is from_ instead of from, this is to distinguish it from python's built-in from found in code like:
Code: Select all
from Tkinter import IntVarThis are the most used widgets but there are more, you can refer to this great site for more widget and examples:
http://www.tutorialspoint.com/python/py ... amming.htm
Now lets put all the above into practice with the following code:
Code: Select all
#! /usr/bin/env python
import Tkinter
from Tkinter import IntVar
def main():
if checkbutton_variable.get() == 0: print "Checkbutton: not ticked"
elif checkbutton_variable.get() == 1: print "Checkbutton: ticked"
print "Entry:", entry.get()
if radiobutton_var.get() == 0: print "Radiobutton: None selected"
elif radiobutton_var.get() == 1: print "Radionbutton: Option 1 selected"
elif radiobutton_var.get() == 2: print "Radionbutton: Option 2 selected"
elif radiobutton_var.get() == 3: print "Radionbutton: Option 3 selected"
try: print "Listbox:", listbox.get(listbox.curselection()[0])
except: print "Listbox: nothing selected"
print "Spinbox value:", spinbox.get()
main_window = Tkinter.Tk()
checkbutton_variable = IntVar()
checkbutton = Tkinter.Checkbutton(main_window, text="This is a checkbutton", variable=checkbutton_variable)
entry = Tkinter.Entry(main_window)
radiobutton_var = IntVar()
radiobutton1 = Tkinter.Radiobutton(main_window, text="Option 1", variable=radiobutton_var, value=1)
radiobutton2 = Tkinter.Radiobutton(main_window, text="Option 2", variable=radiobutton_var, value=2)
radiobutton3 = Tkinter.Radiobutton(main_window, text="Option 3", variable=radiobutton_var, value=3)
listbox = Tkinter.Listbox(main_window)
list_ = ["Option 1", "Option 2", "Option 3"]
for i in list_:
listbox.insert(1, i)
#listbox.insert(1, "Option 1")
#listbox.insert(2, "Option 2")
#listbox.insert(3, "Option 3")
spinbox = Tkinter.Spinbox(main_window, from_=0, to=10)
button = Tkinter.Button(main_window, text="This is a button\nClick Me!", command=main)
checkbutton.pack()
entry.pack()
radiobutton1.pack()
radiobutton2.pack()
radiobutton3.pack()
listbox.pack()
spinbox.pack()
button.pack()
main_window.mainloop()See you soon...
Previous: viewtopic.php?f=37&t=13056
Next: viewtopic.php?f=37&t=13112
Advertising