[Tutor] Tkinter / Variable passing

Michael Lange klappnase at freenet.de
Wed Jul 21 22:28:46 CEST 2004


On Wed, 21 Jul 2004 06:24:17 -0700
"Faulconer, Steven M." <STEVEN.M.FAULCONER at saic.com> wrote:


> 
> This is where the issue lies. How can I tell the CheckerApp instance what
> the user selected in the CheckerWindow instance? I've tried different types
> of variables (global and non). I've even added a function to the CheckerApp
> that I attempt to call from the CheckerWindow, which fails with an
> "AttributeError" Toplevel instance has no attribute 'CheckerAccept'".
> 

In line 252 of your script you create your CheckerWindow instance:

> CheckerWindow( self.newroot, 'Feature Count', command )

where self.newroot is an instance of Tkinter.Toplevel .

Your method "CheckerAccept()" is a method of the CheckerApp class.
So when you call

> self.myparent.CheckerAccept()

and self.myparent is a Tkinter.Toplevel python is right to complain.

There are two possibilities to fix this:

1. make your CheckerApp instance an attribute of the CheckerWindow class, like this:

class CheckerWindow:
    def __init__( self, parent, checkerApp, checker, command ):
        self.checkerApp = checkerApp
        <etc.>

and pass "self" as argument for checkerApp when you create the CheckerWindow from within the CheckerApp:

> CheckerWindow( self.newroot, self, 'Feature Count', command )

Then you could call 

> self.checkerApp.CheckerAccept()

2. make your CheckerApp class a subclass of Tkinter.Toplevel

class CheckerApp(Tkinter.Toplevel):
    <snip>

    def __init__( self, parent, project, database, dbpath ):
        Tkinter.Toplevel.__init__(self, parent)
        # Window Initialization routines
        self.PROJECT = project
        self.DATABASE = database
        self.DBPATH = dbpath
        self.resizable( width = False, height = False )
        self.option_add( "*Font", "Arial 12" )
        self.title( "SAIC Checker Front End" )
        self.focus_set()
        self.Status = 0
        <etc.>

and then pass "self" as parent to the CheckerWindow:

> CheckerWindow( self, 'Feature Count', command )

I hope this helps

Michael





More information about the Tutor mailing list