用 tkinter 创建一个主循环? [英] Create a main loop with tkinter?

查看:66
本文介绍了用 tkinter 创建一个主循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

from tkinter import *

root = Tk()
while True:
    print('hello')
    root.update()

root.mainloop()

这里的主循环是:

while True:
    print('hello')
    root.update()

但我不确定这是最好的方法(如果我想输入一些东西,这不起作用)

But I'm not sure thats the best way to do that (this doesn't work if i want to input something)

然后我尝试了这个:

from tkinter import *
from threading imoport Thread
import time

root = Tk()

text = Label()
text.pack()

def main():
    while True:
        text[text] = str(time.time())

thread = Thread(target=main)
thread.start()

root.mainloop()

但是我已经意识到这并没有我预期的那么快.所以问题是:创建主循环的最佳方法是什么?

But as i've realised that doesn't work as fast as i expected. So the question is: Whats the best way to create a main loop?

推荐答案

Tkinter 为其提供了一个强大的工具,它被称为 之后.它旨在作为同步睡眠命令,但可以通过调用自身在主循环内构建一个循环.

Tkinter provide a powerfull tool for it and it is called after. It is intended as synchronous sleep command but can build a loop inside the mainloop by calling itself.

after ,一个内置的 Tcl 命令,管理脚本的调度未来评估,也可用作同步睡眠命令.

after , a built-in Tcl command, manages the scheduling of scripts for future evaluation, and also functions as a synchronous sleep command.

import tkinter as tk #import tkinter
import datetime #import datetime for our clock

def tick(): #function to update the clock
    showed_time = '' #current showed time
    current_time = datetime.datetime.now().strftime("%H:%M:%S") #real time
    if showed_time != current_time: #if the showed time is not the real time
        showed_time = current_time #update the variable to compare it next time again
        clock.configure(text=current_time) #update the label with the current time
    clock.after(1000, tick) #call yourself in 1000ms (1sec.) again to update the clock
    return None

root=tk.Tk()

clock = tk.Label(root)
clock.pack()
tick()

root.mainloop()

在上面的脚本中,我们构建了一个数字时钟并使用了 after 方法.after 方法只不过是一个间隔,在该间隔结束时我们希望发生一些事情.

In the above script we had built a digital clock and get in touch with the after method. The after method is nothing but an interval and on the end of that interval we want that something happen.

要了解有关此基本小部件方法的更多信息[点击]

To learn more about this basic widget method [click]

after(delay_ms, callback=None, args)

这个方法注册了一个回调函数 ,它将在一个给定的毫秒数.Tkinter 仅保证 回调将不会在此之前被调用;如果系统繁忙,实际延迟可能会更长.

This method registers a callback function that will be called after a given number of milliseconds. Tkinter only guarantees that the callback will not be called earlier than that; if the system is busy, the actual delay may be much longer.

import tkinter as tk 
import datetime 

def tick():
    showed_time = '' 
    current_time = datetime.datetime.now().strftime("%H:%M:%S")
    if showed_time != current_time:
        showed_time = current_time
        clock.configure(text=current_time)
    global alarm #make sure the alarm is known
    alarm = clock.after(1000, tick)#assign the alarm to a variable
    return None
def stop():
    stop.after_cancel(alarm) #cancel alarm
    

root=tk.Tk()

clock = tk.Label(root)
clock.pack()
stop = tk.Button(root, text='Stop it!', command=stop)
stop.pack()
tick()


root.mainloop()

这里我们有相同的代码,但可以使用 tkinter 的 after_cancel 方法取消我们的循环.您不需要在类中全局化警报.self.alarm = self.clock.after(...) 工作正常.

Here we have the same code but with the ability to cancel our loop with the after_cancel method of tkinter. You dont need to global the alarm inside a class. self.alarm = self.clock.after(...) works fine.

after_cancel(id)

取消闹钟回调.

id

警报标识符.

为什么线程在编码框架工作中不是一个好的选择.

这篇关于用 tkinter 创建一个主循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