Re: Access from one class to methode of other class
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Access from one class to methode of other class

From: VK <@example.invalid>
Date: Thu May 26 2005 - 22:49:42 CEST

Kent Johnson wrote:
> VK wrote:
>
>>> On Thu, 26 May 2005 14:33:45 +0200, VK <"myname"@example.invalid>
>>> declaimed the following in comp.lang.python:
>>>
>>>
>>>> Hi, all!
>>>>
>>>> In my programm i have to insert a variable from class 2 to class 1
>>>> and I get error NameError: global name 'd' is not defined. How do I
>>>> get access to d.entry.insert() method of class 1
>
>
>>
>> from Tkinter import *
>>
>> class First:
>> def __init__(self):
>> self.root = Tk() # create window contents as children to
>> root..
>> self.entryframe = Frame(self.root)
>> self.entryframe.pack(fill=BOTH,expand=1)
>>
>> self.entry = Entry(self.entryframe)
>> self.entry.pack(side=TOP,expand=1,fill=BOTH)
>> self.entry.focus()
>> self.entry.bind('<Return>',(lambda event: self.fetch())) #
>> on enter key
>>
>> self.button =
>> Button(self.entryframe,text="Call",command=self.getvar)
>> self.button.pack(side=LEFT,expand=YES,fill=BOTH)
>>
>> self.root.mainloop()
>>
>> def fetch(self):
>> print 'Input => "%s"' % self.entry.get() # get text form
>> entry
>>
>> def getvar(self,event=0):
>> c=Second(self,self.root)
>>
>>
>>
>> class Second:
>> def __init__(self,parent,s="thing"):
>> self.root = Tk()
>> self.ent = Entry(self.root)
>> self.ent.pack()
>> self.btn = Button(self.root,text='Fetch',command=self.fetch)
>> self.btn.pack(side=RIGHT)
>> def fetch(self):
>> text = self.ent.get() # get text form entry in this window
>> d.entry.insert(0, text)# must insert in other window
>>
>> d = First() #First window
>
>
> The problem is that First.__init__() never returns so the instance of
> First is never bound to d. Take the line
> self.root.mainloop()
> out of First.__init__() and and the line
> d.root.mainloop()
>
> at the end of the program and it will work as you expect.
>
> Kent

O, God! It works! Thousend of thanks!

By the way, the second window appears not activ, is there an option to
make it activ on start?

Reg,VK
Received on Thu Sep 29 16:14:33 2005