Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   A noob question on editing variables (http://www.programmingforums.org/showthread.php?t=8623)

MR.T Feb 27th, 2006 7:14 PM

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

tempest Feb 27th, 2006 7:31 PM

variable = variable + 1

ReggaetonKing Feb 27th, 2006 7:33 PM

or the alternative that's in Java,

variable++

lol, sorry tempest, had to throw that in there!
wait...this is a Python board...:o

MR.T Feb 27th, 2006 7:43 PM

Quote:

Originally Posted by tempest
variable = variable + 1

If I put something like that I get this error(num is my variable)

UnboundLocalError: local variable 'num' referenced before assignment

Sane Feb 27th, 2006 8:05 PM

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".

MR.T Feb 27th, 2006 8:25 PM

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

Jessehk Feb 27th, 2006 8:32 PM

Quote:

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.
Then why are you making GUI's? Start off with simple text-based programs to learn the foundations of programming.

DaWei Feb 27th, 2006 8:36 PM

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.

MR.T Feb 27th, 2006 8:45 PM

^^^Sorry I was in kind of a hurry and forgot about the code thing, I edited my post and won't forget again.

Arevos Feb 28th, 2006 2:03 AM

Try moving the "num" declaration to the top of the code, before you define the other functions that reference it.


All times are GMT -5. The time now is 4:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC