8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 1/16
Adapted from Richard P. Muller 1
Python GUIs (Graphical User Interfaces)Python GUIs (Graphical User Interfaces)
• Tkinter
– Python's de-facto standard GUI
– this is the only GUI we will cover
• WxWindows– supports Windows and MacOS; on Unix variants, it supports
both GTk+ and Motif toolkits
• Qt toolkit (PyQt), for KDE (PyKDE)
– Note: QT designer: powerful graphical GUI builder
• Gtk+ (used by Gnome)
• PyOpenGL: 3D applications
• ... and some others
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 2/16
Adapted from Richard P. Muller 2
Tk OverviewTk Overview
• Set of widgets designed by John K. Ousterhout, 1987• Tk == Tool Kit
• Mean to be driven by Tcl (Toolkit Control Language)
– Many people find Tcl limited
– Can also drive Tk with Perl, Python
• Tkinter is the Python Tk Interface
– Very easy to use
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 3/16
Adapted from Richard P. Muller 3
Hello, WorldHello, World
from Tkinter import *
w=Label(text="Hello, World!")
w.pack()
w.mainloop()
• Label() defines a label to be displayed
– text= specifies a parameter to be passed in
• pack() resizes the window to the proper size
• mainloop() enters the event loop, and the program idles
until a button is pushed, a menu is pulled, etc. It has to
idle until the program is killed, since we didn't define anyevents.
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 4/16
Adapted from Richard P. Muller 4
Events (Hello, Goodbye)Events (Hello, Goodbye)
from Tkinter import *
w=Label(text="Hello, World").pack()
b=Button(text="Goodbye",command='exit').pack()
mainloop()
• Button label defined by text parameter
• Button defines a callback function, something to run
when it is pushed.
• Now mainloop() has an event to catch, so when we push
the button, mainloop() executes the exit command.
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 5/16
Adapted from Richard P. Muller 5
WidgetsWidgets
• Button
– A simple button, used to execute a command or other operation.
• Canvas
– Structured graphics. This widget can be used to draw graphs and
plots, create graphics editors, and to implement custom widgets.
• Checkbutton
– Represents a variable that can have two distinct values. Clicking
the button toggles between the values.
• Entry– A text entry field.
• Frame
– A container widget. The frame can have a border and a
background, and is used to group other widgets when creating ana lication or dialo la out.ars all other radiobuttons associated
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 6/16
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 7/16
Adapted from Richard P. Muller 7
Widgets (cont.)Widgets (cont.)
• Radiobutton
– Represents one value of a variable that can have one of many
values. Clicking the button sets the variable to that value, and
clears all other radiobuttons associated with the same variable.
• Scale
– Allows you to set a numerical value by dragging a "slider".
• Scrollbar
– Standard scrollbars for use with canvas, entry, listbox, and text
widgets.
• Text
– Formatted text display. Allows you to display and edit text with
various styles and attributes. Also supports embedded images
and windows.
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 8/16
Adapted from Richard P. Muller 8
Typical widgetsTypical widgets
Menu barLabel
Text area (for geometry input)
Text entry
Radio buttons
Checkbox
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 9/16
Adapted from Richard P. Muller 9
Dialog boxesDialog boxes
• Convenient way to get feedback from a user
– Confirm quit
– Inputs data directly into program
– Here 0 is returned for Yes, and 1 is returned for No
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 10/16
Adapted from Richard P. Muller 10
Simple Dialog Box ExampleSimple Dialog Box Example
import sys
from Tkinter import *
from Dialog import *
def confirm_quit():
d = Dialog(None, title="Goodbye?",
text="Really Leave?", default=0,
bitmap=DIALOG_ICON, strings=("Yes","No"))
if d.num ==0:sys.exit()
return
l = Label(text="Hello, World!").pack()
b = Button(text="Goodbye",
command=confirm_quit).pack()
mainloop()
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 11/16
Adapted from Richard P. Muller 11
File Browser DialogFile Browser Dialog
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 12/16
Adapted from Richard P. Muller 12
File Dialog Example CodeFile Dialog Example Code
From Tkinter import *
from tkFileDialog import *
root = Tk()
– Set up the dialog box
filename=askopenfilename()
print filename
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 13/16
Adapted from Richard P. Muller 13
Menu Example CodeMenu Example Code
from Tkinter import *
root = Tk()
menubar = Menu(root)
root.config(menu=menubar)
filemenu = Menu(menubar)
menubar.add_cascade(label='File',menu=filemenu)
def doPrint(): print 'doPrint'
filemenu.add_command(label='Print',
command=doPrint)
root.mainloop()
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 14/16
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 15/16
Adapted from Richard P. Muller 15
Exercises (II)Exercises (II)
• Numeric and Plotting:
– Create several functions, plot them and their FFT:
periodic functions
gaussians
step
– Add noise
– Do the same with two-dimensional functions
8/12/2019 Friedrich Python 7
http://slidepdf.com/reader/full/friedrich-python-7 16/16
Adapted from Richard P. Muller 16
Exercises (III)Exercises (III)
• Astronomical application: Boltzmann plots
– Read in measured line intensities and transition parameters
– Calculate: Nupper/g = 1.66983E17*Tdv/freq/mu2S
– Plot log(Nupper/g) versus upper Energy
– Do a linear fit to get the temperature and column density
– Add a small GUI
Top Related