from Tkinter import *
import MySQLdb
def tran() :
first= Tk()
label1 = Label(first ,text="From")
label1.pack()
box1=Entry(first)
box1.pack()
label2=Label(first ,text="To")
label2.pack()
box2=Entry(first)
box2.pack()
label3=Label(first ,text="Amt")
label3.pack()
box3=Entry(first)
box3.pack()
Button1 = Button(first , text="Next", command=func3).pack()
def func3() :
conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "natty", db = "dbms")
cursor=conn.cursor()
From=int(box1.get().strip())
To=int(box2.get().strip())
Amt=int(box3.get().strip())
cursor.execute ("select bal from account where acc="+str(From)+"")
a=cursor.fetchone()
fromval=int(a[0])
cursor.execute ("select bal from account where acc="+str(To)+"")
b=cursor.fetchone()
toval=int(b[0])
fromval=fromval-Amt
toval=toval+Amt
cursor.execute("update account set bal="+str(fromval)+" where acc="+str(From)+"")
cursor.execute("update account set bal="+str(toval)+" where acc="+str(To)+"")
cursor.close()
conn.close()
master = Tk()
Button3 = Button(master, text="Transaction", command=tran).pack()
mainloop()
screen shot蟒蛇Tk的接口:NameError:全局名称“盒1”没有定义
下一个按钮不工作。当我点击“下一步”按钮,我发现了以下错误:
File "disp.py", line 24, in func3 From = int (box1.get().strip()) NameError: global name 'box1' is not defined
我应该必须把FUNC3
我不知道它是一个复制/粘贴gotcha,但你真的应该缩进四个空格的Python代码。这使得缩进错误更容易被视为奖金。 – 2012-04-25 13:23:15
出于好奇,尝试过PythonTidy,这里是[prettified code](http://ideone.com/SCJIv)。我不得不更换双换行符。 – KurzedMetal 2012-04-25 14:09:24