【问题标题】:Perform a SQL search on a button click with Tkinter使用 Tkinter 对按钮单击执行 SQL 搜索
【发布时间】:2017-09-06 00:39:28
【问题描述】:

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

"Tkinter 回调异常 回溯(最近一次通话最后): 调用中的文件“C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\tkinter__init__.py”,第 1699 行 返回 self.func(*args) 类型错误: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()

【问题讨论】:

  • 错误看起来不言自明:您已将 login 定义为需要参数,但按钮未传递该参数。
  • 每次我尝试传递一个参数我都会得到 - sqlite3.InterfaceError: Error binding parameter 0 - 可能是不受支持的类型。

标签: python-3.x tkinter sqlite


【解决方案1】:

通常编写 GUI 的方式是您不在回调中传递信息。相反,回调在执行时从 UI 请求信息。

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

例如:

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

【讨论】:

  • 现在完美运行,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
  • 2021-09-15
  • 2013-07-27
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多