Download presentation
Presentation is loading. Please wait.
Published byColleen Green Modified over 8 years ago
1
See https://www.youtube.com/watch?v=P2SsIYEbCio Winter 2016CISC101 - Prof. McLeod1
2
Winter 2016CISC101 - Prof. McLeod2 CISC101 Reminders Assignment 5 is posted – due today. Solution will be posted after the “ultimate” deadline – April 6. Exam preparation/topics page is posted. Quizzes not picked up in class are in GOO241.
3
COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location: GOO 254 (Goodwin Hall) Winter 2016CISC101 - Prof. McLeod3
4
Today… Continue GUI with Tkinter: –Buttons, Cont. –Obtaining information from the user – dialogs and the entry widget. –Binding to a variable. A more advance example – Bears from exercise 8. Where to go from here! Tools and libraries. Winter 2016CISC101 - Prof. McLeod4
5
A Button See ButtonDemo.py: –Binding a function call to a button event. –Binding a keypress event to the window. –Jazzing up the button. Note that the function call (the “handler”) for the button click cannot take any arguments and cannot return anything. The keypress binding can only have the event object. See the help docs for all the event members that you can use. Winter 2016CISC101 - Prof. McLeod5
6
Colours! So far the demo programs have just used the default colours. But, the colour of anything can be changed – the window background, the widget background and the foreground (often the text). How is colour specified? CISC101 - Prof. McLeod6Winter 2016
7
CISC101 - Prof. McLeod7 Colours, Cont. Colours are specified using the RGB system. “Red Green Blue”. You can save a colour 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 2016
8
CISC101 - Prof. McLeod8 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. See Window8.py. Winter 2016
9
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. CISC101 - Prof. McLeod9Winter 2016
10
CISC101 - Prof. McLeod10 Other Dialog Boxes, Cont. tkinter.messagebox defines various “ask” and “show” dialogs: askokcancel, askquestion, askretrycancel, askyesno, showerror, showinfo, showwarning. See Window10.py Winter 2016
11
CISC101 - Prof. McLeod11 Other Dialog Boxes, Cont. tkinter.simpledialog defines askinteger(title, prompt, initialvalue, minvalue, maxvalue), askfloat and askstring. See Window11.py (on your own). Note behaviour if illegal value supplied! Winter 2016
12
CISC101 - Prof. McLeod12 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 2016
13
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 bind the variable to the widget. CISC101 - Prof. McLeod13Winter 2016
14
Variable Binding, Cont. Generate the coupled variable: contents = tkinter.StringVar() Bind 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() CISC101 - Prof. McLeod14Winter 2016
15
An Advanced Demo: BearsDBV2.py Allows viewing and editing of the bears database, one record at a time. (Based on Exercise 8.) –Entry Widgets, Buttons, Labels, a Frame, and a LabelFrame. –Control Variables. –Binding of control variables and handlers (functions). –No more gobals! Handler functions have been declared inside main! –Button navigation. –Store and display images in a Canvas widget. –Use of a filedialog to obtain an image file. –Use of an errordialog to provide a file not found message. Winter 2016CISC101 - Prof. McLeod15
16
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 sits there. yuk. Make a copy of your program with the extension *.pyw. Winter 2016CISC101 - Prof. McLeod16
17
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 2016CISC101 - Prof. McLeod17
18
Other GUI Tools Lots! IDE and different Widget sets. See WxWidgets, for example. Start by looking at: https://wiki.python.org/moin/GuiProgramming WxPython: http://www.wxpython.org/ Or see gui2py: https://code.google.com/p/gui2py/ Winter 2016CISC101 - Prof. McLeod18
19
Other IDEs IDLE is pretty simple and a good place to start learning Python. But, it can be frustrating after a while if you are used to more sophisticated IDEs: –Poor intellisense and code completion. –No debugger. –No line numbering! –No code wizards, project browsers, etc. “Multi-Lingual” IDEs like Visual Studio (uses “PTVS”) and Eclipse (uses PyDev) can be configured to help write Python programs. Winter 2016CISC101 - Prof. McLeod19
20
Other IDEs, Cont. PyCharm: http://www.jetbrains.com/pycharm/ Winter 2016CISC101 - Prof. McLeod20
21
Other Modules Lots and Lots! See the Python Package Index at: https://pypi.python.org/pypi Winter 2016CISC101 - Prof. McLeod21
22
For Example: Enthought Canopy A collection of many packages from many sources – targeted towards data analysis, modeling and visualization. https://enthought.com/products/canopy/ Like Matlab, but based on Python and all code is open, not proprietary. Seems to still be based on Python 2.7… Free for academic use! Winter 2016CISC101 - Prof. McLeod22
23
Python vs Matlab See: http://www.pyzo.org/python_vs_matlab.html And: http://kitchingroup.cheme.cmu.edu/blog/2013/12/30/ Python-as-alternative-to-Matlab-for-engineering- calculations/ Winter 2016CISC101 - Prof. McLeod23
24
From: http://xkcd.com/353/ CISC101 - Prof. McLeod24Winter 2016
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.