2012-04-25 33 views
0
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

+0

我不知道它是一个复制/粘贴gotcha,但你真的应该缩进四个空格的Python代码。这使得缩进错误更容易被视为奖金。 – 2012-04-25 13:23:15

+0

出于好奇,尝试过PythonTidy,这里是[prettified code](http://ideone.com/SCJIv)。我不得不更换双换行符。 – KurzedMetal 2012-04-25 14:09:24

回答

3

你的代码改成这样:

from Tkinter import * 

import MySQLdb 

class App: 

    def __init__(self, master): 
     Button3 = Button(master, text="Transaction", command=self.tran).pack() 

    def tran(self) : 

     first = Tk() 

     label1 = Label(first ,text="From") 

     label1.pack() 

     self.box1=Entry(first) 

     self.box1.pack() 

     label2=Label(first ,text="To") 

     label2.pack() 

     self.box2=Entry(first) 

     self.box2.pack() 


     label3=Label(first ,text="Amt") 

     label3.pack() 

     self.box3=Entry(first) 

     self.box3.pack() 

     Button1 = Button(first , text="Next", command=self.func3).pack() 


    def func3(self) : 

     conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "natty", db = "dbms") 

     cursor=conn.cursor() 

     From=int(self.box1.get().strip()) 

     To=int(self.box2.get().strip()) 

     Amt=int(self.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() 

app = App(master) 

master.mainloop() 

我认为应该工作。

+0

除非函数是在我们没有理由相信它们的类中定义的,否则这将不起作用。 (这只会引发一个关于'self'没有被定义的错误)。即使函数是在类中定义的,“self”应该是函数的第一个参数。 – mgilson 2012-04-25 14:24:00

+0

谢谢@mgilson!我已经相应地更改了代码。 – SuperPrograman 2012-04-25 14:54:25

+0

@SuperProgramam乍一看,这看起来好多了。 – mgilson 2012-04-25 15:44:35

0

BOX1是在TRAN()函数的内部定义,并且因此(作为错误消息表示)内的按钮没有全局定义。

+0

我应该必须声明func3中的box1 – 2012-04-25 13:26:37

+0

在func3()中声明它将意味着引起错误的引用现在将转到不同的box1(func3本地的那个)。看看这里的功能之间共享变量的解释:http://stackoverflow.com/q/423379/535275 – 2012-04-25 13:31:48

+0

@ user1190883由ScottHunter共享的链接是好的,可能会让你到你需要去的地方。一个更好的方法是将所有这些打包到一个类中,在这种情况下,共享信息通过类实例传递而不需要全局变量。 – mgilson 2012-04-25 14:26:24

相关问题