Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2006, 7:14 PM   #1
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
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
MR.T is offline   Reply With Quote
Old Feb 27th, 2006, 7:31 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
variable = variable + 1
__________________

tempest is offline   Reply With Quote
Old Feb 27th, 2006, 7:33 PM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old Feb 27th, 2006, 7:43 PM   #4
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
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
MR.T is offline   Reply With Quote
Old Feb 27th, 2006, 8:05 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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".
Sane is offline   Reply With Quote
Old Feb 27th, 2006, 8:25 PM   #6
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
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.
MR.T is offline   Reply With Quote
Old Feb 27th, 2006, 8:32 PM   #7
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Feb 27th, 2006, 8:36 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 27th, 2006, 8:45 PM   #9
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
^^^Sorry I was in kind of a hurry and forgot about the code thing, I edited my post and won't forget again.
MR.T is offline   Reply With Quote
Old Feb 28th, 2006, 2:03 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Try moving the "num" declaration to the top of the code, before you define the other functions that reference it.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:44 AM.

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