LIS 559 July 21 Week 11 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

LIS 559 July 21 Week 11

Description:

container.grid() # put the frame into the main window and make it visible ... Each widget has a grid() function to place it in the frame in a specific location ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 24
Provided by: publi2
Category:
Tags: lis | frame | july | week

less

Transcript and Presenter's Notes

Title: LIS 559 July 21 Week 11


1
LIS 559 July 21 - Week 11
  • Summer 2006
  • Margaret Kipp
  • mkipp_at_uwo.ca
  • x88687
  • NCB 235

2
Concepts
  • Graphics
  • Python graphics module Tkinter
  • Namespaces
  • Global Variables
  • Local Variables
  • Databases
  • http//publish.uwo.ca/mkipp/teaching/library.mdb

3
Basic GUI Programming Tasks
  • specify how you want the UI to look what the
    user will see
  • decide what the UI will do how the UI will react
    to the user
  • connect how the UI looks to how it works i.e.
    make the widgets react to the user's input (e.g.
    pressing a button)
  • write code that waits for user input

4
Definitions
  • UI
  • User Interface, what the user sees, until now we
    have been writing command line UIs
  • GUI
  • graphical user interface accessed via a mouse or
    keyboard shortcuts, consists of icons, windows,
    entry boxes, buttons, etc.
  • Widget
  • an element of a GUI, can include windows, entry
    boxes, buttons, radio boxes and check boxes (and
    more)
  • Container
  • frame to hold widgets

5
Definitions (cont.)
  • Events
  • input events like mouse clicks or keyboard input
  • Event Handlers
  • a function that responds to events, e.g.
    responding to mouse clicks or keyboard input
  • Bindings
  • connects an event handler to an event associated
    with a specific widget, e.g. binding a single
    click on a close button to a closeWindow function
    that closes the window the button is in
  • Event Loop
  • code that waits for user input

6
Example (without graphics)
  • The following example illustrates the use of an
    event loop, event handlers and binding the event
    handlers to an event (user input) all without
    using Tkinter
  • http//www.ferg.org/thinking_in_tkinter/tt000.py
  • This is very similar to the text based menus we
    have been writing

7
Python Graphics
  • Modules
  • Tkinter is a popular python graphics module
  • Importing
  • from Tkinter import
  • this is a new form of the import statement, using
    this format we do not need to say
    Tkinter.Button() or Tkinter.Label() instead we
    can just say Button() or Label() when we want a
    button or label

8
Main Programme
  • Every Tkinter programme will have at minimum the
    following lines
  • from Tkinter import import the module
  • root Tk() create the main window
  • root.mainloop() display the main window and
    wait for input
  • By itself this code doesn't actually do anything
    useful though, we need to add containers and
    widgets.

9
Widgets and Containers
  • A container holds a widget or widgets.
  • Containers
  • Frame
  • A widget is an element of a GUI like a button or
    checkbox.
  • Widgets
  • Label
  • Button
  • Entry
  • Text
  • Radiobutton
  • Checkbox

10
Containers
  • Frame is the name of the most commonly used
    container in Tkinter
  • from Tkinter import
  • root Tk() create the main window
  • container Frame(root) create a frame and
    associate it with the main window
  • container.grid() put the frame into the main
    window and make it visible
  • root.mainloop() display main window and wait
    for input

11
Containers (cont.)
  • Containers aren't interesting without widgets.
  • Notice that the window has now shrunk down to the
    size of the title bar.
  • This is because the container is empty.
  • If we put widgets in it it will grow again.
  • It is also possible to specify the size and title
    of the window using the following syntax
  • root.geometry("200x100") this is height x width
    and it is a string
  • root.title("Tkinter Test")

12
Widgets
  • Widgets are objects that can be placed in a
    container (e.g. buttons)
  • Each widget has attributes such as the displayed
    text, size, border, background, etc
  • Each widget has a grid() function to place it in
    the frame in a specific location
  • e.g. widget.grid(row0, column1)

row 0 column 0
row 0 column 1
row 1 column 0
row 1 column 1
13
Labels
  • Labels
  • a label is a Tkinter widget
  • labels are used to display text
  • label1 Label(container, text "this is a
    label") create a new label with text and
    associate it with the Frame object container
  • label1.grid() make the label visible
  • It is also possible to set the text later
  • label1"text" "this is a label too"

14
Buttons
  • Buttons
  • buttons are used to accept user input
  • button1 Button(container, text"Submit",
    commanddoSomething)
  • button1.grid()
  • command refers to the function that will be
    called when the user presses the button
    doSomething is a function you must define
  • e.g. def doSomething() pass

15
Example Button Click Counter
  • The following example shows the use of a label
    and a button. The button reacts to user clicks by
    counting the number of times the user has clicked
    it.
  • http//publish.uwo.ca/mkipp/teaching/buttonclick.
    py
  • Note the update_count function that simply
    increments the counter keeping track of the
    clicks and updates the label on the button.

16
Entry
  • Entry
  • entry widgets are used to accept user input, they
    are single line
  • entry1 Entry(container) create a new text
    entry box and associate it with the Frame object
    container
  • entry1.insert(0, "This is an entry box.") use
    END for the end of the entry box
  • entry1.grid() make the entry box
  • userinput entry1.get() gets the value

17
Example Text Entry Field
  • The following example shows the use of a label
    and a button. The button reacts to user clicks by
    counting the number of times the user has clicked
    it.
  • http//publish.uwo.ca/mkipp/teaching/entryfield.p
    y
  • Note the getName function that simply gets the
    value in the Entry field and prints it.

18
Text
  • Text
  • text widgets are used to accept user input, they
    are multiline
  • text1 Text(container) create a new text entry
    box and associate it with the Frame object
    container
  • text1.insert(END, "This is some text.")
  • text1.grid() make the text box visible
  • text1.get(index1, index2) gets all text between
    index1 and index2
  • text1.see() moves to the end

19
Radiobutton
  • Radiobutton
  • radiobuttons are used to accept user input, only
    one can be selected at a time
  • radio1 Radiobutton(container, text"Yearly",
    variablefrequency, value"Yearly",
    commanddoSomething).grid(row3, column1)
  • Like a button, radiobuttons have an associated
    function (doSomething) they also have an
    associated variable which is set when the user
    selects it.

20
Checkbox
  • Checkbox
  • checkboxes are used to accept user input,
    multiple checkboxes can be selected
  • check1 Checkbutton(container, text"Books",
    variablelikesBooks, commanddoSomething)
  • check1.grid(row4, column1)
  • Like a radiobutton have an associated function
    (doSomething) also have an associated boolean
    variable which is set when the user selects it.

21
Some Attributes
  • Widgets have common attributes
  • background"green" (or bg)
  • borderwidth2
  • height10
  • highlightbackground"red"
  • highlightcolor"yellow"
  • highlightthickness3
  • reliefsunken (sunken, flat, raised)
  • width30
  • font(('MS', 'Sans', 'Serif'), '8')
  • and many more...
  • http//www.pythonware.com/library/tkinter/introduc
    tion/

22
Example User Survey
  • The following example demonstrates a very simple
    user survey.
  • http//publish.uwo.ca/mkipp/teaching/usersurvey.p
    y
  • This survey illustrates the use of radiobuttons,
    checkboxes, entry fields and buttons.

23
Example Database Access GUI
  • The following example is a simple graphical
    interface to an MS Access database.
  • http//publish.uwo.ca/mkipp/teaching/queryDB.py
  • Notice the combination of the graphics code and
    the database access code.
Write a Comment
User Comments (0)
About PowerShow.com