社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

scotty3785

scotty3785 最近创建的主题
scotty3785 最近回复了
6 年前
回复了 scotty3785 创建的主题 » python 3:tkinter gui中的udp包发送/接收

继续我对你问题的评论。 下面是一个简单的例子,说明我如何处理这些问题(我通常会做更多的oop,但这是一个简单的例子)。

我将使用tkinter.after方法来计划定期运行发送/接收函数。

import tkinter as tk

sending_enabled = False

def send_message():
    if sending_enabled:
        print("Sending Message")
        root.after(500,send_message)

def receive_messages():
    print("Getting Messages")
    root.after(1000,recieve_messages)


def start_sending():
    global sending_enabled
    if not sending_enabled:
        root.after(500,send_message)
        sending_enabled = True

def stop_sending():
    global sending_enabled
    sending_enabled = False


root = tk.Tk()

startButton = tk.Button(root,text="Start",command=start_sending)
startButton.grid()
stopButton = tk.Button(root,text="Stop",command=stop_sending)
stopButton.grid()
root.after(1000,receive_messages)

root.mainloop()

这个 receive_message 函数计划在程序启动1000毫秒后首先运行,然后每1000毫秒调用一次自身

这个 send_message 按“开始”按钮后,功能首先计划运行1000毫秒。它将继续自称,直到 sending_enabled 标志被设置为false stop_sending 功能。

注意,send或receive函数都没有while循环。