Download presentation
Presentation is loading. Please wait.
1
CISC101 Reminders Grading of Quiz 4 underway.
Winter 2018 CISC101 9/20/2018 CISC101 Reminders Grading of Quiz 4 underway. Last assignment due Friday. We have covered everything you need to know for the assignment. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today GUI Construction With tkinter, Cont.: Next time: Widget Options.
More about Buttons. Colours. tkinter Dialogs. The Entry Widget. Coupling and Binding… Next time: Radiobutton and Canvas Widgets. Bears Demo. More Python tools and modules. Winter 2018 CISC101 - Prof. McLeod
3
Widget Options widget_name.keys() gives you a list of dictionary keys, each of which is an option that can be changed for that widget. Any set of these keys can be set when you create the widget. Or you just accept the default values. widget_name.configure() (no parameters) gives you a list of the keys and their values (a dictionary). Winter 2018 CISC101 - Prof. McLeod
4
Widget Options, Cont. widget_name.cget(option) gives the value of the specified option as a string. widget_name.configure(option=?, option=?, …) allows you to change as many options as you want all at once. You can shorten .configure to just .config. You can treat the widget as a dictionary if you are only getting or setting a single property (use [ ]). Winter 2018 CISC101 - Prof. McLeod
5
Button Widget, Cont. See ButtonDemo.py:
Binding a keypress event to the window. Jazzing up the button. The keypress binding can only have the event object. See the help docs for all the event members that you can use. Winter 2018 CISC101 - Prof. McLeod
6
Virtual Events You can combine events to a single virtual event, for convenience. See ButtonDemoV2.py Winter 2018 CISC101 - Prof. McLeod
7
Colours! The colour of anything can be changed – the window background, the widget background and the foreground (often the text). Colours are specified using a name known to tkinter or the RGB system: “Red Green Blue”. Winter 2018 CISC101 - Prof. McLeod
8
CISC101 Colours, Cont. We have specified colours manually using “#RRGGBB” as a string. You can also specify or save colours as a three element tuple, with the three intensity levels. The intensity of each colour lies between 0x00 and 0xFF, or 0 and 255 (one byte). Possible range of 256*256*256 = 16,777,216 colours (called “24 bit colour”). (Windows uses 32 bit colour, where the extra byte is used to specify transparency.) Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
9
Colours, Cont. This demo program is also using a colour chooser dialog box called tkinter.colorchooser This is a GUI window that has already been constructed for us. (We have used this dialog with a turtle demo before…) See Window8.py. Winter 2018 CISC101 - Prof. McLeod
10
CISC101 Other Dialog Boxes tkinter.commondialog - Base class for the dialogs defined in the other modules listed here. tkinter.filedialog - Common dialogs to allow the user to specify a file to open or save. tkinter.messagebox - Access to standard Tk dialog boxes. tkinter.simpledialog - Basic dialogs and convenience functions. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
11
tkinter.filedialog Use this to get a filename.
Much easier than getting the user to try typing in a filename, let alone a path! See Window9.py. Winter 2018 CISC101 - Prof. McLeod
12
Other Dialog Boxes, Cont.
tkinter.messagebox defines various “ask” and “show” dialogs: askokcancel, askquestion, askretrycancel, askyesno, showerror, showinfo, showwarning. See Window10.py Also note the “Quit” button. This will only work properly if the program is run outside of IDLE. (Type “python window10.py” at a command prompt, for example). Or: Winter 2018 CISC101 - Prof. McLeod
13
Running a tkinter Program
When debugging it helps to run your program from inside IDLE where you can see error messages being displayed to the shell window (or “console”). But if you double-click on a tkinter *.py program an annoying DOS (or “command”) window shows up and goes away. And the top.quit command does not work. yuk. Make a copy of your program with the extension *.pyw. Winter 2018 CISC101 - Prof. McLeod
14
Running a tkinter Program, Cont.
This associates your program with Pythonw.exe rather than the normal Python.exe program. Now it will run without the annoying DOS window if you run it from Windows Explorer, for example. But, you won’t see any console error messages. Winter 2018 CISC101 - Prof. McLeod
15
Other Dialog Boxes, Cont.
tkinter.simpledialog defines askinteger(title, prompt, initialvalue, minvalue, maxvalue), askfloat and askstring. See Window11.py Note behaviour if illegal value supplied! Winter 2018 CISC101 - Prof. McLeod
16
CISC101 Entry Widget Allows the user to provide information to your program by typing it into the text box. Specify a width (width=?), in characters, in addition to the master and the font, as a minimum when you create one. Use the get() method on the Entry widget to get the contents – returned as a string only. See Window12.py Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
17
Coupling a Variable to a Widget
You can couple a variable to an Entry widget (for example) so that when the variable changes the text in the entry box changes, or if you change the text in the entry box the contents of the variable changes. Use StringVar(), IntVar(), DoubleVar(), or BooleanVar(). Use the “textvariable” property to couple the variable to the widget. Winter 2018 CISC101 - Prof. McLeod
18
Variable Coupling, Cont.
Generate the coupled variable: contents = tkinter.StringVar() Couple variable to entry widget: textEntry = tkinter.Entry(…, textvariable=contents) Setting variable changes entry contents, too: contents.set("Hello") Get the variable contents with: contents.get() Winter 2018 CISC101 - Prof. McLeod
19
Clean Up Window12.py Make it so the result label empties out if the input number changes. Make it keyboard responsive: <Enter> should calculate the result. <Esc> should close the window. See Window12C.py (or Window12C.pyw). Winter 2018 CISC101 - Prof. McLeod
20
widget.bind(key name(s), function)
Bindings Use the .bind method invoked from the widget: widget.bind(key name(s), function) Now, when the widget has the focus, the key press will run the named function. You can bind functions to the entire window, as well – then it does not matter which widget has the focus. has a list of key names. Winter 2018 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.