在tkinter Python中获取连续的UDP数据
我是 Tkinter 的新手,在接收 UDP 数据时遇到问题。 我有一个有 2 个按钮的应用程序:“开始录制”和“停止录制”。
我按照这里在其他人的问题中给出的一些说明进行操作 one 但我仍然无法按预期完成这项工作。 我的程序冻结了,我无法再使用其他按钮,我知道这是因为我不再经过主循环,但我不知道该怎么做。
我的应用程序现在非常简单:
from tkinter import *
import socket
UDP_PORT = 40
UDP_IP = "127.0.0.1"
class Client:
def __init__(self, root):
self.root = root
root.title("Title")
root.geometry("550x350")
root.resizable(0, 0)
back = Frame(root, bg='black')
Label(root, text="LABEL", anchor='w', width=20).grid(row=1, column=0)
self.start_recording_button = Button(root, text="Start Recording", command=self.start_recording, anchor='w',
width=20)
self.start_recording_button.grid(row=5, column=0)
self.stop_recording_button = Button(root, text="Stop Recording", command=self.stop_recording, anchor='w',
width=20)
self.stop_recording_button.grid(row=6, column=0)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.receiving = True
def stop_receiving(self):
self.receiving = False
def initialize_udp(self):
self.sock.bind((UDP_IP, UDP_PORT))
def start_receiving(self):
if self.receiving == True:
data, addr = self.sock.recvfrom(1024)
print(data)
self.root.after(1000, self.start_receiving())
def start_recording(self):
self.initialize_udp()
self.start_receiving()
def stop_recording(self):
pass
if __name__ == "__main__":
root = Tk()
gui = Client(root)
root.mainloop()
谢谢您的帮助。
I am new using Tkinter, I have an issue receiving UDP data.
I have an app that has 2 buttons: "start recording" and "stop recording".
I followed some instructions given in here in other people questions like this one but still I couldn't manage to make that work as expected.
My program freezes and I cannot use the other buttons anymore, I know that it comes from the fact that I am not going through the mainloop anymore, but I don't know how to do so.
My app is really simple for now:
from tkinter import *
import socket
UDP_PORT = 40
UDP_IP = "127.0.0.1"
class Client:
def __init__(self, root):
self.root = root
root.title("Title")
root.geometry("550x350")
root.resizable(0, 0)
back = Frame(root, bg='black')
Label(root, text="LABEL", anchor='w', width=20).grid(row=1, column=0)
self.start_recording_button = Button(root, text="Start Recording", command=self.start_recording, anchor='w',
width=20)
self.start_recording_button.grid(row=5, column=0)
self.stop_recording_button = Button(root, text="Stop Recording", command=self.stop_recording, anchor='w',
width=20)
self.stop_recording_button.grid(row=6, column=0)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.receiving = True
def stop_receiving(self):
self.receiving = False
def initialize_udp(self):
self.sock.bind((UDP_IP, UDP_PORT))
def start_receiving(self):
if self.receiving == True:
data, addr = self.sock.recvfrom(1024)
print(data)
self.root.after(1000, self.start_receiving())
def start_recording(self):
self.initialize_udp()
self.start_receiving()
def stop_recording(self):
pass
if __name__ == "__main__":
root = Tk()
gui = Client(root)
root.mainloop()
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了答案,如果将来有人需要解决方案,我会分享它。
只需创建一个更新 GUI 的 while 循环,这样主循环就不会冻结。
I found the answer, I'm sharing it if someone needs the solution in the future.
Just create a while loop that updates the GUI, thus the mainloop doesn't freeze.