Using StringVars in List of Dictionaries as tkInter variables forScale and Label widgets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seanacais

    Using StringVars in List of Dictionaries as tkInter variables forScale and Label widgets

    I'm trying to build an unknown number of repeating gui elements
    dynamically so I need to store the variables in a list of
    dictionaries. I understand that Scale "variable" name needs to be a
    StringVar but I cannot figure out how to initialize the dictionary.

    I've tried the following code

    ps = PowerSupply() # Instantiate a Power Supply VM Object
    numOPs = ps.getOnum() # Find the number of outputs
    OPValues = [] # Global list to hold one dictionary per output

    for c in range(numOPs):
    OPValues.append ({ })
    ps.initOPValues (c, OPValues[c])



    ps.initOPValues is defined as follows:

    def initOPValues(se lf, OPname, dname):

    OPDefaults = {
    'Vval' : 0,
    'Ival' : 0,
    'Otemp' : 0
    }

    dname = dict((d,StringV ar()) for d in OPDefaults)
    for d in OPDefaults:
    dname[d].set(OPDefaults[d])

    I get the following error:

    Traceback (most recent call last):
    File "C:\Code\gui\tk inter.py", line 165, in <module>
    ps.initOPValues (c, OPValues[c])
    File "C:\Code\gui\tk inter.py", line 47, i initOPValues
    dname = dict((d,StringV ar()) for d in OPDefaults)
    File "C:\Code\gui\tk inter.py", line 47, in <genexpr>
    dname = dict((d,StringV ar()) for d in OPDefaults)
    File "C:\Python25\li b\lib-tk\Tkinter.py", line 254, in __init__
    Variable.__init __(self, master, value, name)
    File "C:\Python25\li b\lib-tk\Tkinter.py", line 185, in __init__
    self._tk = master.tk
    AttributeError: 'NoneType' object has no attribute 'tk'
    Exception exceptions.Attr ibuteError: "StringVar instance has no
    attribute '_tk'"
    in <bound method StringVar.__del __ of <Tkinter.String Var instance at
    0x00B7D468>igno red

    Any help is appreciated!

    Thanks,

    Kevin
  • Peter Otten

    #2
    Re: Using StringVars in List of Dictionaries as tkInter variables for Scale and Label widgets

    seanacais wrote:
    I'm trying to build an unknown number of repeating gui elements
    dynamically so I need to store the variables in a list of
    dictionaries. I understand that Scale "variable" name needs to be a
    StringVar but I cannot figure out how to initialize the dictionary.
    >
    I've tried the following code
    >
    ps = PowerSupply() # Instantiate a Power Supply VM Object
    numOPs = ps.getOnum() # Find the number of outputs
    OPValues = [] # Global list to hold one dictionary per output
    >
    for c in range(numOPs):
    OPValues.append ({ })
    ps.initOPValues (c, OPValues[c])
    >
    >
    >
    ps.initOPValues is defined as follows:
    >
    def initOPValues(se lf, OPname, dname):
    >
    OPDefaults = {
    'Vval' : 0,
    'Ival' : 0,
    'Otemp' : 0
    }
    >
    dname = dict((d,StringV ar()) for d in OPDefaults)
    for d in OPDefaults:
    dname[d].set(OPDefaults[d])
    >
    I get the following error:
    >
    Traceback (most recent call last):
    File "C:\Code\gui\tk inter.py", line 165, in <module>
    ps.initOPValues (c, OPValues[c])
    File "C:\Code\gui\tk inter.py", line 47, i initOPValues
    dname = dict((d,StringV ar()) for d in OPDefaults)
    File "C:\Code\gui\tk inter.py", line 47, in <genexpr>
    dname = dict((d,StringV ar()) for d in OPDefaults)
    File "C:\Python25\li b\lib-tk\Tkinter.py", line 254, in __init__
    Variable.__init __(self, master, value, name)
    File "C:\Python25\li b\lib-tk\Tkinter.py", line 185, in __init__
    self._tk = master.tk
    AttributeError: 'NoneType' object has no attribute 'tk'
    Exception exceptions.Attr ibuteError: "StringVar instance has no
    attribute '_tk'"
    in <bound method StringVar.__del __ of <Tkinter.String Var instance at
    0x00B7D468>igno red
    >
    Any help is appreciated!
    There's some magic going on behind the scene which means that you have to
    create a Tkinter.Tk instance before you can start churning out StringVars:
    >>import Tkinter as tk
    >>v = tk.StringVar()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 257, in __init__
    Variable.__init __(self, master, value, name)
    File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 188, in __init__
    self._tk = master.tk
    AttributeError: 'NoneType' object has no attribute 'tk'
    >>root = tk.Tk()
    >>v = tk.StringVar()
    >>v.set(42)
    >>v.get()
    '42'

    Peter

    Comment

    • seanacais

      #3
      Re: Using StringVars in List of Dictionaries as tkInter variables forScale and Label widgets

      On May 19, 2:11 pm, Peter Otten <__pete...@web. dewrote:
      >
      There's some magic going on behind the scene which means that you have to
      create a Tkinter.Tk instance before you can start churning out StringVars:
      >
      >import Tkinter as tk
      >v = tk.StringVar()
      >
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 257, in __init__
      Variable.__init __(self, master, value, name)
      File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 188, in __init__
      self._tk = master.tk
      AttributeError: 'NoneType' object has no attribute 'tk'>>root = tk.Tk()
      >v = tk.StringVar()
      >v.set(42)
      >v.get()
      >
      '42'
      >
      Peter

      I had the Tkinter import as

      from Tkinter import * but I changed it to

      import Tkinter as tk

      and modified the creation of the root object to

      root=tk.Tk()

      I then had to change every instance of Menu, Label,
      Button, and all Tkinter elements to be prefaced by
      tk. (so Button became tk.Button). That kind of makes
      sense to me.

      However even after specifying StringVar is a tk type

      def initOPValues(se lf, OPname, dname):

      OPDefaults = {
      'Vval' : 0,
      'Ival' : 0,
      'Otemp' : 0
      }

      dname = dict((d,tk.Stri ngVar()) for d in OPDefaults)
      for d in OPDefaults:
      dname[d].set(OPDefaults[d])


      I still get a very similar error message

      C:\Code\gui>tki nter.py
      Traceback (most recent call last):
      File "C:\Code\gui\tk inter.py", line 169, in <module>
      ps.initOPValues (c, OPValues[c])
      File "C:\Code\gui\tk inter.py", line 54, in initOPValues
      dname = dict((d,tk.Stri ngVar()) for d in OPDefaults)
      File "C:\Code\gui\tk inter.py", line 54, in <genexpr>
      dname = dict((d,tk.Stri ngVar()) for d in OPDefaults)
      File "C:\Python25\li b\lib-tk\Tkinter.py", line 254, in __init__
      Variable.__init __(self, master, value, name)
      File "C:\Python25\li b\lib-tk\Tkinter.py", line 185, in __init__
      self._tk = master.tk
      AttributeError: 'NoneType' object has no attribute 'tk'
      Exception exceptions.Attr ibuteError: "StringVar instance has no
      attribute '_tk'"
      in <bound method StringVar.__del __ of <Tkinter.String Var instance at
      0x00B7E418>igno red


      Thanks!

      Kevin

      Comment

      • Peter Otten

        #4
        Re: Using StringVars in List of Dictionaries as tkInter variables for Scale and Label widgets

        seanacais wrote:
        I had the Tkinter import as
        >
        from Tkinter import * but I changed it to
        >
        import Tkinter as tk
        >
        and modified the creation of the root object to
        >
        root=tk.Tk()
        >
        I then had to change every instance of Menu, Label,
        Button, and all Tkinter elements to be prefaced by
        tk. (so Button became tk.Button). That kind of makes
        sense to me.
        >
        However even after specifying StringVar is a tk type
        You misunderstood. There is no such thing as a "tk type".
        Whether you use

        from Tkinter import *

        or

        import Tkinter as tk

        is irrelevant for the problem at hand.
        def initOPValues(se lf, OPname, dname):
        >
        OPDefaults = {
        'Vval' : 0,
        'Ival' : 0,
        'Otemp' : 0
        }
        >
        dname = dict((d,tk.Stri ngVar()) for d in OPDefaults)
        for d in OPDefaults:
        dname[d].set(OPDefaults[d])
        >
        >
        I still get a very similar error message
        You have to ensure that the lines

        from Tkinter import *
        root = Tk()

        are *executed* before the line

        dname = dict((d, StringVar()) for d in OPDefaults)

        While I prefer the alternative

        import Tkinter as tk
        root = tk.Tk()
        # ...
        dname = dict((d, tk.StringVar()) for d in OPDefaults)

        this is just a matter of style.

        Peter

        PS: If you still can't fix your script, please post it completely or,
        better, a small self-contained (runnable) script showing the same problem

        Comment

        Working...