【发布时间】:2020-05-08 22:42:44
【问题描述】:
我正在使用 tkinter 编写应用程序,按下按钮时应用程序连接到 sql DB 并运行 sql 查询。我遇到了一个问题,在 sql 查询方法运行时 GUI 显示“没有响应”,完成后又恢复正常。我决定将 sql 查询放在自己的线程中,将 tkinter 的 mainloop 放在自己的线程中,但它根本不起作用......?
def createGUI():
window = Tk()
window.title("Recon Report Generator")
lbl=Label(window, text="Enter Recon File Below", fg='blue', font=("Helvetica", 16))
lbl.place(x=90,y=50)
textBox=Entry(window, text="This is Entry Widget", bd=5)
textBox.place(x=135, y=100)
button=Button(window, height=1, width=15, text="Find Missing",
command=lambda: retrieve_input(textBox,lbl))
button.place(x=140, y=150)
window.geometry("400x400")
window.resizable(False, False)
_thread.start_new_thread(window.mainloop(), (0,))
这是我的 GUI 我把主循环放在线程 0 中
这里
def retrieve_input(textBox,lbl):
detFile = "'"+(textBox.get())+"'"
lbl.place(x=140,y=50)
lbl.config(text="Enter Recon File Below")
_thread.start_new_thread(runDatabaseQuerys(detFile), (1,))
lbl.config(text="Finished...")
print("Finished Please check file")
我将 sql 查询运行方法放在线程 1 中,它们是独立线程,但我仍然“没有响应”???
【问题讨论】:
-
你实际上并没有在线程中运行任何东西。您实际上是在主线程中立即调用
window.mainloop()和runDatabaseQuerys(detFile),然后尝试使用它们的返回值作为函数在线程中运行。
标签: python python-3.x tkinter