Tkinter Part II: Buttons, Lambda & Dynamic Content July 8, 2015 Brian A. Malloy Slide 1 of 11
1. We further investigate Labels and Buttons and hook Python actions to these widgets. We present lambda functions, useful in GUIs We show how to bind keystrokes and mouse events to Python code In Part III we examine Frames to help with layout management, and show some additional Tkinter widgets. Slide 2 of 11
2. 1 import Tkinter as tk 2 counter = 0 3 def counterlabel(label): 4 def count(): 5 global counter 6 counter += 1 7 label.config(text=str(counter)) 8 label.after(1000, count) 9 count() 10 11 root = tk.tk() 12 root.title("second Timer") 13 label = tk.label(root, fg="green") 14 label.pack() 15 counterlabel(label) 16 button = tk.button(root, text= Stop, \ width=25, command=root.destroy) 17 button.pack() 18 root.mainloop() Slide 3 of 11
2.1. Explained Line #12 sets the title of the window Line #13 instantiates a Label Line #16 instantiates a Button Line #15 calls function counterlabel which: 1. calls count on line #9 2. count updates global counter (line #6) 3. count schedules the next call to count using a method in Label, after, which takes 2 parameters: (1) the delay, and (2) the function to call after the delay. (line #8) Slide 4 of 11
2.2. Connecting Widgets with Code On line #7 the text in the Label widget is updated every 1000 milliseconds, which is every second. On line #16, a Button is instantiated, including the command to be executed when the button is pushed. That command is root.destroy destroy terminates the mainloop and deletes all widgets As an exercise, convert the Timer program so that it uses a class. Slide 5 of 11
3. 1 import Tkinter as tk 2 class App: 3 def init (self, master): 4 self.button = tk.button(master, 5 text="quit", fg="red", 6 command=master.quit) 7 self.button.pack(side=tk.left) 8 self.slogan = tk.button(master, text="hello", 9 command=self.writeslogan) 10 self.slogan.pack(side=tk.left) 11 def writeslogan(self): 12 print "Tkinter is easy to use!" 13 14 root = tk.tk() 15 app = App(root) 16 root.mainloop() Slide 6 of 11
3.1. Buttton Class Explained In this example we package the GUI in a class: App We pass root to the constructor Line #6 connects the code master.quit to the button, which stops the tk interpreter Line #9 connects the code in writeslogan to the slogan button. Slide 7 of 11
4. The lambda operator is a way to create small, anonymous functions. They are dixie cup functions that you only use once where they are created. Syntax: lambda argument list: expression Example: >>> f = lambda x, y : x + y >>> f(1,1) 2 Slide 8 of 11
5. 1 import Tkinter as tk 2 GEOMETRY = 300x300+200+60 3 def callback(event): 4 print event.x, event.y 5 6 root = tk.tk() 7 root.geometry(geometry) 8 root.title("my First Button") 9 root.bind( <Escape>, (lambda event: root.quit())) 10 myfont = ( symbol, 12) 11 12 label = tk.label(root, font = myfont, text="hello") 13 label.config(relief=tk.raised) 14 label.config(width=25, height=5, bg="#00ff00", bd=5) 15 label.pack(side=tk.top, padx=5, pady=5) 16 label.bind("<button>", callback) 17 root.mainloop() Slide 9 of 11
5.1. What Does The figure shows the widgets on the right, and the output of the program on the left. The output representes the (x, y) coordinates of the mouse when the left mouse button is clicked. The program terminates when the escape key is pressed in the green Label. Slide 10 of 11
5.2. Key Bind Explained Lines #2 & #7 set size & position of the tk window; size is 300x300, position is (200, 60) Line #9 binds the escape key to a lambda function that terminates the tk program. Line #10 sets the font Line #13 sets the relief of the Label widget. Line #14 uses hex to set the background color to green, and sets the border (bd) to be 5 pixels. Line #16 binds the left mouse button to the function on line #3, callback; we pass the mouse event to the callback function so it can print the mouse coordinates at the time of the left button click. Slide 11 of 11