3 - GUIs With Tkinter PDF
3 - GUIs With Tkinter PDF
3 - GUIs With Tkinter PDF
1 2
3 4
GUIs with Tkinter GUIs with Tkinter
Why GUI? Tkinter
• Or the “visualization” way • Python interface for Tk
• Tk is a library for rapid GUI scripting
• Easy to use
• Good integration in VTK
• Widely available
5 6
def say_hi():
print "Hello Class"
• instantiate Python classes
root = Tk()
7 8
GUIs with Tkinter GUIs with Tkinter
Insertion - Python classes Tk GUI’s are built hierarchical
• Instantiation uses function syntax • You need one root window
root = Tk() root = Tk()
someotherclass.value = 10
9 10
Frame Button
... Button ...
Button
Button ...
11 12
GUIs with Tkinter GUIs with Tkinter
The mainloop() method The event loop in GUI programs
• Last call you issue actively in the entry method
• Starts message processing
• From there, any GUI application is event driven!
13 14
15 16
GUIs with Tkinter GUIs with Tkinter
Widgets Widgets
• Use all the same configuration interface • You can use a dictionary interface
• Works by keyword arguments >>> w["text"]
'button'
>>> w["text"] = "hello"
w = widgetclass(parent, option=value, ...) >>>
• The cget method lets you get an option value • The “keys()” method works as you would expect
w = Button(root, text="button", command=hello) >>> w.keys()
w.cget('command') ['activebackground', 'activeforeground', 'anchor', 'background', 'bd',
>>> '225192hello' 'bg', 'bitmap', 'borderwidth', 'command', 'compound', 'cursor', 'default',
w.cget('text') 'disabledforeground', 'fg', 'font', 'foreground', 'height',
>>> 'button' 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image',
'justify', 'overrelief', 'padx', 'pady', 'relief', 'repeatdelay',
'repeatinterval', 'state', 'takefocus', 'text', 'textvariable',
'underline', 'width', 'wraplength']
>>>
17 18
• Tells the widget to size itself fit to the given contents • side specifies which side to pack the widget against
and make itself visible • fill specifies if the widget should occupy all the space
• Consequence given to it by its parent
• If you forget to call pack() you won’t see your widget! • expand specifies whether the widget is allowed to
expand in order to fill up empty space
19 20
GUIs with Tkinter GUIs with Tkinter
Commonly used widgets Commonly used widgets
• Button • Canvas
• The probably most widely used control in the world • A surface area you can draw on
• Most important options window = Toplevel(width=800, height=600)
tfCanvas = Canvas(ctrlWindow, width=400, height=250, relief=RAISED,
selectbackground='grey')
• command tfCanvas.pack(anchor=NW, side=TOP)
21 22
• Items can be named, queried and so on • Default is: Button selected, var = 1, 0 otherwise
• Details when we need them • You can query the variable like this
• If you are interested anyway, see the tutorial
value = var.get()
23 24
GUIs with Tkinter GUIs with Tkinter
Commonly used widgets Commonly used widgets
• Checkbutton • Entry
• Has a command option that is called when the state • Represents a single line text entry field
changes root = Tk()
e = Entry(root, width=55)
var = IntVar() # this is Tkinters way of saying: I want an int e.pack()
def say_hello(): def say_hello():
print “Hello”, var.get() print e.get()
c = Checkbutton(root, text=”Hello”, command=say_hello, variable=var)
c.pack() b = Button(root,text="Hello", command=say_hello)
b.pack()
root.mainloop()
25 26
b = Button(root,text="Hello", command=say_hello)
b.pack()
root.mainloop()
27 28
GUIs with Tkinter GUIs with Tkinter
Commonly used widgets Commonly used widgets
• Radiobutton root = Tk()
MODES = [
("Monochrome", "1"),
• Used to select one out of several alternatives ("Grayscale", "L"),
("True color", "RGB"),
• Similar to a checkbutton, but ]
("Color separation", "CMYK"),
b = Button(root,text="Hello", command=say_hello)
b.pack()
root.mainloop()
29 30
root.mainloop()
31 32
GUIs with Tkinter GUIs with Tkinter
Commonly used widgets Commonly used widgets
• Text • Toplevel
• Example 2 • A toplevel finally can be used to create more windows
root = Tk()
root.mainloop()
33 34
35 36
GUIs with Tkinter GUIs with Tkinter
Events and Bindings Events and Bindings
• You bind events to widgets using bind() • Event attributes
widget.bind(event, eventhandler) event.widget # the widget generating the event
event.x # current mouse x position in pixels
event.y # current mouse y position in pixels
• Where event handler has the form event.x_root
event.y_root
# current position relative to upper left screen corner
37 38
root.mainloop()
39 40
GUIs with Tkinter GUIs with Tkinter
Events and Bindings Events and Bindings
• Event types • Bindings
• <Return> • The bind() method creates an instance binding
• User pressed the return key
• Can use BackSpace, Tab, Shift_L, Shift_R, Control_L, Alt_L, and so on
• Event handlers are executed on a specific widget instance
• <Key> • You can also bind at toplevel, class or application level
• User pressed any key (query via event.char)
• a, b, c, ...
• Toplevel uses bind() on toplevel window
• User pressed a, b, c ... most printable characters can be used as is • On a class of widgets use bind_class()
• <Shift-Up> • But, don’t do that, really...
• User pressed Shift and the UP arrow
• For application level use bind_all() on the root window
• Most key events can be prefixed by Alt, Shift and Control
41 42
def eventtest(event):
print "Mouse: " , str( (event.x,event.y) )
tl = Toplevel() http://www.pythonware.com/library/tkinter/
t = Text(tl, width=100, height=50)
t.pack()
introduction/index.htm
root.bind_all("<Enter>", eventtest)
def get_value():
v = t.get(1.0,END)
print v
root.mainloop()
43 44