因此,我进行了广泛的尝试来找出运行代码的最佳方法。最好的建议是递归运行 root.after 直到满足条件。这有效,但它会冻结窗口,直到满足条件。我不知道出了什么问题或如何解决这个问题。我想显示 tkinter 对话框窗口,每 1000 毫秒检查一次是否满足条件,一旦满足就允许“下一步”按钮变得可单击。这一切都有效,除非条件从未满足,否则无法退出程序,因为导航栏卡在“无响应”状态。我真的需要这个导航栏不被搞乱。与关闭按钮相比,我更喜欢它。这是代码
def checkForPortConnection(root, initial_ports):
new_ports = listSerialPorts()
root.after(1000)
if initial_ports == new_ports:
checkForPortConnection(root, initial_ports)
else:
return
def welcomeGui():
root = tk.Tk()
root.title('Setup Wizard')
canvas1 = tk.Canvas(root, relief = 'flat')
welcome_text='Welcome to the setup wizard for your device'
text2 = 'Please plug your device into a working USB port'
text3 = 'If you have already plugged it in, please unplug it and restart the wizard. \n Do not plug it in until the wizard starts. \n The "NEXT" button will be clickable once the port is detected'
label1 = tk.Label(root, text=welcome_text, font=('helvetica', 18), bg='dark green', fg='light green').pack()
label2 = tk.Label(root, text=text2, font=('times', 14), fg='red').pack()
label3 = tk.Label(root, text=text3, font=('times', 12)).pack()
nextButton = ttk.Button(root, text="NEXT", state='disabled')
nextButton.pack()
initial_ports = listSerialPorts()
root.update()
checkForPortConnection(root, initial_ports)
new_ports = listSerialPorts()
correct_port = [x for x in initial_ports + new_ports if x not in initial_ports or x not in new_ports]
print(correct_port)
nextButton.state(["!disabled"])
root.mainloop()
慕神8447489
相关分类