[Tutor] some tkinter questions

Suzanne Little s349929@student.uq.edu.au
Wed, 22 Dec 1999 11:19:38 +1000 (GMT+1000)


On Tue, 21 Dec 1999, Hans Nowak wrote:

> "It doesn't know what w1 is"... sounds like some variable names are 
> messed up, or declared locally rather than as class attributes, or a
> missing self, or something like that. :) To be more specific, I need 
> to see more code. If it's a lot, you can mail it to me privately, 
> otherwise you can post it to the mailing list so others may shed 
> their light on it too.

I've been working on a mini version/prototype/whatever you want to call it
which just has a listbox and it pops up another window which has
buttons. When you press the print button in the other window it should
print the currently selected element. This is basically what I need in my
'big' application - to be able to get the currently selected element to
perform functions with it. I've attached this trial listbox code to the
end of this message. Unfortunately I am going home on Friday for the next
12 days and won't have any computer access. I'll be starting up again on
the 4th of Jan but until that time I don't think I'll be able to check my
email. Thanks very much for any advice and Merry Christmas everyone.

Suzanne 

#####list.py#####

from Tkinter import *
from listComp import *

class TrialListbox(Frame):
    def __init__(self, parent=None):
	Frame.__init__(self, parent)
	self.pack()
	self.createWidgets()
	self.master.title('A Listbox Trial')
	self.master.iconname('tkpython')

    def createWidgets(self):
	self.makeList()
	self.makeButtons()

    def makeList(self):
	frame = Frame(self)
	frame.pack()
	scrollbar = Scrollbar(frame, orient=VERTICAL)
	self.listbox = Listbox(frame, yscrollcommand=scrollbar.set)
	scrollbar.config(command=self.listbox.yview)
	scrollbar.pack(side=RIGHT, fill=Y)
	self.listbox.pack(side=LEFT, fill=BOTH, expand=1)
	self.listbox.insert(END, 'hello1')
	#etc etc

    def makeButtons(self):
	bframe = Frame(self)
	bframe.pack(side=BOTTOM)
	Button(bframe, text='Call Print', command=self.callPrint).pack(side=BOTTOM)
	Button(bframe, text='Quit', command=self.getOut).pack(side=BOTTOM)

    def callPrint(self):
	#open up the window with the print command button
	w1 = Toplevel()
	w1.window = ListComp(w1)
	w1.window.pack()

    def getOut(self):
	Frame.quit(self)

if __name__ == '__main__': trialListbox().mainloop()

#####listComp.py#####

from Tkinter import *
from list import *

class ListComp(Frame):

    def __init__(self, parent=None):
	Frame.__init__(self, parent)
	self.pack()
	self.createWidgets()

    def createWidgets(self):
	bframe = Frame(self)
	bframe.pack()
	Button(bframe, text='Print', command=self.Iprint).pack(side=LEFT)
	Button(bframe, text='Quit', command=self.Iquit).pack(side=LEFT)

    def Iprint(self):
	#retreive the element currently selected in the listbox and 
	#print it
	item = trialListbox.listbox.curselection()
	member = trialListbox.listbox.get(item)
	print 'Term: ' + member

    def Iquit(self):
	#close this window but not the one with the listbox
	w1.destroy()


-----------------------------------------------------------------------
"Contrariwise," continued Tweedledee, "If it was so, it might be; and if
it were so, it would be; but as it isn't, it ain't.  That's logic"
                             -Lewis Carroll
------------------------------------------------------------------------