473,549 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter WEIRDNESS or Python WEIRDNESS?

In a nutshell, my problem is that I am getting this runtime error, and
I have no clue why. Please bear in mind its my first python program:

"localvaria ble 'currentAbility ' referenced before asignment"

in this function which is a callback for a button:

def nextThing():
if lookingAtAbilit ies == 1:
currentAbility = currentAbility + 1
if currentAbility not in ability:
currentAbility = 0
clearScreen()
ability[currentAbility].draw(currentAb ility)
drawMainButtons ()
else:
currentCreature = currentCreature + 1
if currentCreature not in creature:
currentCreature = 0
clearScreen()
creature[currentCreature].draw(currentCr eature)
drawMainButtons ()

now the thing is, both lookingAtAbilit ies and currentAbility are
global variables. In fact, they are both assigned at the same part of
the program, right at the start. yet lookingAtAbilit ies is
recognized as a global by this function, and currentAbility is not,
generating this runtime error.

here is the structure of the code:

----------------------------------------
import string
import types
from Tkinter import *

# globals
lookingAtAbilit ies = 1
currentAbility = 2

(snip: some class and function definitions)

def nextThing():
if lookingAtAbilit ies == 1:
currentAbility = currentAbility + 1
if currentAbility not in ability:
currentAbility = 0
clearScreen()
ability[currentAbility].draw(currentAb ility)
drawMainButtons ()
else:
currentCreature = currentCreature + 1
if currentCreature not in creature:
currentCreature = 0
clearScreen()
creature[currentCreature].draw(currentCr eature)
drawMainButtons ()

def drawMainButtons ():
back = Button(win, text="prev", command=prevThi ng)
back.grid(row=2 0, column=0)
next = Button(win, text="next", command=nextThi ng)
next.grid(row=2 0, column=1)

# START OF EXECUTION #

masterWin = Tk()
win = Frame(masterWin )
win.grid()

drawMainButtons ()
masterWin.mainl oop()
------------------------------------------

This has me flumoxed

thanks if you can work it out,

Steve
Jul 18 '05 #1
4 1360
steve said unto the world upon 2005-03-12 00:06:
In a nutshell, my problem is that I am getting this runtime error, and
I have no clue why. Please bear in mind its my first python program:

"localvaria ble 'currentAbility ' referenced before asignment"

in this function which is a callback for a button:

def nextThing():
if lookingAtAbilit ies == 1:
currentAbility = currentAbility + 1
if currentAbility not in ability:
currentAbility = 0
clearScreen()
ability[currentAbility].draw(currentAb ility)
drawMainButtons ()
else:
currentCreature = currentCreature + 1
if currentCreature not in creature:
currentCreature = 0
clearScreen()
creature[currentCreature].draw(currentCr eature)
drawMainButtons ()

now the thing is, both lookingAtAbilit ies and currentAbility are
global variables. In fact, they are both assigned at the same part of
the program, right at the start. yet lookingAtAbilit ies is
recognized as a global by this function, and currentAbility is not,
generating this runtime error.

here is the structure of the code:
<SNIP>
This has me flumoxed

thanks if you can work it out,

Steve


Hi Steve,

While both lookingAtAbilit ies and currentAbility are names in the
global namespace, nextThing() doesn't know that ;-).

The first mention of if lookingAtAbilit ies in nextThing() is:
.. if lookingAtAbilit ies == 1:
This is a reference to the name, so the interpreter fetches
lookingAtAbilit ies value from the first namespace in which it finds
such a name. (The global namespace.)

By contrast, the first mention of currentAbility is:
.. currentAbility = currentAbility + 1
So, the interpreter `creates' (I am a bit fuzzy on the terminology and
not an accomplished Pythoner) a name in the function local namespace
and then looks to see what value to point the name at. When it sees it
should have currentAbility (a name without a value as yet) point to
currentAbility + 1, it barfs.

One way to fix it, is to put `global currentAbility' at the top of
your function definition. (Many frown on globals as it messes with the
namespaces, and gives bad mojo.)

Another would be to pass currentAbility in to your function as an
argument.

Yet another (and probably the best) would be to wrap your code into a
class and then reference self.currentAbi lity.

