Tkinter GUI开发过程学习-界面按钮与threading线程结合实现多线程运行

tkinter开发的简单GUI中添加按钮会有一个问题:按下按钮之后会先运行按钮绑定的回调函数,在运行完回调函数之前GUI界面是什么响应都没有的——不能拖动窗口,不能点击其他交互,而且不会实时显示回调函数中状态,只有运行完才能返回所有值。

举个例子:回调函数中不断读取20行数据,每读取一行就在前端窗口text组件中不删除上一行并显示一行。

只使用tkinter写是这样的:

import tkinter as tk
import threading
import time

# 创建主窗口
root = tk.Tk()
root.title("实时显示文件内容")

# 创建一个文本框,用于显示文件内容
text_area = tk.Text(root, height=20, width=50)
text_area.pack(pady=20)


def update_text_area():
    """不断读取文件内容并更新到文本框中"""
    with open("GatheringData.dat", "r") as file:
        while True:
            line = file.readline()
            if line:
                text_area.insert(tk.END, line)  # 在文本框中插入新行
                text_area.see(tk.END)  # 滚动到最底部
            else:
                time.sleep(1)  # 暂停一秒钟再尝试读取下一行


# 创建一个按钮,点击后启动文件读取
start_button = tk.Button(root, text="开始读取文件", command=lambda: target=update_text_area())
start_button.pack(pady=10)

# 运行主循环
root.mainloop()

其中button代码是这样的:

start_button = tk.Button(root, text="开始读取文件", command=lambda: start_reading())
start_button.pack(pady=10)

那么前端text显示的是运行完回调函数后读完了20行数据的总的内容,而不是在前端实时显示回调函数运行过程,即一行一行显示出来。

查询之后,找到了解决方法:使用threading多线程库来解决

threading.Thread 是 Python 的 threading 模块提供的一个类,用于创建和管理线程。线程允许在程序中并发地执行代码,适合需要同时处理多个任务的场景。

button代码修改为:

start_button = tk.Button(root, text="开始读取文件", command=lambda: 
                threading.Thread(target=update_text_area).start())
start_button.pack(pady=10)

完成我预期的效果。

注:如果  .start()不加 "() "则 ‘command:’ 后一定不要加lambda,否则button没有响应。

       如果  .start()加了 "() "则 ‘command:’ 后一定要加lambda,否则不按buttion,窗口也会直接执行回调函数。

总的代码如下:

import tkinter as tk
import threading
import time

# 创建主窗口
root = tk.Tk()
root.title("实时显示文件内容")

# 创建一个文本框,用于显示文件内容
text_area = tk.Text(root, height=20, width=50)
text_area.pack(pady=20)


def update_text_area():
    """不断读取文件内容并更新到文本框中"""
    with open("GatheringData.dat", "r") as file:
        while True:
            line = file.readline()
            if line:
                text_area.insert(tk.END, line)  # 在文本框中插入新行
                text_area.see(tk.END)  # 滚动到最底部
            else:
                time.sleep(1)  # 暂停一秒钟再尝试读取下一行


# 创建一个按钮,点击后启动文件读取
start_button = tk.Button(root, text="开始读取文件", command=lambda: threading.Thread(target=update_text_area).start())
start_button.pack(pady=10)

# 运行主循环
root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值