![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
A noob question on editing variables
OK I have just started to learn how to program in python yesterday and I have never programed before that, that being said please don't flame me to much if this is a really stupid question.
OK heres what I want to do, I have a button made using tkinter and when I click that button it runs a function(I have all that set up already), now what I want the function to do is to add a number to a global variable and have that variable keep that value,for example if the variable originally equals 0 then when I click the button the variable would equal 1 and if I click the button a second time the variable would equal 2 and so on. So how would I do that? thanks in advance |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
variable = variable + 1
__________________
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
or the alternative that's in Java,
variable++ lol, sorry tempest, had to throw that in there! wait...this is a Python board...:o
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
Quote:
UnboundLocalError: local variable 'num' referenced before assignment |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
You first have to declare "num" with an initial value. If you are trying to add num to itself plus one, it must first have a value.
Try putting "num = 0" at the beginning of your program. As well, "num += 1" is a more well-formed alternative to "num = num + 1". |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
When doing that I get this error:
UnboundLocalError: local variable 'num' referenced before assignment. Here is the whole code I am using: #!/usr/bin/python
from Tkinter import *
def Insert():
num += 1
input = ent.get()
temp3.write(temp1)
temp3.write(str(num))
temp3.write(temp2)
temp3.write(input)
temp3.write('\n')
ent.delete(0,END)
def Close():
temp3.close()
def Open():
open("test.txt","w")
num = 0
temp1 = "I have "
temp2 = "5 "
temp3 = open("test.txt","w")
root = Tk()
root.geometry('200x210+350+70')
ent = Entry(root, bg = 'white')
button = Button(root, text = "Insert", command = Insert)
button2 = Button(root, text = "Close File", command = Close)
button3 = Button(root, text = "Open File", command = Open)
ent.pack(anchor = W)
button.pack(padx = 4, pady = 4, anchor= E)
button2.pack(padx = 1, pady = 8, anchor= E)
button3.pack(padx = 3, pady = 15, anchor= E)
root.mainloop()As you can see it creates a window, a entry box, and three buttons. First you click th "open" button to create a text file , then you enter something in the entry box and press "insert", what was in the entry box is added to the text file along with the value of the variable I'm having a problem with and another string, when your done adding things to the text file you press "close" to close the file and that all there is to it Last edited by MR.T; Feb 27th, 2006 at 8:43 PM. |
|
|
|
|
|
#7 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Quote:
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Also, you might actually pay attention to the responses you have. You also need to read the forum's rules/FAQ and learn about code tags.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
^^^Sorry I was in kind of a hurry and forgot about the code thing, I edited my post and won't forget again.
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Try moving the "num" declaration to the top of the code, before you define the other functions that reference it.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|