【发布时间】:2021-12-18 04:40:11
【问题描述】:
我正在尝试制作一个不断读取并在Tkinter GUI 上更新它的脚本,并使用一个简单的按钮进行刷新,但我似乎无法使其工作。
我已经使用while True 循环读取文件,但这不起作用,它只读取一次,然后它可能以root.mainloop() 行结束。有什么解决办法吗?
到目前为止我写的代码:
from tkinter import *
root = Tk()
root.geometry("400x400")
while True:
with open('door1.txt', 'r') as f:
f_contents = f.read()
f.close
def something():
global my_label
my_label.config(text=f_contents)
my_label = Label(root, text="this is my first text")
my_label.pack(pady=10)
my_buttton = Button(root, text="C",command=something)
my_buttton.pack(pady=10)
root.mainloop()
【问题讨论】: