This document discusses Tkinter, a GUI toolkit for Python. It provides examples of basic Tkinter code for common widgets like buttons, labels, entries and more. It also covers Tkinter concepts like packing, grids, styling with themes, and events. The document seeks to demonstrate that Tkinter is simple to use yet robust, with a rich set of widgets and capabilities.
1. it’s simpleto use
- rich base widget set
- which is not verbose
- automatic packing
Saturday, 7 November 2009
16.
2. it’s alwaysthere
on Windows & OS X
-- on Linuxes you might
need to install an
optional package
Saturday, 7 November 2009
17.
3. it’s mature
first release 1991
8.0 released August 1999
8.5 released December 2007
Saturday, 7 November 2009
18.
how I discoveredthis
I had this project that I wanted to be click-to-run, but needed to select a file
Looked for a cross-platform “file dialog” solution and found some hacks
Then I noticed a reference to Tkinter, and lo and behold...
Saturday, 7 November 2009
import Tkinter astk
root = tk.Tk()
tk.Label(root, text="Hello, world").pack()
root.mainloop()
No point showing the
other three lines in other
examples
Saturday, 7 November 2009
38.
from PIL importImage, ImageTk
image = ImageTk.PhotoImage(Image.open('kitten.jpg'))
tk.Label(root, image=image).pack()
Saturday, 7 November 2009
listbox = tk.Listbox(root)
listbox.pack()
listbox.insert(tk.END, "a list entry")
for item in ‘one two three four’.split():
listbox.insert(tk.END, item)
Saturday, 7 November 2009
45.
text = tk.Text(root)
text.pack()
text.insert(tk.END, '''some text
more text''')
also allows:
- embedding of images
and widgets
- searching
- tagging (identification,
useful for properties)
- styles
Saturday, 7 November 2009
tk.Button(root, text='Press me!').grid(row=0,column=0)
tk.Message(root, text='Pressnmentoo!').grid(row=1, column=0)
tk.Button(root, text='And me!').grid(row=1, column=1)
those are tk.CENTER “sticky”
other options are the 9 points of the compass
pack() also allows the sticky side to be specified
see I snuck in the multiline “Message” widget
there?
Saturday, 7 November 2009
widgets with avariable
variable = tk.IntVar()
value = variable.get()
variable.set(value)
Saturday, 7 November 2009
64.
text widgets
entry = tk.Entry()
value = entry.get()
entry.delete(0, tk.END)
entry.insert(0, value)
Saturday, 7 November 2009
65.
list boxes
listbox = tk.Listbox(root)
listbox.pack()
for item in ‘one two three four’.split():
listbox.insert(tk.END, item)
selected = list.curselection()
Saturday, 7 November 2009
tk.Button(root, text="Hello, world!",
background='black',
foreground='white',
font='Courier').pack()
some limitations
(sometimes depending on
your theme)
Saturday, 7 November 2009
from [tT]kinter importttk
lower-case “t” in py3k+
If you want it earlier you
can install the tile
extension
Saturday, 7 November 2009
72.
complementary
Though the themed widget commands work similarly to the originals,
there are important differences.
Themed widgets are not a drop-in replacement.
In particular, themed widgets generally provide less options for
customizing their appearance than regular Tk widgets (e.g. they will
often not have options like "-background").
Such changes, if needed (and they should be needed much less often)
must be done by defining new widget styles, using the facilities offered
by the themed widget package.
Saturday, 7 November 2009
keyboard event types
focus <FocusIn>, <FocusOut>
<Enter>, <Delete>, <Left>,
specific keys <Right>, ...
any key <Key>
any text a, b, 1, 2, ...
<Shift-Up>, <Alt-Enter>,
modified keys <Control-Tab>, ...
Saturday, 7 November 2009
87.
special event types
configuration <Configure>
The widget changed size (or location, on some
platforms). The new size is provided in the width
and height attributes of the event object passed to
the callback.
Saturday, 7 November 2009
88.
Canvas
w = tk.Canvas(root, width=400, height=300)
w.pack()
Saturday, 7 November 2009