|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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! |
|
#2
|
||||
|
||||
|
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() |
|
#3
|
||||
|
||||
|
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 secondThis should work, if not let me know ![]() Mark |
|
#4
|
||||
|
||||
|
one, two, three jinx telex
, i guess great minds do think alike lolMark. |
|
#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... |
|
#6
|
||||
|
||||
|
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)
|
|
#7
|
||||
|
||||
|
Quote:
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. |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
||||
|
||||
|
Baltor, to use the randint() function from random you need to import the random module..
Mark |
|
#10
|
||||
|
||||
|
Thanks for be so patient with me, I am new to Python and programming in general.
|
|
#11
|
||||
|
||||
|
That's ok Baltar, we all had to start somewhere
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Variable not defined?!? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|