Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2006, 9:30 AM   #11
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
Ok heres my code with the variables defined in the begining of the program.

#!/usr/bin/python

from Tkinter import *

temp1 = "I have "
temp2 = "5 "
temp3 = open("test.txt","w")
num = 0

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

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()

but I still get the "UnboundLocalError: local variable 'num' referenced before assignment" error.

what could be the problem?
MR.T is offline   Reply With Quote
Old Feb 28th, 2006, 9:38 AM   #12
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Ah, sorry. I missed the fact that num is not global.

You need to define "num" as global in Insert:
def Insert():
             global num
             num += 1
	     input = ent.get()
             ...
This is to tell Python that you want to use the "num" that has a global scope, rather than to create a new variable called "num" local to the function.
Arevos is offline   Reply With Quote
Old Feb 28th, 2006, 9:50 AM   #13
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
YAHOOO it works. I new it had something to do with local global variable thing, but I didnt now you had to define it as global first, before I tried doing like "global num += 1" but that of course didnt work.

thanks you all very much for taking the time to help me
MR.T is offline   Reply With Quote
Old Feb 28th, 2006, 4:13 PM   #14
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
I still agree with Jessehk on this one, starting with GUIs is probably the best way to work yourself in to a corner of non-confidence.
Sane 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 3:34 PM.

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