COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Computer Science 112 Fundamentals of Programming II User Interfaces
Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
HTML Essentials Tables and Table Tags. Overview Use of Tables goes beyond tabulating data Frequently used to format Web pages / control layout Especially.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Getting started (For the absolute beginner: download and install python) Task 1: Creating 3 widgets: a label, text entry and a button (and ‘PACK’) Task.
WEEK Introduction to GUI programming. Introduction Each data type can represent a certain set of values, and each had a set of associated operations.
COMPSCI 101 Principles of Programming
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
The University of Texas – Pan American
Microsoft Word 2000 Presentation 5. Major Word Topics Columns Tables Lists.
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Graphical User Interface Components: Part 1 Outline 10.1 Introduction 10.2 Tkinter Overview 10.3.
PYTHON GUI PROGRAMMING
Computer Information Technology – Section 4-12 Some text and examples used with permission from: Note: We not endorsing or promoting.
Copyright 2006 South-Western/Thomson Learning Chapter 12 Tables.
Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
PERL TK. 4.Use an IDE 3. Use the documentation! 2. Experiment. 1.Learn the basics.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Tkinter Basics. Initial Stuff To get the package: from tkinter import * To make a window, give it a title and operate it: windowVar = Tk() windowVar.title(“your.
Creating visual interfaces in python
How To Create HTML Tables. Table Structure General HTML code for a Web Table: table cells table cells.
Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective.
Guide to Programming with Python
Java Swing - Lecture 3 Layout Management
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
See Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
COMPSA Exam Prep Session On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
Innovation Intelligence ® Feb Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. GUI design Controls Toplevel.
Topics Graphical User Interfaces Using the tkinter Module
Event Loops and GUI Intro2CS – weeks
Graphical User Interfaces (GUIs)
Creating LOVs and Editors
Positioning Objects with CSS and Tables
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
My Final Project: Calorie Counter
CISC101 Reminders Last lecture!  Grading of Quiz 4 underway.
Chap 7. Building Java Graphical User Interfaces
CISC101 Reminders Grading of Quiz 4 underway.
GUI Using Python.
Graphical User Interfaces -- Introduction
Fundamentals of Python: From First Programs Through Data Structures
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
CISC101 Reminders Quiz 2 this week.
Tkinter Widgets CS 260 Dick Steflik.
CISC124 Last Quiz this week. Topics listed in last Tuesday’s lecture.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
NexTk/NtkWidget A replacement for Tk ?! 1.
Fundamentals of Programming I Windows, Labels, and Command Buttons
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Topics Graphical User Interfaces Using the tkinter Module
CISC101 Reminders Assignment 2 due this Friday.
Python – Tkinter Windows Application
CISC101 Reminders Assignment 3 due today.
Positioning Objects with CSS and Tables
Tkinter User Input Form
Presentation transcript:

COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1

Today… Continue GUI with Tkinter: –Labels –Using the grid manager –Widget properties and how to change them –Buttons –Getting a button to do something. Winter 2016CISC101 - Prof. McLeod2

3 Using the tkinter Module First you must import the tkinter module: import tkinter Or: from tkinter import * If this does not work, then you may have to install the module – but, for Windows Python installs, it should already be there. Winter 2016

CISC101 - Prof. McLeod4 Using tkinter, Cont. Create your main window (or “root” window) using: top_window = tkinter.Tk() Then start the main loop using: tkinter.mainloop() (This assumes you used the first import statement.) Winter 2016

CISC101 - Prof. McLeod5 Using tkinter, Cont. The first line: top_window = tkinter.Tk() invokes the constructor method for the Tk object and now top_window is an object of this type. The main loop is the main “listener” loop for the window – it will wait very patiently for the user to do something! See Window1.py. Note functionality built into even this simple window! Winter 2016

CISC101 - Prof. McLeod6 Add a Label See Window2.py: helloWorldLabel = tkinter.Label(top, text="HelloWorld!") helloWorldLabel.pack() The pack() is invoked to place the label into the first available position in the window. Pretty small font – see Window3.py How can we get away from this top-down stacking of widgets in the window? Winter 2016

CISC101 - Prof. McLeod7 Using Frames See Window4.py Create a Frame, add labels to the Frame, pack the labels and specify a relative position in the Frame, then pack the Frame. Winter 2016

So Far… Can change the window title. Add labels. Customize the font of the label. Use frames. But, using the packer makes for some pretty ugly designs!! CISC101 - Prof. McLeod8Winter 2016

CISC101 - Prof. McLeod9 The Grid Manager The “packer” is very simple but just jams stuff in from top to bottom. You must use frames to get more than one widget in a row. The Grid manager is an alternative that is just a bit harder to use. You specify a row and column position instead. Do not mix the packer and grid together – use one or the other! Winter 2016

CISC101 - Prof. McLeod10 The Grid Manager, Cont. Grid method syntax:.grid(row=?, column=?, rowspan=?, columnspan=?, sticky="news", ipadx=?, ipady=?, padx=?, pady=?) "news" is North, East, West, and/or South. row, column positions and rowspan, columnspan are ints. Other “pad” dimensions are in pixels. Winter 2016

CISC101 - Prof. McLeod11 The Grid Manager, Cont. See Window5.py Note how the columns and rows are sized to the width and height of the largest widget in that row or column. Widgets are centre-aligned by default – change this using the sticky option. They can span multiple rows and columns – use rowspan or columnspan (or both!). Add extra “padding” using padx and pady. Winter 2016

CISC101 - Prof. McLeod12 Widget Options or “Properties” Before we go too much further: widget_name.keys() gives you a list of dictionary keys, each of which is an option or “property” that can be changed for that widget. Any set of these keys can be set when you create the widget. widget_name.configure() (no parameters) gives you a list of the keys and their values (a dictionary). Winter 2016

CISC101 - Prof. McLeod13 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. Winter 2016

Widget Properties, Cont. While many properties are common to every widget a few are unique for a particular widget type. All of the label properties: CISC101 - Prof. McLeod14Winter 2016

activebackground : SystemButtonFace activeforeground : SystemButtonText anchor : center background : SystemButtonFace bd : 2 bg : SystemButtonFace bitmap : borderwidth : 2 compound : none cursor : disabledforeground : SystemDisabledText fg : SystemButtonText font : Times 20 bold foreground : SystemButtonText height : 0 highlightbackground : SystemButtonFace highlightcolor : SystemWindowFrame CISC101 - Prof. McLeod15 highlightthickness : 0 image : justify : center padx : 1 pady : 1 relief : flat state : normal takefocus : 0 text : Hello textvariable : underline : -1 width : 0 wraplength : 0 Label Properties and Default Values Winter 2016

Widget Properties, Cont. The property values that start with System… are determined by your OS preference settings. Most properties will remain at their default values – you only change the ones you want to change. For a reference on tkinter, see: CISC101 - Prof. McLeod16Winter 2016

Getting/Setting Widget Properties Use the cget method along with the property name to get the current property value: testLabel.cget("text") Or, you can treat the widget variable as a dictionary: testLabel["text"] CISC101 - Prof. McLeod17Winter 2016

Getting/Setting Widget Properties, Cont. Use the configure method to set the property value: testLabel.configure(text="Hello") Or: testLabel.config(text="Hello") Or, use it as a dictionary again: testLabel["text"] = "Hello" CISC101 - Prof. McLeod18Winter 2016

Demo Program See LabelProperties.py Using the “gridder” instead of the “packer”. Explores: –the basic form –widget properties, especially for a Label –using and configuring the grid –padding, anchoring, spacing, colours, cursors, fonts, an image in a widget, borders, backgrounds, … –the use of a LabelFrame widget Winter 2016CISC101 - Prof. McLeod19

Named Colours See Lots of them! Also see “Widget Styling” in “Tkinter docs from effbot”: styling.htm You can use a colour chooser dialog to allow the user to set colours (remember the “spiral” demo?) Winter 2016CISC101 - Prof. McLeod20

Widgets The most common widgets in windows are buttons, labels and entry (or text) boxes. You can build some very functional GUI windows with just these components. So far we have just covered labels. Not very “interactive” windows! Yet! CISC101 - Prof. McLeod21Winter 2016

CISC101 - Prof. McLeod22 Button Widget A button allows the user to initiate some event, most often to invoke some function in your program. Syntax: button = Tkinter.Button(master, text=?, command=?) master is the window’s name. This parameter must be present when you create any widget. Winter 2016

CISC101 - Prof. McLeod23 Button Widget, Cont. See Window6.py. Note that the command option is given the name of a function, not as a string and it cannot have brackets or parameters. Why are top and testLabel global variables? That window re-sizing thing is really annoying! How to fix it? Winter 2016

CISC101 - Prof. McLeod24 Grid Manager Methods Use the columnconfigure() or rowconfigure() methods on the master (“top” in our demos): columnconfigure(column, option=value,...) Options include: minsize, pad and weight. Winter 2016

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. McLeod25