Graphical User Interface (GUI) Programming Joan Boone - - PowerPoint PPT Presentation

graphical user interface gui programming
SMART_READER_LITE
LIVE PREVIEW

Graphical User Interface (GUI) Programming Joan Boone - - PowerPoint PPT Presentation

INLS 560 Programming for Information Professionals Graphical User Interface (GUI) Programming Joan Boone jpboone@email.unc.edu Summer 2020 Slide 1 Topics Part 1 Graphical User Interface overview Example: Frame demo Part 2


slide-1
SLIDE 1

Slide 1

Graphical User Interface (GUI) Programming

Joan Boone

jpboone@email.unc.edu

Summer 2020

INLS 560

Programming for Information Professionals

slide-2
SLIDE 2

Slide 2

Topics

Part 1

  • Graphical User Interface overview
  • Example: Frame demo

Part 2

  • Example: Kilometers to Miles Converter

Part 3

  • Color, fonts, padding
  • Radio buttons
  • Layout management
slide-3
SLIDE 3

Slide 3

Various Python User Interfaces

Command line interface PyCharm Run Window Python tkinter

slide-4
SLIDE 4

Slide 4

Graphical User Interfaces (GUIs)

  • Interfaces that allow users to interact with applications

using graphical elements (widgets) such as buttons, text fields, and menus.

  • Many similarities between GUI elements and their

behavior for an application program, and the appearance and behavior of a web page

– GUI design and web design both require widgets, layout

management, and event handling

– Both are developed using languages

  • GUI: programming languages (Python, Java, C, etc.)
  • Web: HTML, CSS, JavaScript
slide-5
SLIDE 5

Slide 5

General GUI Concepts

Widgets

  • All graphical elements in an application window are widgets
  • All widgets are part of a window hierarchy.
  • Most are considered 'user controls'

– button, checkbutton, radiobutton, listbox – label, entry, text, scale, scrollbar – frame, menu, menubutton

Layout (or geometry) management

  • Determines where to place widgets in a window or frame

Event handling

  • Respond to user actions (button click, keystrokes) associated

with a widget

  • Define a callback function to handle the event
slide-6
SLIDE 6

Slide 6

GUI Frameworks and Toolkits

  • Generally, all languages have built-in support for GUIs
  • There are many 3rd party frameworks and toolkits, e.g.,

GUI Programming in Python, but there is much variation in platform support and maintenance.

  • We'll be using the tkinter module because it is included with

the Python interpreter, and it's a de facto standard.

– tkinter (“Tk interface”) is a standard Python interface to the

Tk GUI Toolkit, a cross-platform widget toolkit

  • References

– Python documentation: tkinter – Python interface to Tcl/Tk – TkDocs has a useful tutorial with good widget descriptions – Python Course: Tkinter Tutorial

slide-7
SLIDE 7

Slide 7

Frequently Used Widgets

Frame Button Label Entry

Source: TkDocs

Frames are containers for other widgets that organize and simplify layout Labels show static text (or images), usually to identify another element

  • r display results

Buttons are 'controls' that allow users to perform some action, for example, calculate a result, save a file, or quit the application Entry widget allows the user to enter a single line of input as text

slide-8
SLIDE 8

Slide 8

A few more Widgets

Radiobutton

Source: TkDocs

Checkbutton Combobox Text

Radiobuttons allow

  • ne selection among

several mutually exclusive options Checkbuttons allow multiple selections among mutually exclusive options Combobox allows one selection from a list of options Text widget supports the input and display of multiple lines of text

slide-9
SLIDE 9

Slide 9

Example: Frame Demo

frame_demo.py

slide-10
SLIDE 10

Slide 10

Frame Demo

import tkinter class MyGUI: def __init__(self):

