[Tutor] Tkinter / Variable passing

Faulconer, Steven M. STEVEN.M.FAULCONER at saic.com
Thu Jul 22 15:52:29 CEST 2004


Michael,

Thank you very much. I liked the method you used in the second option, which
worked quite well. Thanks again for the response.

Steven Faulconer

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Michael Lange
Sent: Wednesday, July 21, 2004 4:29 PM
To: tutor at python.org
Subject: Re: [Tutor] Tkinter / Variable passing


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



_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list