Issue with 'StringVar' in Python Program - python

I am trying to write a VERY simple UI in Python using Tkinter. I have run into a small problem with the StringVar class. The thing is, when I run the python script, I get an error on the line that initializes the StringVar variable. I have written a sample program with this issue that I would like to get working:
from Tkinter import *
var = StringVar()
var.set('test');
When I run it through python I see this error:
$ python test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
var = StringVar()
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 254, in __init__
Variable.__init__(self, master, value, name)
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception AttributeError: "StringVar instance has no attribute '_tk'" in <bound method StringVar.__del__ of <Tkinter.StringVar instance at 0xb73cc80c>> ignored
I have a feeling that this is an issue with my Python installation, but it may be that I am doing something wrong? I am using python version 2.6.5 on Ubuntu Linux if that makes a difference.

I think you might need to call Tk() explicitly before invoking StringVar.
Just do this:
from Tkinter import *
Tk() # Add this
var = StringVar()
var.set('test');

I've never done anything with Tkinter myself, but here it looks like this StringVar class inherits from a base Variable class, as you can see in the traceback with the call to Variable.__init__(). The exception was raised with the statement "self.tk = master.tk". The following error message indicates that this "master" parameter is NoneType, and thus would have no such tk attribute. Looking at the Tkinter documentation for StringVar here: http://epydoc.sourceforge.net/stdlib/Tkinter.StringVar-class.html
the master parameter is set to default to None. It looks like master should be supplied as a widget that might contain this StringVar (i.e. would it make sense to have a StringVar not associated with a widget?). I would have to say that you most definitely need to associate a StringVar object with a widget for it to have a 'tk' attribute.

Related

Whether the wrapped C++ object of pyqt5 still exists [duplicate]

I have an object in the heap and a reference to it. There are certain circumstances in which the object gets deleted but the reference that points to its location doesn't know that. How can I check if there is real data in the heap?
For example:
from PySide import *
a = QProgressBar()
b = QProgressBar()
self.setIndexWidget(index,a)
self.setIndexWidget(index,b)
Then the a object gets deleted but print(a) returns a valid address. However if you try a.value() - runtime error occurs (C++ object already deleted).
a is None returns False.
For the PySide objects you'll need the shiboken module to perform object queries. For Pyside2, you'll need shiboken2.
import shiboken # shiboken2
print shiboken.isValid(a)
use sip module, read more about sip here
import sip
a = QProgressBar()
sip.isdeleted(a)
False
sip.delete(a)
a
<PyQt4.QtCore.QObject object at 0x017CCA98>
sip.isdeleted(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: underlying C/C++ object has been deleted
It is explicitly mentioned in the documentation when an object takes the responsibility for the deletion of another object. In your example, you can see this in the Qt doc :
If index widget A is replaced with index widget B, index widget A will be deleted.

python tkinter package tk.call eval

I'm using python tkinter to run tcl in python
And there are two ways to run a tcl command:
import tkinter
root = tkinter.Tk()
root.eval("winfo exists .l0")
root.tk.call("winfo exists .l0")
They have the same meaning
But what's different?
And if I haven't define a widget names .l0
and can I directly use
child = ".l0"
child.winfo_exists()
?
Because python told me "str has no attribute winfo_exists"
The difference is that call passes each argument to tcl as a separate word, where eval will evaluate a string by first parsing it and then executing it.
In other words, this:
root.eval("winfo exists .l0")
... is functionally identical to this:
root.tk.call("winfo", "exists", ".l0")
As for the error message 'str' object has no attribute 'winfo_exists', it means exactly that. "l0" is the name of an object in the embedded tcl interpreter, but in python "l0" is just a string and python strings don't have the attribute winfo_exists.

Python's threading.local() isn't working for me

This is pretty simple:
import threading as t
t.local().x = 1
print t.local().x
When I run it, I get this:
Traceback (most recent call last):
File "C:\Documents and Settings\e272nk\Desktop\tst.py", line 3, in <module>
print t.local().x
AttributeError: 'thread._local' object has no attribute 'x'
The attribute assignment seems to be working ok, why can't I retrieve the value?
This is Python v2.7.5 on Windows XP.
You're creating an instance of the class local, setting an attribute on it, then discarding it. Then in the next line, you are creating another instance of local and trying to print a certain attribute from it. Since you didn't set the attribute on that instance, it doesn't have it, and you get an error.
As shown in the documentation, you should be creating an instance of local and keeping it around:
import threading as t
locs = t.local()
locs.x = 1
print locs.x

Attribute Error when trying to setlabel in wx python

This seems like a simple issue but I'm having a very difficult time understanding why I am getting the following error:
Traceback (most recent call last):
File "....py", line 46, in update
self.Grob3Text.SetLabel('Grob 3: ' + str(Grob3))
AttributeError: 'Frame' object has no attribute 'Grob3Text'
When running the following script:
Please see -
.
I understand the message is telling me that the static text variable 'Grob3Text' is not defined in the Frame, but it appears to me that the definition for update event is under the same class. All I'm trying to do is execute the faultreport function and update the static text. I have got this working in another script but for whatever reason I am overlooking something here.
You create a local variable Grob3Text in __init__, but you don't actually store it as an instance attribute on the class instance. To do that, you could simply do:
self.Grob3Text = Grob3Text
in __init__ after you create Grob3Text.

How to know if object gets deleted in Python

I have an object in the heap and a reference to it. There are certain circumstances in which the object gets deleted but the reference that points to its location doesn't know that. How can I check if there is real data in the heap?
For example:
from PySide import *
a = QProgressBar()
b = QProgressBar()
self.setIndexWidget(index,a)
self.setIndexWidget(index,b)
Then the a object gets deleted but print(a) returns a valid address. However if you try a.value() - runtime error occurs (C++ object already deleted).
a is None returns False.
For the PySide objects you'll need the shiboken module to perform object queries. For Pyside2, you'll need shiboken2.
import shiboken # shiboken2
print shiboken.isValid(a)
use sip module, read more about sip here
import sip
a = QProgressBar()
sip.isdeleted(a)
False
sip.delete(a)
a
<PyQt4.QtCore.QObject object at 0x017CCA98>
sip.isdeleted(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: underlying C/C++ object has been deleted
It is explicitly mentioned in the documentation when an object takes the responsibility for the deletion of another object. In your example, you can see this in the Qt doc :
If index widget A is replaced with index widget B, index widget A will be deleted.

Categories

Resources