# Create the main window widget. self.main_window = tkinter.Tk() # Create two frames, one for the top of the window, and one for the bottom. # Both frames are contained in the self.main_window self.top_frame = tkinter.Frame(self.main_window) self.bottom_frame = tkinter.Frame(self.main_window) # Create three Label widgets that are contained in the top frame. self.label1 = tkinter.Label(self.top_frame, text='Winken') self.label2 = tkinter.Label(self.top_frame, text='Blinken') self.label3 = tkinter.Label(self.top_frame, text='Nod') # Packing tells tkinter how to layout the widgets. # If packing is not specified, the widgets are not displayed # Pack the labels that are in the top frame. # Use the side='top' argument to stack vertically self.label1.pack(side='top') self.label2.pack(side='top') self.label3.pack(side='top') # Create three Label widgets for the bottom frame. self.label4 = tkinter.Label(self.bottom_frame, text='Winken') self.label5 = tkinter.Label(self.bottom_frame, text='Blinken') self.label6 = tkinter.Label(self.bottom_frame, text='Nod') # Pack the labels that are in the bottom frame. # Use the side='left' argument to arrange horizontally . self.label4.pack(side='left') self.label5.pack(side='left') self.label6.pack(side='left') # Yes, we have to pack the frames too! self.top_frame.pack() self.bottom_frame.pack() # Enter the tkinter main loop. tkinter.mainloop() # Create an instance (object) of the MyGUI class.

my_gui = MyGUI()

frame_demo.py self is a variable that refers to the object class defines the attributes and methods

for an object. Every class has an init method that initializes the object.

Creates an object from the MyGUI class definition. When MyGUI() is called, the init method for the class is executed to create the object

slide-11
SLIDE 11

Slide 11

Topics

Part 1

  • Graphical User Interface overview
  • Example: Frame demo

Part 2

  • Example: Kilometers to Miles Converter

Part 3

  • Color, fonts, padding
  • Radio buttons
  • Layout management
slide-12
SLIDE 12

Slide 12

Example: Kilometers to Miles Converter

kilo_converter.py kilo_converter2.py

Better version: uses a Label widget to display the result in the window Initial version: uses a separate message box to display the result

slide-13
SLIDE 13

Slide 13

Kilometers to Miles Converter

kilo_converter2.py main_window top_frame mid_frame bottom_frame prompt_label kilo_entry descr_label miles_label calc_button quit_button

slide-14
SLIDE 14

Slide 14

Kms to Miles Converter: pseudocode

import tkinter class KiloConverterGUI: def __init__(self): # Create the main window. self.main_window = tkinter.Tk() # Create three frames to group widgets # Create the widgets for the top frame and pack them # Create the widgets for the middle frame and pack them # Create the button widgets for the bottom frame and pack them # Pack the frames. tkinter.mainloop() # Enter the tkinter main loop. # The convert method is called when Convert button is clicked def convert(self): # Convert kilometers to miles # Create an instance of the KiloConverterGUI class. kilo_conv = KiloConverterGUI()

kilo_converter2.py

slide-15
SLIDE 15

Slide 15

Create frames and widgets, then pack them

kilo_converter2.py # Create 3 frames to group widgets. self.top_frame = tkinter.Frame() self.mid_frame = tkinter.Frame() self.bottom_frame = tkinter.Frame() # Create the widgets and add to the top frame. self.prompt_label = tkinter.Label(self.top_frame, text='Enter a distance in kilometers:') self.kilo_entry = tkinter.Entry(self.top_frame, width=10) # Pack the top frame's widgets. self.prompt_label.pack(side='left') self.kilo_entry.pack(side='left') The pack method determines where a widget should be positioned and makes the widget visible when the window is displayed.

slide-16
SLIDE 16

Slide 16

Create widgets for the middle frame that displays the result

kilo_converter2.py # Create the widgets for the middle frame. self.descr_label = tkinter.Label(self.mid_frame, text='Converted to miles:') # Create a StringVar object to associate with an output label. self.value = tkinter.StringVar() # Create a label and associate it with the StringVar object. # Any value stored in the StringVar object will automatically # be displayed in the label. self.miles_label = tkinter.Label(self.mid_frame, textvariable=self.value) # Pack the middle frame's widgets. self.descr_label.pack(side='left') self.miles_label.pack(side='left') The miles_label is initially an empty placeholder label to be filled in with the converted value that will be assigned to self.value. descr_label miles_label

slide-17
SLIDE 17

Slide 17

Create buttons for the bottom frame

kilo_converter2.py # Create the button widgets for the bottom frame. self.convert_button = tkinter.Button(self.bottom_frame, text='Convert', command=self.convert) self.quit_button = tkinter.Button(self.bottom_frame, text='Quit', command=self.main_window.destroy) # Pack the buttons. self.convert_button.pack(side='left') self.quit_button.pack(side='left') def convert(self): # Get the kilometer value entered # in the kilo_entry widget. kilo = float(self.kilo_entry.get()) # Convert kilometers to miles. miles = format(kilo * 0.6214, '.2f') # Store miles in StringVar object. # This automatically updates # the miles_label widget. self.value.set(miles) When the Convert button is clicked, the convert function is called

