Tkinter-helper-gui and a main class: their communication

Egbert Bouwman egbert at bork.demon.nl
Tue Apr 30 06:41:42 EDT 2002


The experiment below illustrates my problem, but the idea is this:
- a class uses a helper-gui-class for collecting data
- I want to use a helper-class for building the gui in stead of
  just a method that does the job, because the helper-class
  inherits from a super-class
- each time I push a button in the gui it should send a signal 
  to the main class: new data are available, you may come and use them
- the gui is non-modal: it stays on screen eternally, waiting for data

It doesn't work: the main class turtle does not receive any signal,
or at least the method buttonwork() is never used.

Is there a simple solution (which one), or is my whole idea flawed,
and why ?

egbert
--------------------------------------------------------------------
#!/usr/bin/python
import exceptions
from Tkinter import *
root = Tk()
#
class ButtonPush(exceptions.Exception):
    def __init__(self,args=None):
        self.args = args
#
class Amphibian_gui:
    def __init__(self):
        pass
#
class Turtle_gui(Amphibian_gui):
    def __init__(self):
        Amphibian_gui.__init__(self)
        knop=Button(root,text='New Turtle',command=self.pushed)
        knop.grid(row=0,column=0)
    def pushed(event=None):
        print "The button raises the ButtonPush exception"
        raise ButtonPush, "Here comes a new turtle"
#
class Turtle:
    def __init__(self):
        try:
            gui = Turtle_gui()
            print "I am trying ..."
        except Buttonpush, e:
            buttonwork()
    def buttonwork(self):
        print "main class Turtle sees the The ButtonPush exception"
        # do something useful with this knowledge

turtle = Turtle()
root.mainloop()

-- 
Egbert Bouwman - Keizersgracht 197 II - 1016 DS  Amsterdam - 020 6257991
========================================================================





More information about the Python-list mailing list