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

The Next button is not working .when i click "Next" button i'm getting the following error:

File "disp.py", line 24, in func3
    From = int (box1.get().strip())
NameError: global name 'box1' is not defined

Should i have to place the button inside the func3

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Pradeesh tet
  • 617
  • 7
  • 16
  • I don't know if it is a copy/paste gotcha, but you really ought to indent python code with four spaces. That makes indentation errors easier to spot as a bonus. – Reinout van Rees Apr 25 '12 at 13:23
  • Out of curiosity tried PythonTidy, here is the [prettified code](http://ideone.com/SCJIv). I had to replace double newlines too. – KurzedMetal Apr 25 '12 at 14:09

2 Answers2

3

Change your code to this:

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

I think that should work.

SuperPrograman
  • 1,814
  • 17
  • 24
0

box1 was defined inside of the tran() function, and thus (as the error message says) is not globally defined.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • should i have to declare the box1 inside the func3 – Pradeesh tet Apr 25 '12 at 13:26
  • Declaring it in func3() will mean the reference causing the error will now be to a DIFFERENT box1 (the one local to func3). Look here for an explanation of sharing variables between functions: http://stackoverflow.com/q/423379/535275 – Scott Hunter Apr 25 '12 at 13:31
  • @user1190883 The link shared by ScottHunter is good and will probably get you where you need to go. A *better* way of doing it would be to pack all of this into a class in which case the shared information is passed via the class instance without the need for global variables. – mgilson Apr 25 '12 at 14:26