Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tinkering with Tkinter

Tinkering with Tkinter

Tkinter - the Python wrapper to the Tk graphics library - has been part of the Python standard library since very early on. However, that inclusion hasn't translated into extensive use.

There was a very good reason for this. Tk's documentation was beyond awful. And if you managed to get over that hurdle, Tkinter apps looked awful - they had a woefully inadequate set of widgets, styled with the very best of mid 1990's open source graphic skill.

And then, the world got obsessed with web frameworks, and the desktop was declared as dead.

However, in the last few years, many of the reasons Tkinter was ignored have been quietly fixed. Tk 8.4 massively improved the visual appearance of Tk. tkdocs.com has emerged, addressing many of the problems with Tk documentation.

In this talk, you'll get a re-introduction to an old friend, and an explanation of why, in a web and mobile world, you should care.

Russell Keith-Magee

July 07, 2013
Tweet

More Decks by Russell Keith-Magee

Other Decks in Technology

Transcript

  1. CameronLaird calls the yearly decision to keep Tkinter "one of

    the minor traditions of the Python world." - Python wiki
  2. Step 2: Init tk from Tkinter import * import ttk

    root = Tk() root.title("Feet to Metres")
  3. Step 3: Main frame mainframe = ttk.Frame(root, padding=(3, 3, 12,

    12)) mainframe.grid( column=0, row=0, sticky=(N, W, E, S) ) root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1)
  4. Step 4: Data Input feet = StringVar() feet_entry = ttk.Entry(mainframe,

    width=7, textvariable=feet) feet_entry.grid( column=1, row=0, sticky=(W, E) )
  5. Step 6: Labels ttk.Label(mainframe, text="feet" ).grid(column=2, row=0, sticky=W) ttk.Label(mainframe, text="is

    equivalent to" ).grid(column=0, row=1, sticky=E) ttk.Label(mainframe, text="metres" ).grid(column=2, row=1, sticky=W)
  6. Step 7: The heavy lifting def calculate(*args): try: value =

    float(feet.get()) metres.set(0.3048 * value) except ValueError: pass
  7. Step 9: Let ‘er rip! for child in mainframe.winfo_children(): child.grid_configure(padx=5,

    pady=5) feet_entry.focus() mainframe.columnconfigure(1, weight=1) root.mainloop()
  8. There’s a lot more • Dialogs • Menus • Lots

    more widgets • Extremely rich text widget • Canvas • Raw TK access if you need it.
  9. Cricket • Early days - v0.1 • Doesn’t support Python

    3 • Only runs Django test suites • Supports pre1.6 and 1.6 discovery • Has a backend API • But - does one thing, very well