这篇文章的目的是确定代码中的错误,我花了无数个小时试图改变代码以查看错误是什么。我试图通过python GUI(tkinter)将员工添加到数据库。目前只插入" "信息,这是在我们的def删除与print语句。我使用def删除类似的问题但是元组中的信息没有显示在listBox中。有没有人能解决为什么会出现这些故障?
def addEmp():
top = Toplevel()
top.title("Add Employee")
top.geometry("470x385")
app = Frame(top)
app.grid()
firstName = StringVar()
lastName = StringVar()
address = StringVar()
payRate = StringVar()
inUnion = StringVar()
paymentMethod = StringVar()
grp1 = StringVar()
firstNameLabel = Label (app, text = "First Name:", font = 15)
firstNameLabel.grid(row = 0, column = 0)
firstNameEntry = Entry (app, text = "", textvariable = firstName)
firstNameEntry.grid(row = 0, column = 1)
surNameLabel = Label (app, text = "Surname:", font = 15)
surNameLabel.grid(row = 1, column = 0)
lastNameEntry= Entry (app, text = "", textvariable = lastName)
lastNameEntry.grid(row = 1, column = 1)
addressLabel = Label (app, text = "Address:", font = 15)
addressLabel.grid(row = 2, column = 0)
addressEntry = Entry (app, text = "", textvariable = address)
addressEntry.grid(row = 2, column = 1)
payRateLabel = Label (app, text = "Pay Rate Hourly:", font = 15)
payRateLabel.grid(row = 3, column = 0)
payRateEntry = Entry (app, text = "", textvariable = payRate)
payRateEntry.grid(row = 3, column = 1)
salaryLabel = Label (app, text = "Pay Rate Monthly:", font = 15)
salaryLabel.grid(row = 4, column = 0)
salaryEntry = Entry (app, text = "")
salaryEntry.grid(row = 4, column = 1)
payTypeLabel = Label (app, text = "Work Type:", font = 15)
payTypeLabel.grid(row = 5, column = 0)
radio1 = Radiobutton(app, text = "Full Time (Monthly Salary)", value = 'Monthly', variable = grp1)
radio1.grid(row = 6, column = 1)
radio2 = Radiobutton(app, text = "Part Time (Hourly Pay per Week)", value = 'Weekly', variable = grp1)
radio2.grid(row = 7, column = 1)
radio3 = Radiobutton(app, text = "Commission", value = 'Commission', variable = grp1)
radio3.grid(row = 8, column = 1)
paymentOptionsLabel = Label (app, text = "Payment Option:", font = 15)
paymentOptionsLabel.grid(row = 9, column = 0)
radio4 = Radiobutton(app, text = "Mail to current address", value = "Mail", variable = paymentMethod)
radio4.grid(row = 10, column = 1)
radio5 = Radiobutton(app, text = "Held by Paymaster", value = "Hold", variable = paymentMethod)
radio5.grid(row = 11, column = 1)
radio6 = Radiobutton(app, text = "Directly Deposit into bank account", value = "Bank", variable = paymentMethod)
radio6.grid(row = 12, column = 1)
unionLabel = Label (app, text = "Union:", font = 15)
unionLabel.grid(row = 13, column = 0)
radio1union = Radiobutton(app, text = "Yes", value = '1', variable = inUnion)
radio1union.grid(row = 14, column = 1)
radio2union = Radiobutton(app, text = "No", value = '0', variable = inUnion)
radio2union.grid(row = 15, column = 1)
submitButton = Button (app, command = addEmpSubmit, text = "Submit", font = 15)
submitButton.grid(row = 20, column = 3)
def addEmpSubmit():
c.execute("INSERT INTO employees (first_name, last_name, address, in_union, pay_type, payment_method) VALUES (?, ?, ?, ?, ?, ?)",
(str(firstName), str(lastName), str(address), str(inUnion), str(payType), str(paymentMethod)))
connection.commit()
if payType == "Weekly":
c.execute("UPDATE employees SET pay_rate=? WHERE employee_id=?", (payRate, c.lastrowid))
connection.commit()
elif payType == "Monthly":
c.execute("UPDATE employees SET monthly_salary=? WHERE employee_id=?", (monthlySalary, c.lastrowid))
connection.commit()
elif payType == "Commission":
c.execute("UPDATE employees SET monthly_salary=?, commission_rate=? WHERE employee_id=?",
(monthlySalary, commissionRate, c.lastrowid))
connection.commit()
def deleteEmp():
top = Toplevel()
top.title("Delete Employee")
top.geometry("470x385")
deleteList = Listbox(app)
deleteList.grid(row = 0, column = 0)
listItem = ""
c.execute("SELECT * FROM employees")
allEmp = c.fetchall()
for line in allEmp:
for item in line:
print(str(item))
listItem += str(item)
deleteList.insert(END, listItem)
connection.commit()
empIDLabel = Label (app, text = "Please select an Emp ID:", font = 15)
empIDLabel.grid(row = 1, column = 0)
empIDEntry = Entry (app, text = "")
empIDEntry.grid(row = 2, column = 0)
deleteButton = Button (app, text = "Delete", font = 15)
deleteButton.grid(row = 2, column = 1)
答案 0 :(得分:-1)
INSERT INTO
提供的值 - 查询不是条目小部件的值。
>>> name=tk.StringVar()
>>> name.set("First Name")
>>> name
<Tkinter.StringVar instance at 0x01EB4CB0>
>>> str(name)
'PY_VAR0'
>>> name
<Tkinter.StringVar instance at 0x01EB4CB0>
>>> name.get()
'First Name'
请确保您发布仅代码示例,这些示例可以在(至少)您的系统上重现您的错误。至于这个原因,非常难以重现你所遇到的问题。 拆分代码并在较小的片段中调试它。自己检查不同的组件(例如,首先使用一些虚拟数据但使用变量,StringVars等sql部分)。
您收到任何错误消息吗?如果是这样,请将它们添加到您的帖子中,以便我们可以看到您是否在某个时刻挣扎。像 - &#34;我的代码不起作用 - 为什么?&#34;如果有正确的信息,很难分析。