Tkinter
Creating and configuring widgets
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text = 'Click Me')
button.pack()
button['text'] # 'Click Me'
button['text'] = 'Press Me'
button.config(text = 'Push Me')
button.config()
ttk.Label(root, text='Hello Tkinter!').pack()
Managing widget placement
Tk Geometry Managers
- Pack
- Grid
- Place
Handling User Events
When Event Occurs | Execute Handler Code |
---|---|
ButtonPress | handle_buttonpress() |
Key | handle_key() |
Leave | handle_leave() |
Motion | handle_motion() |
Configure | handle_configure() |
Configuting Event Handlers
Command callbacks Available for interactive widgets that produce events Configure by setting widget's "command" property ex) Button
Event bindings Used for events that do not have a command callback Configure by using widget's "bind" method
Sample
#!/usr/bin/python3
# hello_local.py by Barron Stone
# This is an exercise file from Python GUI Development with Tkinter on lynda.com
from tkinter import *
from tkinter import ttk
class HelloApp:
def __init__(self, master):
self.label = ttk.Label(master, text = "Hello, Tkinter!")
self.label.grid(row = 0, column = 0, columnspan = 2)
ttk.Button(master, text = "Texas",
command = self.texas_hello).grid(row = 1, column = 0)
ttk.Button(master, text = "Hawaii",
command = self.hawaii_hello).grid(row = 1, column = 1)
def texas_hello(self):
self.label.config(text = 'Howdy, Tkinter!')
def hawaii_hello(self):
self.label.config(text = 'Aloha, Tkinter!')
def main():
root = Tk()
app = HelloApp(root)
root.mainloop()
if __name__ == "__main__": main()