![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#12 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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()
... |
|
|
|
|
|
#13 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|