tkinter: buttons - marquette university · grid method example • we expand on the previous...
TRANSCRIPT
![Page 1: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/1.jpg)
TkInter: ButtonsPython
Marquette University
![Page 2: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/2.jpg)
Buttons• Apps have buttons
• You press on them, and something happens
• Implementation in TkInter:
• Create button (usually with text, sometimes with an image)
• Always linked with an event handler
• Place button
• Create event handler — a callback function
![Page 3: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/3.jpg)
Buttons• A super-simple example: Create an app
![Page 4: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/4.jpg)
Buttons• Now create two
buttons:
• Need to give a function as the command parameter
• Easiest to define as class parameters
![Page 5: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/5.jpg)
Buttons• Callbacks: Our code tells the button Constructor what to
do in the future, namely when the button is pressed
• Small problem: we pass a function without parameters
![Page 6: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/6.jpg)
Buttons• If we want the button to do something to the app, the
function needs to know how to reach the components
• Solution:
• Create only class fields instead of instance fields
• This way everything is reachable from within function definitions
class MyApp: def __init__(self): MyApp.main = tk.Tk() MyApp.main.title("Buttons") self.create_widgets() MyApp.main.mainloop()
![Page 7: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/7.jpg)
Buttons• Change the background color of the main window
• Uses the configure method and sets the parameter background
def callback1(): print("callback 1 called") MyApp.main.configure(background="Red")
![Page 8: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/8.jpg)
Buttons
![Page 9: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/9.jpg)
Buttons• We can also change the text of a label.
• A polyglot “Hello World” Application
• Main widget is a label with text
• Use width and height to make it big enough
• Number interpreted as text lines
def create_widgets(self): MyApp.label = tk.Label(MyApp.main,
text="Hello World", height = 5, width = 75)
MyApp.label.pack(side="left")
![Page 10: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/10.jpg)
Buttons• Then create a number of buttons
• Each would need their own callback function
• But that is insane
• Can use the lambda trick in order to call a function with different parameters
• RECALL: lambda defines an anonymous python function
• is the same as
• def add(x, y): return x+y
lambda x, y: x+y
![Page 11: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/11.jpg)
![Page 12: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/12.jpg)
Buttons• Define one callback function with an argument
• Define a function derived from that one anonymously
my_button4 = tk.Button(MyApp.main, text=“Español”, command=lambda : MyApp.callback("Hola Mundo"))
my_button4.pack(side="bottom") my_button5 = tk.Button(MyApp.main, text=“Italiano", command=lambda : MyApp.callback("Ciao Mondo")) my_button5.pack(side="bottom") def callback(my_text): MyApp.label.configure(text=my_text)
![Page 13: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/13.jpg)
Buttons• Now we can go overboard and create many buttons
![Page 14: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/14.jpg)
![Page 15: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/15.jpg)
The grid method• Placing widgets with pack does not give a lot of control
• Much more control wielded by grid
• grid takes two coordinates, row and column
• Distributes widgets into rows and columns
• Can use rowspan or columnspan if a widget needs to take up more than a single row or column
![Page 16: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/16.jpg)
grid method example• We expand on the previous example in order to show how
grid works
• The label takes up several rows
• But because it is big, it just defines a single big column
• The buttons are arranged in two columns
• By the way, Python 3 understands UTF, so we can add text in non-latin alphabets (russian, greek, gujarati, mahrati) as well as text with diacritic marks
• I use copy and paste to convince the IDLE editor instead of looking up unicode codes.
![Page 17: TkInter: Buttons - Marquette University · grid method example • We expand on the previous example in order to show how grid works • The label takes up several rows • But because](https://reader034.vdocument.in/reader034/viewer/2022050714/5f0905917e708231d424dabd/html5/thumbnails/17.jpg)