beginner python GUI question

Chris Hare chare at labr.net
Mon Aug 2 13:46:05 CEST 2010


On Aug 1, 2010, at 8:33 PM, rechardchen wrote:

> 于 2010-8-2 6:15, Chris Hare 写道:
>> I hope I can explain this correctly.
>> 
>> I have a GUI, which is already being processed by a mainloop.  I want to be able to open a second window so the user can interact with specific information in the second window.  I pulled together this code example
>> 
>> from Tkinter import *
>> 
>> class Net:
>> 	def __init__(self,tkWin):
>> 		self.window = tkWin
>> 	def show(self,t):		
>> 		self.l = Label(self.window,text=t)
>> 		self.l.grid()
>>                 button = Button(self.window, text="Again")
>> 		button.bind("<Button-1>", self.Again)
>> 		button.grid()
>> 	def Again(self,event):
>> 		win3 = Tk()
>> 		x = Net(win3)
>> 		x.show("window 3")
>> 
>> root = Tk()
>> root.title = "test"
>> f = Frame(root,bg="Yellow")
>> l = Label(f,text="window 1")
>> f.grid()
>> l.grid()
>> 
>> win2 = Tk()
>> x = Net(win2)
>> x.show("window 2")
>> if __name__ == "__main__":
>> 	root.mainloop()
>> 
>> Is this the right way to do things, or would you suggest something different?
>> 
>> Thanks,
>> Chris
>> 
> Using Tkinter.Toplevel may be better. :)
> -- 
> http://mail.python.org/mailman/listinfo/python-list

I thought that would be a good idea, so I changed the code a bit to this:

from Tkinter import *

class Net:
	def __init__(self):
		self.window = Toplevel()
	def show(self,t):		
		self.l = Label(self.window,text=t)
		self.l.grid()
                button = Button(self.window, text="Again")
		button.bind("<Button-1>", self.Again)
                button2 = Button(self.window, text="Dismiss")
		button2.bind("<Button-1>", self.window.destroy)
		button.grid()
		button2.grid()
	def Again(self,event):
		x = Net()
		x.show("window 3")

root = Tk()
root.title = "test"
f = Frame(root,bg="Yellow")
l = Label(f,text="window 1")
f.grid()
l.grid()

x = Net()
x.show("window 2")
if __name__ == "__main__":
	root.mainloop()

I put the call to Topevel into the Class.  This however, gets me an error message

Traceback (most recent call last):
  File "a.py", line 27, in <module>
    x = Net()
  File "a.py", line 5, in __init__
    self.window = Toplevel()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 1978, in __init__
    self.title(root.title())
TypeError: 'str' object is not callable

I should think it would work, but I don't understand why it doesn't.

Thanks




More information about the Python-list mailing list