查询功能有问题
root = tk.Tk()
root.title('信息查询')
root.geometry('600x500')
Label(root,text='订单号码:').place(relx='0.12',rely='0.2')
Onum = StringVar()
entry_Onum = Entry(root,textvariable='Onum').place(relx='0.24',rely='0.2',width=200)
Tree1 = ttk.Treeview(root,show="headings",column=('Onum','Fname','Sname','Oadd'))
Tree1.column('Onum', width=50, anchor="center")
Tree1.column('Fname', width=70, anchor="center")
Tree1.column('Sname', width=40, anchor="center")
Tree1.column('Oadd', width=70, anchor="center")
# 表格标题设置
Tree1.heading('Onum', text='订单号码')
Tree1.heading('Fname', text='寄件人姓名')
Tree1.heading('Sname', text='收件人姓名')
Tree1.heading('Oadd', text='物流站点')
Tree1.place(relx=0.01,rely=0.5, relwidth=0.98)
Button(root,text='查询',command=lambda: Guset_info(Tree1),width=15).place(relx='0.65',rely='0.19')
root.mainloop()
def Guset_info(Tree1):
delButton(Tree1)
db = pymysql.connect(host="localhost", user="root", password="123456", db="wqf", port=3306)
cur = db.cursor()
cur.execute("select * from o where Onum = '%s'")
results = cur.fetchall()
if results:
for item in results:
Tree1.insert('', "end", values=item)
else:
showinfo(title='提示', message='无该订单编号!')
cur.close()
db.close()
点击输入单号后查询按钮无反应
没报错吗?你把def Guset_info(Tree1):函数放到上边去试试
你在Guset_info里加个断点,或者print,目测函数根本没执行
如果你的代码没有放到class里面,那么只能调用前面定义好的函数,函数定义在后面会报错找不到定义
而如果你的代码在class里,你的Guset_info方法要么应该有参数self,要么应该声明为静态的
delButton(Tree1)?
先排查下
把下面代码的输出结果发一下,看看进行到哪一步报错,然后进行后续排查。
def Guset_info(Tree1):
print("0")
delButton(Tree1)
print("1")
db = pymysql.connect(host="localhost", user="root", password="123456", db="wqf", port=3306)
print("2")
cur = db.cursor()
print("3")
cur.execute("select * from o where Onum = '%s'")
print("4")
results = cur.fetchall()
print("5")
if results:
print("6")
for item in results:
print("7")
Tree1.insert('', "end", values=item)
else:
print("8")
showinfo(title='提示', message='无该订单编号!')
print("9")
cur.close()
print("10")
改成command=Guset_info(Tree1)
cur.execute("select * from o where Onum = '%s'")#你这格式化字符串没看到变量填充啊,
#要么你就这样
cur.execute("select * from o ")
#要么就这样,把要查找的字段值替换anywords
cur.execute("select * from o where Onum = ‘?' ”,anywords)
另外你在tree中插入数据的时候需要先清空tree,否则点击一次按钮插入一次会造成重复插入