Still another (but one not so good for clarity or beauty) would be to
put a line like:
temp = currentAbility # temp a name to reference global scope name
# before assignment to follow.

Perhaps more expert voices will chime in and say better things. :-)

HTH,

Brian vdB
Jul 18 '05 #2
steve wrote:
In a nutshell, my problem is that I am getting this runtime error, and
I have no clue why. Please bear in mind its my first python program:

"localvaria ble 'currentAbility ' referenced before asignment"


If you're assigning an object to a name, it will be assigned to that
name in local namespace. So, in your case, you are doing augmented
assignment on a local name that does not yet exist (hence the error
message). You need to tell the interpreter that what you want is assign
to that name in global namespace. You can do this by using the 'global'
statement, as in:
my_global = 1
def some_func():
.... global my_global
.... my_global += 1 some_func()
my_global

2
--

Vincent Wehren
Jul 18 '05 #3
Brian van den Broek <bv****@po-box.mcgill.ca> wrote in message news:<ma******* *************** *************** @python.org>...
steve said unto the world upon 2005-03-12 00:06:
In a nutshell, my problem is that I am getting this runtime error, and
I have no clue why. Please bear in mind its my first python program:

"localvaria ble 'currentAbility ' referenced before asignment"

in this function which is a callback for a button:
now the thing is, both lookingAtAbilit ies and currentAbility are
global variables. In fact, they are both assigned at the same part of
the program, right at the start. yet lookingAtAbilit ies is
recognized as a global by this function, and currentAbility is not,
generating this runtime error.

here is the structure of the code:
<SNIP>
This has me flumoxed

thanks if you can work it out,

Steve


Hi Steve,

While both lookingAtAbilit ies and currentAbility are names in the
global namespace, nextThing() doesn't know that ;-).

The first mention of if lookingAtAbilit ies in nextThing() is:
. if lookingAtAbilit ies == 1:
This is a reference to the name, so the interpreter fetches
lookingAtAbilit ies value from the first namespace in which it finds
such a name. (The global namespace.)

By contrast, the first mention of currentAbility is:
. currentAbility = currentAbility + 1
So, the interpreter `creates' (I am a bit fuzzy on the terminology and
not an accomplished Pythoner) a name in the function local namespace
and then looks to see what value to point the name at. When it sees it
should have currentAbility (a name without a value as yet) point to
currentAbility + 1, it barfs.


Thanks to both of you, this has fixed the problem. I wouldnt have
guessed that in a million years.

But it leads me to ask what is the use of something being in the
gloabl namespace if it cant be accessed globally?

I also find the continual need for objects to reference their own
variables by prefixing 'self' annoying. It seems that Python is the
reverse of my assuptions. whats the rationale for that?
One way to fix it, is to put `global currentAbility' at the top of
your function definition. (Many frown on globals as it messes with the
namespaces, and gives bad mojo.)

Another would be to pass currentAbility in to your function as an
argument.

Yet another (and probably the best) would be to wrap your code into a
class and then reference self.currentAbi lity.

Still another (but one not so good for clarity or beauty) would be to
put a line like:
temp = currentAbility # temp a name to reference global scope name
# before assignment to follow.

Perhaps more expert voices will chime in and say better things. :-)

HTH,

Brian vdB

Jul 18 '05 #4
steve said unto the world upon 2005-03-12 18:46:
Brian van den Broek <bv****@po-box.mcgill.ca> wrote in message news:<ma******* *************** *************** @python.org>...
steve said unto the world upon 2005-03-12 00:06:
In a nutshell, my problem is that I am getting this runtime error, and
I have no clue why. Please bear in mind its my first python program:

"localvariab le 'currentAbility ' referenced before asignment"

in this function which is a callback for a button:
now the thing is, both lookingAtAbilit ies and currentAbility are
global variables. In fact, they are both assigned at the same part of
the program, right at the start. yet lookingAtAbilit ies is
recognized as a global by this function, and currentAbility is not,
generating this runtime error.

here is the structure of the code:
<SNIP>
This has me flumoxed

thanks if you can work it out,

Steve


Hi Steve,

While both lookingAtAbilit ies and currentAbility are names in the
global namespace, nextThing() doesn't know that ;-).

