This Week GUIs - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

This Week GUIs

Description:

Pack widgets into the container: button1 = Button(container1, text='Enter') button1.pack ... to pack the widget into. Then other features. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 16
Provided by: annabre
Category:
Tags: guis | week | widget

less

Transcript and Presenter's Notes

Title: This Week GUIs


1
This Week -- GUIs
  • Graphical User Interfaces
  • windows
  • widgets
  • containers
  • events
  • Tkinter (Tool kit interface)
  • Tk is a common GUI toolkit
  • Tkinter is the python module that interfaces with
    Tk

2
What is a GUI?
  • A window consists of
  • containers
  • Frames
  • widgets
  • Buttons
  • Entry (text fields)
  • Menus
  • Labels

Window
Labels
Buttons
Frame (all but title bar)
3
Basic GUI Programming Steps
  • How do you want the GUI to look?
  • What do you want the GUI to do?
  • Bind 1. and 2. together.
  • Write code to wait for user input.

4
How do you want the GUI to look?
  • Import Tkinter
  • from Tkinter import
  • Create a window
  • window Tk()
  • Pack a container into it
  • container1 Frame(window)
  • container1.pack()
  • Pack widgets into the container
  • button1 Button(container1, textEnter)
  • button1.pack()
  • button2 Button(container1, textQuit)
  • button2.pack()

Pack puts the container into the window and makes
it visible.
5
What do you want the GUI to do?
  • React to user events
  • Button clicks
  • Text Entry
  • Checkbox Selections
  • Menu Selections
  • Write event handlers (functions) to react to
    the user input.
  • Example Enter Button
  • def enter()
  • print hello

6
Binding What with How
  • Example Enter Button
  • def enter()
  • print hello
  • button1 Button(container1, textEnter)
  • Need to bind button1 with the function enter().
  • button1 Button(container1, textEnter, \
    commandenter)
  • Note The command feature does not allow
    arguments.

7
Using Functions With Arguments
  • Example Quit Button
  • def quit(w)
  • w.destroy()
  • button2 Button(container1, textQuit,\
    command??)
  • Use a python keyword lambda
  • button_quit lambdaquit(window)
  • button2 Button(container1, textQuit,\
  • commandbutton_quit)

close the window w
8
More on Lambda
  • Example
  • gtgtgt def f(x,y)
  • gtgtgt return xy
  • gtgtgt create a function variable g() to
  • gtgtgt represent the function f(x,y)
  • gtgtgt g lambda f(a,b)
  • gtgtgt define a and b to use g()
  • gtgtgt a 10
  • gtgtgt b 5
  • gtgtgt g()
  • 15

9
Another Example
  • Want to add a Label to our container.
  • welcome Label(container1,\
  • textWelcome to my window.)
  • welcome.pack()
  • Lets add another Label and a (text) Entry.
  • name Label(container1, \
  • textWhat is your name?)
  • name.pack()
  • name_entry Entry(container1)
  • name_entry.pack()

10
Example Continued
  • Want to be able to change the text in
  • welcome Label(container1, \
  • textWelcome to my window.)
  • Make the text a variable!
  • message StringVar()
  • message.set("Welcome to my window.")
  • welcome Label(frame, textvariablemessage)

When we click Enter wed like to see
textvariable means the text can be altered --
only use with a variable.
11
Putting it all together
  • def enter(var, input)
  • var.set(Welcome input.get())
  • message StringVar()
  • message.set("Welcome to my window.")
  • welcome Label(frame, textvariablemessage)
  • welcome.pack()
  • name Label(container1, textWhat is your
    name?)
  • name.pack()
  • name_entry Entry(container1)
  • name_entry.pack()
  • button_enter lambdaenter(message, name_entry)
  • button1 Button(container1, textEnter, \
  • commandbutton_enter)
  • button1.pack()

12
Opening our Window
  • We run the window and transfer control to the
    user by typing
  • window.mainloop()
  • at the end.

13
Tkinter Mutable Type Variables
  • Immutable types in Python (cant be changed)
  • string long bool int
  • Tkinter mutable equivalent types (can be
    changed)
  • StringVar() DoubleVar() BooleanVar() IntVar()
  • Example
  • mynum IntVar()
  • mynum.get()
  • mynum.set(10)

14
This Week
  • Checkbuttons
  • Dropdown menus
  • grid vs. pack
  • Simple Notepad implementation

15
Checkbuttons
Write a Comment
User Comments (0)
About PowerShow.com