slide-18
SLIDE 18

Slide 18

Setting Window Title and Size

Replace the default window title ('tk') self.main_window.title("Kilometer Converter") Modify the default window dimensions self.main_window.geometry('400x150')

slide-19
SLIDE 19

Slide 19

Exercise: Add Miles to Kms Conversion

Update kilo_converter2.py to also convert from miles to kilometers

  • Add another button to handle the additional conversion
  • Add a new function to convert miles to kilometers (kms = miles / 0.6214)
  • Replace the label to include a description of the conversion type

Initial display Miles to Kms Kms to Miles

slide-20
SLIDE 20

Slide 20

Topics

Part 1

  • Graphical User Interface overview
  • Example: Frame demo

Part 2

  • Example: Kilometers to Miles Converter

Part 3

  • Color, fonts, padding
  • Radio buttons
  • Layout management
slide-21
SLIDE 21

Slide 21

Adding Color, Fonts and Padding

Specifying colors, fonts, and padding is similar to how this is done in HTML and CSS for web development.

  • Colors can be specified by name or RGB/hex value

HTML color names can be used, and there is also a list of Tk colors

  • Fonts are defined by the font name (e.g., Arial, Helvetica, Georgia)

And optionally, by size (e.g., 8, 10, 12, 14) and style (normal, italic, bold)

  • Padding adds extra space around a widget

padx adds space on the left and right, pady adds space above and below

slide-22
SLIDE 22

Slide 22

Adding Color, Fonts and Padding

Set the background color of the main window: self.main_window.configure(background='lightblue') Add some padding around the buttons when you pack them: self.km2mi_button.pack(side='left', padx=10, pady=10) Set the font for the prompt label when you create the Label: self.prompt_label = Tkinter.Label(self.top_frame,

text='Enter a distance:', font='Verdana 12')

slide-23
SLIDE 23

Slide 23

Buttons Radio Buttons

  • Radio buttons allow for the selection of one of several options
  • A function can be associated with each button, so that when the

button clicked, the function is called

  • Each group of radio buttons is associated with the same variable.

Clicking a button changes the value of this variable to a predefined value associated with each RadioButton widget

slide-24
SLIDE 24

Slide 24

self.radio_var = tkinter.IntVar() self.km2mi_radio = tkinter.Radiobutton(self.mid_frame, text='Kms to Miles', variable=self.radio_var, value=1, command=self.convert_km2mi) self.mi2km_radio = tkinter.Radiobutton(self.mid_frame, text='Miles to Kms', variable=self.radio_var, value=2, command=self.convert_mi2km) km2mi_converter_radio.py

Radio Buttons

Variable associated with all buttons Unique value associated with a button

slide-25
SLIDE 25

Slide 25

Exercise: Handling invalid input

Valid numeric input Invalid numeric input

slide-26
SLIDE 26

Slide 26

Layout (or Geometry) Management

Problem: Different widgets have different sizes and you want them placed in an organized and visually appealing way in the window

  • All widgets have a 'natural' (or default) size, and the layout manager uses a

complex algorithm to determine how to make everything fit within the window constraints

Tkinter has 3 layout managers

  • Pack: easy, but limited. Allows you to specify location and padding relative to
  • ther widgets within a container.
  • Place: explicitly set the position and size of a window in either absolute or

relative terms. Have complete control, but more complex.

  • Grid: places widgets in a 2-dimensional table. A widget's position is defined

by row and column.

References

  • The Tkinter Grid Geometry Manager
  • Python Course: Tkinter Layout Management
  • TkDocs: Grid Geometry Manager
slide-27
SLIDE 27

Slide 27

Pack vs. Grid Layouts

Pack Layout Manager

  • Uses Frame widget to

group other widgets

  • Uses pack function to

lay out widgets

Grid Layout Manager

  • Uses main_window to

group widgets

  • Widgets are laid out

using row and column attributes

slide-28
SLIDE 28

Slide 28

Grid Layout for km2mi converter

1 1 2 1

self.prompt_label = tkinter.Label(self.main_window, text='Enter a distance:', font='Verdana 12 italic', background=bg_color).grid(row=0, column=0, padx=10, pady=5)

How to specify the grid layout for the 'Enter a distance:' label

km2mi_converter_grid_layout.py