Attribute Error when trying to setlabel in wx python - 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.

Related

Using Functions from Other .py Files

So I'm very new to Python and programming in general. I've run into an issue with my text game. I'm trying to create a function (pretty sure 'def (name) is a function, right?)in a .py called 'locationMenu' and use it in my main game file. Here is the start of my locationMenu.py (the rest is simply prints and if/elif/else statements.
from main import Engine, Hallway, Canteen, Bedroom, Room
class locationsMenu():
def locationMenu(self):
and here is the place in my main game file where I am trying to use the 'locationMenu' function that I just created.
locationMenu()
That is just one line after I made a choice with input, etc. but now I would like to skip to the locationMenu that is in the other file.
Any help much appreciated. Please don't use "big words" because as I said I'm still very new to programming in general.
locationMenu() is an instance method of the locationsMenu() class, so you'd need to create an instance and call the method on it, something like:
from locationMenu import locationsMenu
my_menu = locationsMenu() # Create a new object
my_menu.locationMenu() # Call its instance method
If you were to just try locationsMenu.locationMenu() then you'd get something like this error:
Traceback (most recent call last):
File "./prog.py", line 3, in <module>
locationsMenu.locationMenu()
TypeError: locationMenu() missing 1 required positional argument: 'self'
because locationMenu() is not a class method, and you're trying to call it in the absence of a locationsMenu object.
If you don't want it inside a class at all, then make it a regular function, and do:
locationMenu.py:
from main import Engine, Hallway, Canteen, Bedroom, Room
def locationMenu():
print("In function locationMenu()")
prog.py:
from locationMenu import locationMenu
locationMenu()

How to use *args in a python class?

I am trying to get some args working in a class, I already got it running in a function from How to use *args in a function?.
I'm trying to get that function into a class but I don't seem to understand how to initialize that class which has an init function taking *args. The code is as following :
class classname(object):
def __init__(self, *args):
<code-snip>
...
</code-snip>
if __name__ == "__main__":
start = classname()
start()
So here I'm confused on what to do with 'start()'. Do I have to use 'start(*sys.argv[1:])' or 'start()'. Both doesn't seem to work. I want to get the *args which is expected in init to be passed properly.
Any pointers please.
Thanks a ton..
======
I'm sorry if I wasn't clear on detailing how it didn't work.
a) While using start(*sys.argv[1:])
Traceback (most recent call last):
File "check.py", line 126, in <module>
start(*sys.argv[1:])
TypeError: 'check' object is not callable
b) While using start(), I get :
Traceback (most recent call last):
File "check.py", line 126, in <module>
start()
TypeError: 'check' object is not callable
These were the errors which came up.
#alko, yes you are correct. I was looking on how to get the *args in init passed properly.
Objects are instantiated by passing arguments to class constructor. They are in turn initalized with __init__ function. In your example this would be
start = ClassName(*sys.argv[1:])
that expression is processed as follows:
New instance of classname is instantiated with object.__new__(ClassName, *sys.argv[1:]) named start in local namespace. From now on start object may be referenced inside your if __name__ == "__main__" script.
Its contents are in turn initialized invoking start.__init__(*sys.argv[1:]). Note that args to __init__ are the same passed to constructor.
And read PEP 8 for python naming convention. That is:
Class names should normally use the CapWords convention.
Your example contains a class which is first instantiated - which involves calling __init__() - and then called - which is done by calling __call__().
So your stuff should be put in the call start = classname(...) in order to be passed to __init__().
The call to the newly instantiated object will fail, however, unless it contains a __call__() method. That would have been easier to answer if you had told us what
Both doesn't seem to work.
exactly means.

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

NameError when using reload()

I've got file named recommend.py. It has a dict data named critics.
When I try to reload it in the interpreter it gives the following error:
>>> from recommend import critics
>>> reload(recommend.py)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'recommend' is not defined
>>>
I'm new to python. Please help me.
recommend.py is parsed as recommend . py which means that python looks for an object bound to the name recommend and then tries to get the py attribute from it. That doesn't work because you don't have an object named recommend in the current namespace and because even if you did have an object bound to that name, it probably wouldn't have an attribute py.
Of course, you'll need to give reload an actual module object. Something more like:
import recommend
reload(recommend)
reload() takes a module object, not a filename:
import recommend
reload(recommend)

Issue with 'StringVar' in Python Program

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.

Categories

Resources