The first mention of if lookingAtAbilit ies in nextThing() is:
. if lookingAtAbilit ies == 1:
This is a reference to the name, so the interpreter fetches
lookingAtAbil ities value from the first namespace in which it finds
such a name. (The global namespace.)

By contrast, the first mention of currentAbility is:
. currentAbility = currentAbility + 1
So, the interpreter `creates' (I am a bit fuzzy on the terminology and
not an accomplished Pythoner) a name in the function local namespace
and then looks to see what value to point the name at. When it sees it
should have currentAbility (a name without a value as yet) point to
currentAbilit y + 1, it barfs.

Thanks to both of you, this has fixed the problem. I wouldnt have
guessed that in a million years.


Hi Steve,

you're welcome. I'm just glad I've finally understood enough to offer
helpful answers :-) (Though note the self-correction at the end of
this post.)
But it leads me to ask what is the use of something being in the
gloabl namespace if it cant be accessed globally?
I think that your question shows there is some residual confusion.
Things in the global namespace can be accessed just fine. The issue
with your code was that, while you had a global name currentAbility in
the global namespace sitting around just waiting to be accessed, your
first use of that name in your function was to assign it.

Assignments are by default to names in the local namespace. If you'd
put the global line in, you'd have told the interpreter to make the
assignment in the global scope. Without this, your function told the
interpreter to create a new name in the local namespace and make it
refer to the object to the right of the '=' which, in your case, was
not well-defined.

There is a certain logic to making assignments be assignments within
the active namepace by default. There may be a deeper reason, but I
don't know it. (To anyone more expert: if there is, I'd love to learn it).
I also find the continual need for objects to reference their own
variables by prefixing 'self' annoying. It seems that Python is the
reverse of my assuptions. whats the rationale for that?


The self prefix is just a conventional way of indicating a reference
to the current class instance. I hated it at first too. Python is my
first language since some BASIC long ago and the selfs were one reason
I steered clear of OOP at first. But, a few evenings of programming
with classes (a recent thing for me) and the aversion went away. So:
give it a bit of time; you might get so you aren't bothered by them.

I should also note there was some residual confusion in what I wrote, too:
Still another (but one not so good for clarity or beauty) would be to
put a line like:
temp = currentAbility # temp a name to reference global scope name
# before assignment to follow.
This won't work. Witness:
a = 42
def my_mistake(): .... temp = a
.... a = a * 2
.... my_mistake() Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "<interacti ve input>", line 2, in my_mistake
UnboundLocalErr or: local variable 'a' referenced before assignment


Sorry 'bout that :-[

Best,

Brian vdB

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7006
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter import * class App:
3
2224
by: phil | last post by:
Using Tkinter Canvas to teach High School Geometry with A LOT of success. My drawing gets very slow after a lot of actions. For instance I have created code to rotate a set of objects about a rotation point. rotate 360 degrees starts to get slow after 720 degrees its crawling.
2
4071
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I open a python interpreter and run my subprocess command, all is well. But when I run the same command from inside a Tkinter app, I'm getting errors.
1
2219
by: John Chambers | last post by:
Sp my latest adventure is attempting to use python's Tkinter module on a few machines. On my PB (OSX 10.3.9), I got the following confusing results: /Users/jc: python Python 2.3 (#1, Sep 13 2003, 00:49:11) on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter Traceback (most recent call...
1
3587
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings...
13
3339
by: Daniel Fetchinson | last post by:
Was looking at PEP 3108, http://www.python.org/dev/peps/pep-3108/ , Is it just me or others also think that it would be a major loss to remove tkinter from the python core? PEP 3108 starts off with: Each module to be removed needs to have a justification as to why it should no longer be distributed with Python. then goes on with,
8
3270
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for...
2
2149
by: Dudeja, Rajat | last post by:
Hi, So, now I've finally started using Eclipse and PyDev as an IDE for my GUI Application. I just wrote some sample programs as an hands on. Now I would like to take up Tkinter. I'm using Active State Python version 2.5 and found that there is not Tkinter and Tk module in it. To use Tkinter do I actually require Tk installed on my...
3
3904
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new...
0
7985
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5387
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.