Tuesday, July 7, 2009

TkDocs first example in python 3.1

The following code does exactly the same as Mark Roseman's TkDocs A First (Real) Example. To match the other language examples, this code has no structuring. The Python way would obviously be to do this in a more OOP way.

So, here is the code (gist):

#! /usr/bin/env python3

# include tkinter packages
from tkinter import *
from tkinter.ttk import *

# create tk root class
root = Tk()

# create frame, add title and setup grid
frame = Frame(root, padding='3 3 12 12')
frame.master.title('Feet to meters')
frame.grid(column=0, row=0, sticky=NW)
frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1)

# text entry
frame.feet = Entry(frame, width=7)
frame.feet.grid(column=2, row=1, sticky=W)

# calculated meters label
frame.meters = Label(frame, text='')
frame.meters.grid(column=2, row=2, sticky=W)

# calculate callback
def calculate(*e):
text = str(0.3048 * int(frame.feet.get()) *

# calculate button
frame.calc = Button(frame, text='Calculate', command = calculate)
frame.calc.grid(column=3, row=3, sticky=W)

# feet label
frame.flbl = Label(frame, text='feet')
frame.flbl.grid(column=3, row=1, sticky=W)

# is eq label
frame.islbl = Label(frame, text='is equivalent to')
frame.islbl.grid(column=1, row=2, sticky=E)

# meters label
frame.mlbl = Label(frame, text='meters')
frame.mlbl.grid(column=3, row=2, sticky=W)

# distance the widgets a bit
for w in frame.children.values():
w.grid(padx=5, pady=5)

# set focus to feet entry

# bind to calculate
frame.calc.bind("<Return>", calculate)

# enter main loop

No comments:

Post a Comment