return from class?

Matthew Dixon Cowles matt at mondoinfo.com
Sun Jun 10 14:01:30 EDT 2001


On Sun, 10 Jun 2001 12:21:38 +0000 (UTC), Jacek Pop³awski
<jp at ulgo.koti.com.pl> wrote:

>I will try to explain this better...  I need to select
>"corporation". I need it in many places in my application, so I
>decided to create class SelectCorporation. This class should open
>window with Listbox. User should select one item from that Listbox,
>then click "OK".  I don't know what to do when OK is clicked. I
>should destroy window, and class instance, but how to return value to
>the class which called SelectCorporation?

Dear Jacek,
Thanks for working to make your question as clear as possible. In fact
it was very clear from the start. I remember having the same problem
myself.

I think that the easiest way to solve it is to look at the problem the
other way round. That is, instead of having an entry that is trying to
pull a value from your new window, let the new window push the value
into the entry. The only trick is to give your sub-window a reference
to the entry that it will want to put the value in. I'll append an
example of what I mean.

Best regards,
Matt


from Tkinter import *

class mainWin:

  def __init__(self,root):
    self.e=Entry(root)
    self.e.pack()
    b=Button(root,text="Sub-window",command=self.newWin)
    b.pack()
    return None

  def newWin(self):
    subWin(self.e)
    return None

class subWin:
  def __init__(self,mainWinEntry):
    self.mainWinEntry=mainWinEntry
    self.t=Toplevel()
    b=Button(self.t,text="Push",command=self.putTextInEntry)
    b.pack()
    return None

  def putTextInEntry(self):
    self.mainWinEntry.insert(END,"Wibble")
    self.t.destroy()
    return None

def main():
  root=Tk()
  mainWin(root)
  root.mainloop()

if __name__=="__main__":
  main()



More information about the Python-list mailing list