15. 1. it’s simple to use
- rich base widget set
- which is not verbose
- automatic packing
Saturday, 7 November 2009
16. 2. it’s always there
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 discovered this
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
37. import Tkinter as tk
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 import Image, ImageTk
image = ImageTk.PhotoImage(Image.open('kitten.jpg'))
tk.Label(root, image=image).pack()
Saturday, 7 November 2009
44. 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
60. 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
63. widgets with a variable
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
69. tk.Button(root, text="Hello, world!",
background='black',
foreground='white',
font='Courier').pack()
some limitations
(sometimes depending on
your theme)
Saturday, 7 November 2009
71. from [tT]kinter import ttk
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
86. 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