Graphical User Interfaces (GUIs)

Slides:



Advertisements
Similar presentations
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Advertisements

Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Create a Narrated Story with PowerPoint. Basics Enter Text. (Click in the text box and start typing. If a text box is not visible, go to Insert > Text.
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.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
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.
COMPSCI 101 Principles of Programming
18. Python - GUI Programming (Tkinter)
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
PYTHON GUI PROGRAMMING
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.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
© 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.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Creating visual interfaces in python
Tutorial 7 Creating Animations. XP Objectives Learn about animation Create a timeline Add AP divs and graphics to a timeline Move and resize animation.
Guide to Programming with Python
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
See Winter 2016CISC101 - Prof. McLeod1.
LEARN TO FORMAT TABLES Unit 10: Lessons What is a Table? ◦ A table is an arrangement of data (words and/or numbers) in rows and columns. ◦ A table.
Innovation Intelligence ® Feb Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. GUI design Controls Toplevel.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Topics Graphical User Interfaces Using the tkinter Module
Event Loops and GUI Intro2CS – weeks
PYGAME.
Chapter 21 – Graphics/Tk Outline 21.1 Introduction 21.2 GD Module: Creating Simple Shapes 21.3 GD Module: Image Manipulation 21.4 Chart Module 21.5 Introduction.
Chapter Topics 15.1 Graphical User Interfaces
Chapter 2 – Introduction to the Visual Studio .NET IDE
Lecture 27 Creating Custom GUIs
European Computer Driving Licence
My Final Project: Calorie Counter
Ellen Walker Hiram College
CISC101 Reminders Grading of Quiz 4 underway.
DB Implementation: MS Access Forms
GUI Using Python.
Fundamentals of Python: From First Programs Through Data Structures
Tkinter Python User Interface
Program and Graphical User Interface Design
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Chapter 2 – Introduction to the Visual Studio .NET IDE
Tkinter Widgets CS 260 Dick Steflik.
Tutorial 6 Creating Dynamic Pages
GTK + Programming.
DB Implementation: MS Access Forms
I210 review.
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:
Computer Science 111 Fundamentals of Programming I User Interfaces
Topics Graphical User Interfaces Using the tkinter Module
Computer Science 111 Fundamentals of Programming I
Chapter 15: GUI Applications & Event-Driven Programming
Visual C# - GUI and controls - 1
Python – Tkinter Windows Application
The University of Texas – Pan American
Tkinter User Input Form
Presentation transcript:

Graphical User Interfaces (GUIs) CS 2316 Fall 2016

Before we had GUIs: > Everything was run through the command line/terminal!

GUI: Graphical User Interface A way for people to interact with computer programs Uses windows, icons and menus Can be manipulated by a mouse and keyboard So far in this course we’ve written programs that the user must interact with in the command line. Today we’ll learn how to make a GUI for users to interact with your program.

GUIs: Step by Step Design the GUI Create all the buttons, labels, entries, etc. (widgets) And specify where they go in a window Write functions/methods to do what needs to be done Connect the widgets to the functions/methods Start the main loop

Tkinter We will use the Tkinter GUI toolkit to create Python GUIs tkinter: the module containing all the code To import the tkinter library: from tkinter import * https://docs.python.org/3/library/tk.html Tkinter is the de facto Python standard GUI library, it comes with your installation of Python and doesn’t need to be installed separately

Some simple tkinter widgets Label Just displays text, not clickable Button Can be linked to a function/method; that function/method is called on click Entry Can allow the user to enter text Can also be used to display text Has 3 possible states: normal, readonly, disabled There are many GUI widgets but here are 3 common ones

Basic tkinter GUI win = Tk() win.title("My GUI") l = Label(win, text="Hello, world") b = Button(win, text="Click me!") l.pack() b.pack() win.mainloop() This code creates a basic GUI with a Label and a Button and places them in a window using the Pack layout manager. The button has no “command” so it has no function when clicked.

Linking the button to a function def clicked(): print("Button clicked!") win = Tk() win.title("My GUI") l = Label(win, text="Hello, world") b = Button(win, text="Click me!", command=clicked) l.pack() b.pack() win.mainloop() Now the clicked function will be called every time the button is clicked If we defined the clicked function below the GUI code, the line creating the Button would throw a NameError We can avoid this problem by writing a class for our GUI

Encapsulating a GUI into an Object class MyGUI: def __init__(self, win): l = Label(win, text="Hello, world!") b = Button(win, text="Click me!", command=self.clicked) l.pack() b.pack() def clicked(self): print(“Button clicked!”) The __init__ method takes in a Tk window, creates GUI widgets, and packs them Command is now self.clicked because clicked is a method in the class

Encapsulating a GUI into an Object def main(args): win = Tk() win.title("My GUI") MyGUI(win) win.mainloop() if __name__ == '__main__': import sys main(sys.argv) In the main method, we create a Tk window, give it a title, and pass it as the win argument of a new GUI object

Don’t use grid and pack in the same container! GUI layout managers Pack Positions widgets relative to each other Simple to use Limited layout possibilities compared to grid Grid Allows you to place widgets in rows and columns More complicated than pack but good for GUIs where a particular arrangement is desired Don’t use grid and pack in the same container!

Exercise: Adder Create a GUI with two NORMAL entries and one READONLY entry There should a button that, when clicked, adds the numbers entered in the first two entries and displays the sum in the third entry If the calculation can’t be performed due to invalid input, display a message box with an error message.

Next time... Frames Radiobuttons, StringVars and IntVars Multiple windows! Making the GUI pretty

Frames Container that can be placed within a window Holds other widgets Each frame has its own grid layout, independent of the main window You can use a different layout manager for each container

Example: Frames (use within GUI class) f1 = Frame(win, height=100, width=100, bd=1, relief=SUNKEN) f2 = Frame(win, height=100, width=100, bd=1, relief=SUNKEN) f1.grid(row=0, column=0, sticky=EW) f2.grid(row=0, column=1, sticky=EW) # self.l1 will be used for another method later self.l1 = Label(f1, text="One", height=5, width=5) self.l1.grid(row=0, column=0) Label(f1, text="Two", height=5, width=5).grid(row=0, column=1) Label(f2, text="Three", height=5, width=5).pack(side=LEFT) Label(f2, text="Four", height=5, width=5).pack(side=LEFT) bd=1 sets the border width to 1 so the border is visible, relief=SUNKEN gives the frame’s border a sunken appearance

Radiobuttons Allows the user to choose from one or more options Can be linked to other Radiobuttons with StringVars and IntVars If configured correctly, only one Radiobuttons from a group can be selected at at time

StringVars and IntVars Two variable classes included in Tkinter Have .set() method to set their value e.g. my_string_var.set(“Hello”) StringVars have string value, IntVars have integer value Have .get() method to get their value e.g. val = my_string_var.get() Can be passed in as “variable” argument when creating a Radiobutton, or “textvariable” argument when creating an Entry

Exercise Revisit our Adder example from the last class Instead of using Entry.insert and Entry.delete, use a StringVar to change the text of the result Entry See what happens if you don’t set the Entry state to “normal” before changing the text!

Adding Radiobuttons to our Frame example self.color = StringVar() rb1 = Radiobutton(win, text="Red", variable=self.color, value="red") rb2 = Radiobutton(win, text="Blue", variable=self.color, value="blue") rb3 = Radiobutton(win, text="Green", variable=self.color, value="green") self.color.set("unknown") # deselects all radiobuttons linked to self.color rb1.grid(row=1, column=0) rb2.grid(row=2, column=0) rb3.grid(row=3, column=0) Button(win, text="Change Color", command=self.change_color).grid(row=1, column=1)

Adding Radiobuttons to our Frame example def change_color(self): new_color = self.color.get() if new_color == "unknown": pass else: self.l1.config(bg=new_color)

Additional Exercise Add another set of Radiobuttons to the GUI Separate from those linked to self.change_color There should be four Radiobuttons, each linked to one of the labels Create an IntVar to use as the variable for these Radiobuttons The user’s selection should determine which Label will change color when the button is clicked.

Root Window vs. Toplevels The root window is your main window (Tk) A GUI application should have only one root window Any additional windows should be Toplevels Hide windows with .withdraw() Show windows with .deiconify() Delete windows with .destroy() (they will be gone forever!) If you destroy the root window, all Toplevels will also be destroyed

Adding multiple windows to our example: # in __init__ method, add line: self.root = win def new_window(self): self.root.withdraw() self.new = Toplevel() Label(self.new, width=30, height=10, text="I'm a new window!").pack() Button(self.new, text="Close window", command=self.close_new).pack() def close_new(self): self.new.destroy() # destroy new window self.root.deiconify() # show root window

Changing the GUI appearance GUI windows and widgets have many optional parameters to change their appearance bg: background color fg: foreground color (for Labels, this is the font color) bd: border width border: border type (RAISED, SUNKEN, etc) For color codes: http://htmlcolorcodes.com/ For more options, check the documentation or online tutorials