TKinter GUI 主循环块处理 TCP-IP 命令
TKinter GUI mainloop blocks processing TCP-IP Commands
我在 Raspberry Pi 上使用 TKinter GUI。用户在该 GUI 的输入字段中输入一个数字,然后按 return。之后,条目将被验证并发送到 PC (TCPIP)。 PC 将响应并更新 GUI 显示(获取用户条目 -> 验证 -> 发送到 PC -> PC 接受或拒绝命令 -> 消息出现在 GUI 标签上)
我为 GUI 和条目 validation/TCP-IP 通信制作了单独的 classes。我创建了 GUI 实例“app”和实例 Communication,有必要从 GUI 访问一些变量,因此我必须将实例“app”作为参数提交给通信 class -> Communication(app)
问题是我无法首先使用 app.root.mainloop 启动 GUI,因为 mainloop 命令之后的所有内容都将被忽略,直到我销毁 GUI window。不可能 communication/validation。
尝试 1:
app = Gui()
app.root.mainloop()
print("Cannot reach that code because of GUI main loop")
Communicator = Communication(app)
print("[STARTING] server is starting...")
print("[WAITING] looking for connection...")
listenthread = threading.Thread(target=Communicator.startcommunication())
listenthread.start()
if __name__ == '__main__':
main()
但另一方面:如果我更改顺序并在 mainloop.command 之前创建实例“Communicator”,它也不起作用并且我收到一条错误消息(Gui 对象没有属性.. ..) 因为 Communicator = Communication(app) 此时不知道参数“app”(我想这就是原因)。
尝试 2:
def main():
app = Gui()
Communicator = Communication(app)
print("[STARTING] server is starting...")
print("[WAITING] looking for connection...")
listenthread = threading.Thread(target=Communicator.startcommunication())
listenthread.start()
app.root.mainloop()
# if mainloop command is at this point Communicatior instance cannot be created because class doesn´t now the parameter "app" yet.
if __name__ == '__main__':
main()
除了移动 GUI 中的所有代码外,我不知道如何解决该问题 class。
还有其他机会让它与不同的 classes 一起工作吗?
提前致谢!
首先改变
listenthread = threading.Thread(target=Communicator.startcommunication())
进入
listenthread = threading.Thread(target=Communicator.startcommunication)
否则你正在调用 Communicator.startcommunication()
,然后将 target
设置为任何 startcommunication
returns(可能是 None
)。
其次确保 startcommunication
函数不使用任何 tkinter 变量。 tkinter
不允许从多个线程调用自身(除非使用队列,否则无法解决)。
注意:我不知道你的 startcommunication
函数是做什么的,如果你用 startcommunication
函数中的代码编辑你的答案,我将能够改进这个答案
谢谢。它现在正在工作。第一个问题是 listenthread = threading.Thread(target=Communicator.startcommunication())
。 Wuth 会发生一些奇怪的错误并且线程无法正确启动。第二个问题是我在我的 Gui class 中不小心删除了一个变量声明。现在它看起来像那样并且正在工作。非常感谢!
def main():
app = Gui()
Communicator = Communication(app)
print("[STARTING] server is starting...")
print("[WAITING] looking for connection...")
listenthread = threading.Thread(target=Communicator.startcommunication)
listenthread.start()
app.root.mainloop()
if __name__ == '__main__':
main()
我在 Raspberry Pi 上使用 TKinter GUI。用户在该 GUI 的输入字段中输入一个数字,然后按 return。之后,条目将被验证并发送到 PC (TCPIP)。 PC 将响应并更新 GUI 显示(获取用户条目 -> 验证 -> 发送到 PC -> PC 接受或拒绝命令 -> 消息出现在 GUI 标签上)
我为 GUI 和条目 validation/TCP-IP 通信制作了单独的 classes。我创建了 GUI 实例“app”和实例 Communication,有必要从 GUI 访问一些变量,因此我必须将实例“app”作为参数提交给通信 class -> Communication(app)
问题是我无法首先使用 app.root.mainloop 启动 GUI,因为 mainloop 命令之后的所有内容都将被忽略,直到我销毁 GUI window。不可能 communication/validation。
尝试 1:
app = Gui()
app.root.mainloop()
print("Cannot reach that code because of GUI main loop")
Communicator = Communication(app)
print("[STARTING] server is starting...")
print("[WAITING] looking for connection...")
listenthread = threading.Thread(target=Communicator.startcommunication())
listenthread.start()
if __name__ == '__main__':
main()
但另一方面:如果我更改顺序并在 mainloop.command 之前创建实例“Communicator”,它也不起作用并且我收到一条错误消息(Gui 对象没有属性.. ..) 因为 Communicator = Communication(app) 此时不知道参数“app”(我想这就是原因)。
尝试 2:
def main():
app = Gui()
Communicator = Communication(app)
print("[STARTING] server is starting...")
print("[WAITING] looking for connection...")
listenthread = threading.Thread(target=Communicator.startcommunication())
listenthread.start()
app.root.mainloop()
# if mainloop command is at this point Communicatior instance cannot be created because class doesn´t now the parameter "app" yet.
if __name__ == '__main__':
main()
除了移动 GUI 中的所有代码外,我不知道如何解决该问题 class。 还有其他机会让它与不同的 classes 一起工作吗?
提前致谢!
首先改变
listenthread = threading.Thread(target=Communicator.startcommunication())
进入
listenthread = threading.Thread(target=Communicator.startcommunication)
否则你正在调用 Communicator.startcommunication()
,然后将 target
设置为任何 startcommunication
returns(可能是 None
)。
其次确保 startcommunication
函数不使用任何 tkinter 变量。 tkinter
不允许从多个线程调用自身(除非使用队列,否则无法解决)。
注意:我不知道你的 startcommunication
函数是做什么的,如果你用 startcommunication
函数中的代码编辑你的答案,我将能够改进这个答案
谢谢。它现在正在工作。第一个问题是 listenthread = threading.Thread(target=Communicator.startcommunication())
。 Wuth 会发生一些奇怪的错误并且线程无法正确启动。第二个问题是我在我的 Gui class 中不小心删除了一个变量声明。现在它看起来像那样并且正在工作。非常感谢!
def main():
app = Gui()
Communicator = Communication(app)
print("[STARTING] server is starting...")
print("[WAITING] looking for connection...")
listenthread = threading.Thread(target=Communicator.startcommunication)
listenthread.start()
app.root.mainloop()
if __name__ == '__main__':
main()