[Tkinter-discuss] Multiple windows, accessing variables across them won't work for me (Using Tkinter)

Firat Ozgul ozgulfirat at gmail.com
Mon Aug 23 08:22:27 CEST 2010


Hello,

In Tkinter, when you want to create a secondary window you do not have to
implement a whole new class. What you need to do is just create a new
toplevel. Furthermore, never use "globals" within a class.

I made some modifications to your code. You can find the modified version of
your code in here:

http://paste-it.net/public/hc7b554/

Firat

2010/8/22 Rlain <robert.hedman at mac.com>

>
> Hi!
> I've been working on this app last days, and can't get passed this problem:
> I've created two windows, (one is created by a button click in the first
> window), and from the other window I want to change some variables defined
> in the first window.
>
> I'm guessing either I haven't understood the whole parent thingy, or messed
> up somewhere there.
>
> The source code is:
> (there's some translations and explanations below)
>
>
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
>
> from __future__ import division
> import Tkinter
>
>
> class enhetsomvandlare_tk(Tkinter.Tk):
>    def __init__(self,parent):
>        Tkinter.Tk.__init__(self,parent)
>        self.parent = parent
>        self.initialize()
>
>
>    def initialize(self):
>        self.grid()
>
>        self.LV = Tkinter.StringVar()
>        label = Tkinter.Label(self,textvariable=self.LV,
>                              anchor="w",fg="white",bg="blue")
>        label.grid(column=0,row=0,columnspan=1,sticky='EW')
>        self.LV.set(u"Lbs to Kg")
>
>        global enhet
>        enhet = "lbstokg"
>
>
>        self.entryVariable1 = Tkinter.StringVar()
>        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable1)
>        self.entry.grid(column=0,row=1,sticky='W')
>        self.entry.bind("<Return>", self.OnPressEnter)
>        self.entryVariable1.set(u"")
>
>        self.entryVariable2 = Tkinter.StringVar()
>        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable2)
>        self.entry.grid(column=1,row=1,sticky='W')
>        self.entry.bind("<Return>", self.OnPressEnter)
>        self.entryVariable2.set(u"")
>
>
>
>        raknaknapp = Tkinter.Button(self,text=u"Räkna ut!",
>                                command=self.Onraknaknapp1)
>        raknaknapp.grid(column=2,row=1)
>
>        Change_units = Tkinter.Button(self,text=u"Change Units",
>                                command=self.OnChange_units)
>        Change_units.grid(column=1,row=2)
>
>
>        self.grid_columnconfigure(0,weight=1)
>        self.grid_columnconfigure(1,weight=1)
>        self.grid_columnconfigure(2,weight=1)
>        self.resizable(True,False)
>        self.update()
>        self.geometry(self.geometry())
>
>
>
>
>    def Onraknaknapp1(self):
>        if enhet == "lbstokg":
>                lbs = float(self.entryVariable1.get())
>                kg = lbs / 0.455
>                self.entryVariable2.set(kg)
>
>        elif enhet == "kgtolbs":
>                kg = float(self.entryVariable1.get())
>                lbs = kg * 0.455
>                self.entryVariable2.set(lbs)
>
>
>
>    def OnChange_units(self):
>
>        class Change_units_tk(Tkinter.Tk):
>            def __init__(self,parent):
>                Tkinter.Tk.__init__(self,parent)
>                self.parent = parent
>                self.initialize()
>
>            def initialize(self):
>                self.grid()
>
>                self.LV2 = Tkinter.StringVar()
>                label = Tkinter.Label(self,textvariable=self.LV2,
>
>  anchor="w",fg="white",bg="blue")
>                label.grid(column=0,row=0,columnspan=2,sticky='EW')
>                self.LV2.set(u"Choose Unit...")
>
>                kgtolbs = Tkinter.Button(self,text=u"Kg to lbs",
>                                command=self.Onkgtolbs)
>                kgtolbs.grid(column=0,row=1)
>
>                lbstokg = Tkinter.Button(self,text=u"Lbs to kg",
>                                command=self.Onlbstokg)
>                lbstokg.grid(column=1,row=1)
>
>                self.grid_columnconfigure(0,weight=1)
>                self.grid_columnconfigure(1,weight=1)
>                self.grid_columnconfigure(2,weight=1)
>                self.resizable(True,False)
>                self.update()
>                self.geometry(self.geometry())
>
>            def Onkgtolbs(self):
>                self.LV.set("Kg to lbs")
>                global enhet
>                enhet = "kgtolbs"
>
>            def Onlbstokg(self):
>                self.LV.set("Lbs to kg")
>                global enhet
>                enhet = "lbstokg"
>
>
>        if __name__ == "__main__":
>            app = Change_units_tk(enhetsomvandlare_tk)
>            app.title("What unit?")
>
>
>    def OnPressEnter(self,event):
>        self.LV.set( "Hej" )
>
>
>
> if __name__ == "__main__":
>    app = enhetsomvandlare_tk(None)
>    app.title("Robert's Enhetsomvandlare")
>    app.mainloop()
>
>
>
> (Some things are swedish, so I'll translate them to make this easier to
> understand)
> Legend for my code:
>
> enhet = unit
> LV = Labelvariable
> raknaknapp = calculatebutton
> Räkna ut = calculate
>
>
>
>
>
>
> Now as soon as I get the crossover thing to work I'll start adding more
> math, functions and other things to it to make it useful. But right now I
> want to get rid of this error message:
>
> Exception in Tkinter callback
> Traceback (most recent call last):
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
>    return self.func(*args)
>  File "/home/robert/Desktop/programmering/python/12 enhets
> omvandlare/12.py", line 118, in OnChange_units
>    app = Change_units_tk(enhetsomvandlare_tk)
>  File "/home/robert/Desktop/programmering/python/12 enhets
> omvandlare/12.py", line 78, in __init__
>    Tkinter.Tk.__init__(self,parent)
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1646, in __init__
>    self.tk = _tkinter.create(screenName, baseName, className, interactive,
> wantobjects, useTk, sync, use)
> TypeError: create() argument 1 must be string or None, not classobj
>
> OR
>
> when i have the line:
>  app = Change_units_tk(enhetsomvandlare_tk)
> changed to:
>  app = Change_units_tk(None)
> I will get the error message:
>
> Exception in Tkinter callback
> Traceback (most recent call last):
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
>    return self.func(*args)
>  File "/home/robert/Desktop/programmering/python/12 enhets
> omvandlare/12.py", line 107, in Onkgtolbs
>    self.LV.set("Kg to lbs")
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1725, in __getattr__
>    return getattr(self.tk, attr)
> AttributeError: LV
>
> Which is the one I really don't get...
>
>
>
> Anyways, I've searched the net for some help, but couldn't find anything
> really helpful. (I know I probably missed it, but this was easier... :P)
>
> Help is appreciated, (I'm a first time poster).
>
> Have a nice day! :)
>
> /Robert
> --
> View this message in context:
> http://old.nabble.com/Multiple-windows%2C-accessing-variables-across-them-won%27t-work-for-me-%28Using-Tkinter%29-tp29503063p29503063.html
> Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20100823/df976bee/attachment-0001.html>


More information about the Tkinter-discuss mailing list