首次尝试使用类

时间:2019-02-18 06:14:27

标签: python-2.7 tk

因此,我读到使用全局是一种不好的做法。之前我只使用函数来完成整个程序,现在我想在函数内使用类。这是类中的两个函数。我得到一个功能对象,没有属性数量项。 错误

def addmoneyindb(self,amountentry):
            #global accno
            moneyentry_ip = amountentry.get()
            query = "select balance from bankui where accountno ="+accno_log+""
            cur.execute(query)
            queryy = cur.fetchone()
            amt = int(queryy[0])+int(moneyentry_ip)
            amt = str(amt)
            print amt
            query1 = "update bankui set balance='"+amt+"' where accountno ="+accno_log+""
            cur.execute(query1)
            tkMessageBox.showinfo("Sucess", "Money Added Sucessfully")
            db.commit()


def addmoney(self):
           # global amountentry
            Screen2 = Toplevel()
            Screen2.title("Add Money")
            Screen2.geometry("1366x768")
            Screen2.configure(background = "light cyan")
            Label(Screen2,text = "ADD MONEY" , height = 3, width = 155, fg = "black" , bg = "RoyalBlue" , font = ("Arial",20)).pack()
            Label(Screen2,text = "" , height = 2, width = 10, fg = "black" , bg = "light cyan").pack()
            Label(Screen2,text = "Enter Amount:" , height = 1, width = 20, fg = "black" , bg = "RoyalBlue" , font = ("Arial",12)).pack()
            Label(Screen2,text = "" , height = 2, width = 10, fg = "black" , bg = "light cyan").pack()
            amountentry = Entry(Screen2)
            amountentry.pack()
            Label(Screen2,text = "" , height = 2, width = 10, fg = "black" , bg = "light cyan").pack()
            Button(Screen2 , text = "Submit", height =4 , width =20, fg = "black" , bg = "RoyalBlue" , command = self.addmoneyindb.amountentry.get(), font = ("Arial",12)).pack()
            Label(Screen2,text = "" , height = 2, width = 10, fg = "black" , bg = "light cyan").pack()
            Label(Screen2,text = "" , height = 1, width = 10, fg = "black" , bg = "light cyan").pack()
            Button(Screen2 , text = "Exit", height =4 , width =20, fg = "black" , bg = "RoyalBlue" , command = Screen2.destroy , font = ("Arial",12)).pack()
            Label(Screen2,text = "" , height = 1, width = 10, fg = "black" , bg = "light cyan").pack()
            Screen2.mainloop()

1 个答案:

答案 0 :(得分:0)

您需要向要通过同一类的其他方法访问的变量添加self。同样,您的Button命令将不起作用-我已经对其进行了修改,以打印self.amountentry的结果。

def addmoneyindb(self): #don't need the amountentry arg
    moneyentry_ip = self.amountentry.get()
    ...

def addmoney(self):
    ...
    self.amountentry = Entry(Screen2)
    self.amountentry.pack()
    ...
    Button(Screen2, text="Submit", height=4, width=20, fg="black", bg="RoyalBlue",
           command=lambda: print(self.amountentry.get()), font=("Arial", 12)).pack()
    ...