SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 29th, 2003, 06:34 PM
Baltor's Avatar
Baltor Baltor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 67 Baltor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 5
Question Variable not defined?!?

When I run this program:
Code:
# Dice Roller Program
# By Evan Patterson

from Tkinter import *

class App:

    def __init__(self, master):

        frame = Frame(master)

        # Labels        
        self.label1 = Label(master, text="# of Rolls:")
        self.label2 = Label(master, text="# of Sides:")
        self.label3 = Label(master, text="Results:")
        self.label4 = Label(master, text="Dice Roller\n by Evan Patterson")
        self.label1.grid(sticky=E)
        self.label2.grid(sticky=E)
        self.label3.grid(sticky=E)
        self.label4.grid(row=0, column=2, columnspan=2, rowspan=2)
        
        # Entries
        self.entry1 = Entry(master)
        self.entry2 = Entry(master)
        self.entry1.grid(row=0, column=1)
        self.entry2.grid(row=1, column=1)
    
        # Buttons
        self.exitb = Button(master, text="Exit", command=root.destroy)
        self.rollb = Button(master, text="Roll!", command=self.roll)
        self.resetb = Button(master, text="Clear Results", command=self.clear)
        self.resetb.grid(row=3, column=2)
        self.exitb.grid(row=3, column=3)
        self.rollb.grid(row=2, columnspan=2, column=2)        
        
        # Checkpoint
        rollopt = IntVar()
        dontaddrolls = Checkbutton(root, text="Don't Add Rolls Together", variable=rollopt, onvalue="1", offvalue="0")
        dontaddrolls.grid(columnspan=2, column=0, row=3, sticky=W)

        # Text Box
        self.text = Text(master, height=4, width=20,)
        self.text.grid(row=2, column=1)       
        self.text.config(state=DISABLED)
            
    def clear(self):
        self.text.config(state=NORMAL)
        self.text.delete(1.0, END)
        self.text.config(state=DISABLED)
        
    def roll(self):
        x = rollopt.get()
        if x == 0:
            while self.entry1 > 0:
                rollr = random.randint(1, self.entry2)
                rollrsum = rollrsum + rollr
                self.entry1 = self.entry1 - 1
                self.text.insert(END, rollrsum)
        #else:
        
root = Tk()
app = App(root)
root.title("Dice Roller")
root.mainloop()

It gives me this error:
Code:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:\Python23\My Programs\Dice Roller.txt", line 52, in roll
    x = rollopt.get()
NameError: global name 'rollopt' is not defined

I tried everything but it still happens. Do you know what's wrong and how it can be fixed?
Thanks for help.
__________________
Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!

Reply With Quote
  #2  
Old August 29th, 2003, 07:01 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
When you define "rollopt" in App.__init__ you are assigning it the namespace of that init method, not of the whole class. You need to assign it as "self.rollopt" and then reference it as such within the class...


< rollopt = IntVar()
---
> self.rollopt = IntVar()

< x = rollopt.get()
---
> x = self.rollopt.get()

Reply With Quote
  #3  
Old August 29th, 2003, 07:01 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Balor,

'rollopt' isn't visable by your roll method so you wont be able to use it, inorder to make it available you can either pass the value manually i.e roll(rollopt).. or change rollopt to self.rollopt , personally i'd go for the second

This should work, if not let me know

Mark

Reply With Quote
  #4  
Old August 29th, 2003, 07:04 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
one, two, three jinx telex , i guess great minds do think alike lol

Mark.

Reply With Quote
  #5  
Old August 29th, 2003, 07:21 PM
Baltor's Avatar
Baltor Baltor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 67 Baltor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 5
Your first suggestion gave me this error:
Code:
Traceback (most recent call last):
  File "C:\Python23\My Programs\Dice Roller.txt", line 62, in -toplevel-
    app = App(root)
  File "C:\Python23\My Programs\Dice Roller.txt", line 38, in __init__
    dontaddrolls = Checkbutton(root, text="Don't Add Rolls Together", variable=rollopt, onvalue="1", offvalue="0")
NameError: global name 'rollopt' is not defined

Your second gave me this:
Code:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
TypeError: roll() takes exactly 2 arguments (1 given)

This doesn't make sense...

Reply With Quote
  #6  
Old August 29th, 2003, 07:24 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Heh

There are also some other problems with this code...

In the "roll" function, the line "if x == 0:" won't ever return true because x is an object, not a variable, so even if there are no values in rollopt, it will still always exist.

You also refer to "self.entry1" rather than "self.entry1.get()" when getting the information from the Entry forms. You'll then need to perform an int() operation on those to turn the strings into numbers.

Here's a little amended code:

Code:
    def roll(self):
        x = self.rollopt.get()
        #if x == 0:
	if 1 is 1: # always true, just to retain indentation
	    rollrsum = 0
            while int(self.entry1.get()) > 0:
	        # Deduct one from rolls to go
		newval = int(self.entry1.get())
		newval = newval - 1
		self.entry1.delete(0, END)
		self.entry1.insert(END, newval)
		# Add new value to list
                rollr = random.randint(1, int(self.entry2.get()))
		rollrtext = "".join([str(rollr), " + ", str(rollrsum), " = ", str(rollr + rollrsum), "\n"])
                rollrsum = rollrsum + rollr
        	self.text.config(state=NORMAL)
                self.text.insert(END, rollrtext)
        	self.text.config(state=DISABLED)

Reply With Quote
  #7  
Old August 29th, 2003, 07:26 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Quote:
Originally posted by Baltor
Your first suggestion gave me this error:
Code:
Traceback (most recent call last):
  File "C:\Python23\My Programs\Dice Roller.txt", line 62, in -toplevel-
    app = App(root)
  File "C:\Python23\My Programs\Dice Roller.txt", line 38, in __init__
    dontaddrolls = Checkbutton(root, text="Don't Add Rolls Together", variable=rollopt, onvalue="1", offvalue="0")
NameError: global name 'rollopt' is not defined


You need to change "variable=rollopt" to "variable=self.rollopt"...

You really need to make sure your objects are in the correct namespace every time you use them.

Reply With Quote
  #8  
Old August 29th, 2003, 07:43 PM
Baltor's Avatar
Baltor Baltor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 67 Baltor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 5
Now, something is wrong with the random part. Here is the error:
Code:
Traceback (most recent call last):
  File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:\Python23\My Programs\diceroller.py", line 63, in roll
    rollrtext = "".join([str(rollr), " + ", str(rollrsum), " = ", str(rollr + rollrsum), "\n"])
NameError: global name 'random' is not defined

I remember having this problem before...
Thanks.

Reply With Quote
  #9  
Old August 29th, 2003, 07:49 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Baltor, to use the randint() function from random you need to import the random module..

Mark

Reply With Quote
  #10  
Old August 29th, 2003, 08:48 PM
Baltor's Avatar
Baltor Baltor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 67 Baltor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 5
Thanks for be so patient with me, I am new to Python and programming in general.

Reply With Quote
  #11  
Old August 30th, 2003, 02:41 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
That's ok Baltar, we all had to start somewhere

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Variable not defined?!?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |