python - 睡眠 while 循环而不中断它,用于在 tkinter 中写入新条目
问题描述
在这个 UI 中有 2 个按钮和 1 个输入框。名为“开始循环”和“打印”的按钮。
当我在输入框中写入文本时,按下相关按钮时我应该能够看到它被打印出来。我想要做的是,把那个按钮按下一个while循环。但有趣的部分是尝试每 10 秒打印一个新条目。
当您输入一个条目并按下“开始循环”按钮时,它就会运行。同时用户界面窗口将冻结。无法编写新条目或按“打印”按钮。即使我使用 time.sleep 功能它仍然冻结。它打印旧条目,但我想在每次迭代时写一个新条目。查看代码:
import time
from tkinter import *
class tkin(Frame):
def __init__(self,parent):
Frame.__init__(self, parent)
self.parent = parent
self.UI()
def UI(self):
self.down = StringVar()
self.down_entry = Entry(self, textvariable=self.down)
self.down_entry.grid(row=2, column=0)
self.start_loop_buuton = Button(text="Start Loop", command=self.loop_func)
self.start_loop_buuton.place(x=10,y=40)
self.print_func_button = Button(text="Print ", command=self.pprint)
self.print_func_button.place(x=120,y=40)
self.pack()
def loop_func(self):
start = time.time()
while True:
print("standart print out")
end = time.time()
if (end- start) >10:
time.sleep(10)
self.print_func_button.invoke() ## press print_func_button
start = time.time()
def pprint(self):
print("WHICH PRINT LINE I WANT TO PRINT IN LIKE EVERY 10 SECONDS")
print(self.down.get())
def main():
root = Tk()
tkin(root)
root.geometry("195x100+300+300")
root.mainloop()
main()
任何建议都会很好。提前致谢。
解决方案
这就是我重新定义您的方法的方式:
def loop_func(self):
self.print_func_button.invoke() # press print_func_button
self.after(3000, self.loop_func)
时间以毫秒为单位,所以这将是 3 秒
推荐阅读
- sql - 仪表板中的 KPI 区域指标聚合
- jpa - JPQL - 基于 @ElementCollection 元素中最后更新的时间戳属性进行过滤
- angular - 如何在 COMPONENT 文件中获取 *ngFor 数组的计数
- python - 将列表元素与捕获进行比较:相互比较必须首先发生......元素顺序也很重要
- c++ - 如何以类型安全的方式分配 uint32_t::max 和 uint64_t 的最小值?
- node.js - 在 Node JS 中,在子模块中如何找到主代码的“当前路径”
- reactjs - 反应键 = {row._id} 不起作用
- r - 为什么一个简单的用户定义函数会为不同的变量名给出不同的结果。'c' v/s 'k'
- ios - 由于表格视图中可重复使用的单元格,当它滚动到视图之外时丢失 Youtube 播放器
- sql - Vertica SQL:连接中的“或”会导致速度大幅下降?