CS 112: Intro to Comp Prog CS 112: Intro to Comp Prog Tkinter Layout Managers: place, pack, grid Custom Frames Widgets In-depth StringVar tkFont Upcoming
Tk Tk ● To use Tkinter Widgets (components: buttons, labels, etc). You must import it from Tkinter import * ● Tk is the top level widget of Tkinter which represents the main window of an application. The simplest Tkinter application: from Tkinter import * Tk().mainloop() -----------OR--------------- from Tkinter import * root = Tk() root.mainloop()
Widget Widget ● A widget is a fundamental unit of Python GUIs, anything you see on the GUI is in fact a widget: **Image slightly modified from Dr. Heishman's CS 112 Slides**
Widget Widget ● Lets add four buttons to our window : from Tkinter import * root = Tk() btn_1 = Button(root, text = "Button 1") btn_2 = Button(root, text = "Button 2") btn_3 = Button(root, text = "Button 3") btn_4 = Button(root, text = "Button 4") root.mainloop() ● NOTHING HAPPENED? ● Python didn't know how to place your widgets on the root window. We need to use a Layout Manager.
Layout Managers Layout Managers ● A Layout manager is a system that determines how to place and size widgets/components ● Python provides three: ● place - Allows programmer to state exactly the location and size of the widgets ● pack - places components in order (a stacking-type approach) ● grid - places components in a grid location (table-like approach) ● Each has their own benefits and pitfalls
place() place() ● Inside the parenthesis we need to specify at least the location of the widget. Example below of placing btn_1 at the top-left corner btn_1.place(x = 0, y= 0) --or-- btn_1.place(relx = 0, rely = 0) ● x and y refer to absolute coordinates in pixel dimension (0,0) is the top-left corner and increase by going to the right (x-direction) or down (y-direction) ● relx and rely refer to relative coordinates based on the component it is placed in. Must be a value in [0.0, 1.0] (ex. 0.5 is the center of the parent) ● width and height are another common arguments as well, which override the default size (size is in pixels) ● Using this manager it is possible to have widgets overlap
place() place() ● anchor is another option, which specifies which part of the widget will be located at the specified coordinates, default is 'nw' (north-west...top- left), possiblities: 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'c' ● Example of placing the 4 buttons from earlier using a variety of arguments: from Tkinter import * root = Tk() btn_1 = Button(root, text = "Button 1") btn_2 = Button(root, text = "Button 2") btn_3 = Button(root, text = "Button 3") btn_4 = Button(root, text = "Button 4") btn_1.place(x = 0, y = 0, width = 150) btn_2.place(relx = 0.5, rely = 0.5, height = 50, anchor = 'c') btn_3.place(x=20,y=15) btn_4.place(x = 30, rely = 0.5, anchor = 'se') root.mainloop()
pack() pack() ● pack() doesn't need any arguments for it places the widget, as the default values will place widgets vertically btn_1.pack() ● side refers to how the components will be arranged: vertically (keywords TOP (default) or BOTTOM ) or horizontally (keywords LEFT or RIGHT )...top and left are typically the ones to use ● fill refers to resizing...should the component resize horizontally (X) or vertically (Y) to fill the parent widget ( X, Y, NONE (default) , and BOTH are valid values) ● There are other argument values, but these two are the most common
pack() pack() ● Example of placing the 4 buttons from earlier using a variety of arguments: from Tkinter import * root = Tk() btn_1 = Button(root, text = "Button 1") btn_2 = Button(root, text = "Button 2") btn_3 = Button(root, text = "Button 3") btn_4 = Button(root, text = "Button 4") btn_1.pack(fill = X, side = BOTTOM) btn_2.pack(fill = X, side = TOP) btn_3.pack(fill = Y, side = RIGHT) btn_4.pack(fill = Y, side = LEFT) root.mainloop()
grid() grid() ● grid() doesn't need any arguments for it places the widget, as the default values will place widgets vertically (column = 0...row increments) btn_1.grid() ● r ow and column refer to which grid location it belongs in (0,0) is the top-left spot with the values increasing to the right and down ● columnspan and rowspan refer to the number of columns or rows the widget will occupy ● sticky defines what side the component should stick to (if N it will be locked to the top of the cell) (N,S,E,W are valid options...you can also set the value as a combination: for example N+S means it will expand vertically to fill the cell ● There are other argument values, but these two sets are the most common ● NEVER use grid() and pack() in the same parent!!! It will freeze python!
grid() grid() ● Example of placing the 4 buttons from earlier using a variety of arguments: from Tkinter import * root = Tk() btn_1 = Button(root, text = "Button 1") btn_2 = Button(root, text = "Button 2") btn_3 = Button(root, text = "Button 3") btn_4 = Button(root, text = "Button 4") btn_1.grid(row=0, column=0, rowspan = 2,sticky = N+S) btn_2.grid(row=0,column=1) btn_3.grid(row=1,column=1) btn_4.grid(row=2, column=0, columnspan=2,sticky = E+W) root.mainloop()
Frame Frame ● A frame is a specialized type of widget, which can contain other widgets.(e.g. you can place Buttons, Labels, etc inside of it). Since it is also a widget, you can place Frames within Frames. ● We can write our own Frame(s) that can be used multiple times, by using classes, Example: from Tkinter import * class MyFrame(Frame): def __init__(self,root,name="Test"): Frame.__init__(self,root) self.btn_1 = Button(self, text = name+" 1") self.btn_2 = Button(self, text = name+" 2") self.btn_3 = Button(self, text = name+" 3") self.btn_4 = Button(self, text = name+" 4") self.btn_1.grid(row=0, column=0, rowspan = 2,sticky = N+S) self.btn_2.grid(row=0,column=1) self.btn_3.grid(row=1,column=1) self.btn_4.grid(row=2, column=0, columnspan=2,sticky = E+W) root = Tk() f = MyFrame(root, "One") f2 = MyFrame(root, "Two") f3 = MyFrame(root, "Three") f4 = MyFrame(root, "Four") f.grid(row = 0, column = 0) f2.grid(row = 0, column = 1) f3.grid(row = 1, column = 0) f4.grid(row = 1, column = 1) root.mainloop()
Widgets In-depth Widgets In-depth ● Upon construction of widgets we can pass in arguments that can set the size (so long as they are not using the place layout manager), text, or command depending on whether or not an event is being used, common arguments for some widgets are listed below: Button - ● ● text - set the text of the button ● command - function name to be called, when button is pressed Label - ● ● text - set the text of the label Entry - ● ● (mainly use the common arguments below...particular importance on textvariable) Common arguments for all of the above: ● ● background - set the back color using strings...”black”,”blue”,”red”,... ● font - the size, family, bolded, ...font for the text (tkFont) ● foreground - set the text color using strings...”black”,”blue”,”red”,... ● textvariable - associate a Tkinter StringVar variable to the text
Widgets In-depth Widgets In-depth ● Example of using a separate Frame class, that uses buttons, labels, and entrys: from Tkinter import * class MyFrame(Frame): def __init__(self,root): Frame.__init__(self,root) self.data = StringVar(self,"Before") self.setup_widgets() def setup_widgets(self): self.btn = Button(self, text = "My Button",\ background = "green", foreground = "white",\ command = self.press_btn) self.lbl = Label(self,foreground="blue",text = "Blah") self.entry = Entry(self,textvariable = self.data) self.lbl.grid(row=0,column=0) self.entry.grid(row=0,column=1) self.btn.grid(row=1,column=0,columnspan=2,sticky=W+E) def press_btn(self): Before Button Click After Button Click self.data.set("After") root = Tk() f = MyFrame(root) f.pack() root.mainloop()
StringVar StringVar ● StringVar is a class that stores a string that can be accessed through get and set methods. ● It's constructor takes the parent (typically self ) and an initial value: ● StringVar(self, “text”) ● When the textvariable = is used, the variable becomes associated with the text of the widget. If the StringVar is modified (set with new text, then all widgets that are associated with the variable, then their text will also be updated (The Entry in the last example) ● get() will return the current string value of the StringVar variable, can be used to get what the user changes the Entry to . ● set(“new text”) will updated and set the value to what is passed as an argument
Recommend
More recommend