在tkinter Python中获取连续的UDP数据

发布于 2025-01-12 16:48:34 字数 1841 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2025-01-19 16:48:34

我找到了答案,如果将来有人需要解决方案,我会分享它。
只需创建一个更新 GUI 的 while 循环,这样主循环就不会冻结。

from tkinter import *
import socket 
from threading import Thread

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

        self.initialize_udp()


    def stop_receiving(self):
        self.receiving = False

    def initialize_udp(self):
        self.sock.bind((UDP_IP, UDP_PORT))

    def receiving_udp(self):
        while self.receiving == True:
            data, addr = self.sock.recvfrom(1024)
            print(data)
            self.root.update()

    def start_recording(self):
        
        print("start receiving udp data")
        self.receiving = True
        self.receiving_udp()


    def stop_recording(self):
        self.stop_receiving()

if __name__ == "__main__":
    root = Tk()
    gui = Client(root)
    gui.root.mainloop()

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.

from tkinter import *
import socket 
from threading import Thread

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

        self.initialize_udp()


    def stop_receiving(self):
        self.receiving = False

    def initialize_udp(self):
        self.sock.bind((UDP_IP, UDP_PORT))

    def receiving_udp(self):
        while self.receiving == True:
            data, addr = self.sock.recvfrom(1024)
            print(data)
            self.root.update()

    def start_recording(self):
        
        print("start receiving udp data")
        self.receiving = True
        self.receiving_udp()


    def stop_recording(self):
        self.stop_receiving()

if __name__ == "__main__":
    root = Tk()
    gui = Client(root)
    gui.root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文