[Tkinter-discuss] First Tkinter program

Jeff Epler jepler at unpythonic.net
Thu Jun 16 19:12:08 CEST 2005


On Thu, Jun 16, 2005 at 09:45:29AM -0400, William O'Higgins wrote:
> None of the examples seem to code in a straight line, and I'm having 
> trouble following the progression of the "simple" examples.  Can Tkinter
> programs been developed in a flat structure, without the class within
> class stuff that's hindering my understanding?  Thanks.

Sure they can.  I didn't read your original post, but here's a
straight-line Tkinter program that creates a listbox with a scrollbar. When the
selected item changes, the 'changed' function is executed, and prints (to the
console) some information about the selection.  Of course, this example *does*
use classes, since Tkinter.Tk, Tkinter.Scrollbar, and friends are all classes.

Jeff

from Tkinter import *

t = Tk()
s = Scrollbar(t)
l = Listbox(t, yscrollcommand=s.set)
s.configure(command=l.yview)

l.pack(side=LEFT, fill=BOTH, expand=YES)
s.pack(side=LEFT, fill=Y)

for i in range(1, 101):
    l.insert(END, "Item %d" % i)

def changed(event):
    index = event.widget.curselection()
    if isinstance(index, (list, tuple)): index = index[0]
    print "Item index:", index
    print "Item value:",  event.widget.get(index)
l.bind("<<ListboxSelect>>", changed)

t.mainloop()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20050616/f0239f92/attachment.pgp


More information about the Tkinter-discuss mailing list