首页 文章

使用Tkinter单击按钮执行SQL搜索

提问于
浏览
2

我目前正在创建一个登录系统,以便当用户单击登录按钮时,我的“员工”表被搜索,如果在表中找不到输入的ID,则会打印错误消息,但是,我一直收到此错误 -

"Exception in Tkinter callback Traceback (most recent call last): File " C:\ Users \ User \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ tkinter__init __ . py“,第1699行,在 call 返回self.func(* args)TypeError:login()缺少1个必需的位置参数:'id'“

from tkinter import *
import sqlite3

global employeeIDVar

win = Tk()
img = PhotoImage( file = 'download_1_.gif' )
imgLbl = Label ( win, image = img)

frame1=Frame(win)
frame1.pack()
Label(frame1, text="Welcome to the system!",font=('Comic Sans MS',18)).grid(row=0, column=1)

Label(frame1, text="EmployeeID").grid(row=1, column=0, sticky=W)
employeeIDVar=IntVar(win)
eID= Entry(frame1, textvariable=employeeIDVar)
eID.grid(row=1,column=1,sticky=W)

frame2 = Frame(win)
frame2.pack()

b1= Button(frame2, text=" Login ")
b2= Button(frame2, text=" Quit ")
b1.pack(side=LEFT); b2.pack(side=LEFT)

def login(id):
    with sqlite3.connect("comicBookGuys.db") as db:

            cursor = db.cursor()
            cursor.execute ("select employeeID, numberOfSales, salesTarget from Employee where employeeID=?", (id,))
            dataFound = cursor.fetchone()
            return dataFound    

            if not dataFound:
                messagebox.showinfo("No such employeeID found! Try again.")

def logEnd():
    exit()

b1.configure(command=login)
b2.configure(command=logEnd)
win.mainloop()

win.mainloop()

1 回答

  • 2

    GUI的编写方式是您不在回调中传递信息 . 相反,回调在执行时请求来自UI的信息 .

    在您的情况下,我建议您删除 login 的参数,并修改 login 以在调用时获取信息 .

    例如:

    def login():
        id = eID.get()
        ...
    

相关问题