tkinter:如何使用 after 方法 [英] tkinter: how to use after method

查看:129
本文介绍了tkinter:如何使用 after 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是 python 新手,我的 gui 使用 tkinter.我在使用after"方法时遇到问题.目标是每 5 秒随机出现一个字母.

Hey I am new to python and am using tkinter for my gui. I am having trouble using the "after" method. The goal is to make a random letter appear every 5 seconds.

这是我的代码:

import random
import time
from tkinter import *


root = Tk()

w = Label(root, text="GAME")
w.pack()

frame = Frame(root, width=300, height=300)
frame.pack()

L1 = Label(root, text="User Name")
L1.pack(side=LEFT)
E1 = Entry(root, bd =5)
E1.pack(side=LEFT)


tiles_letter = ['a', 'b', 'c', 'd', 'e']


while len(tiles_letter) > 0:
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    frame.after(500)
    tiles_letter.remove(rand)  # remove that tile from list of tiles

root.mainloop()

有人可以帮助我---问题肯定是frame.after(500):我不确定使用frame"是否正确,我也不知道 500 后面是什么参数.

can someone please help me --- the problem is definitely frame.after(500): i'm not sure if it is correct to use "frame" and I don't know what which argument follows the 500.

谢谢

推荐答案

你需要给出一个在延时后被调用的函数作为 after:

You need to give a function to be called after the time delay as the second argument to after:

after(delay_ms, callback=None, *args)

注册在给定时间后调用的闹钟回调.

Registers an alarm callback that is called after a given time.

所以你真正想做的是:

tiles_letter = ['a', 'b', 'c', 'd', 'e']

def add_letter():
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    root.after(500, add_letter)
    tiles_letter.remove(rand)  # remove that tile from list of tiles


root.after(0, add_letter)  # add_letter will run as soon as the mainloop starts.
root.mainloop()

您还需要通过在回调函数内重复调用 after 来安排再次调用的函数,因为 after 只执行一次给定的函数.这也在文档中注明:

You also need to schedule the function to be called again by repeating the call to after inside the callback function, since after only executes the given function once. This is also noted in the documentation:

每次调用此方法只调用一次回调.保持调用回调,需要在里面重新注册回调本身

The callback is only called once for each call to this method. To keep calling the callback, you need to reregister the callback inside itself

请注意,一旦您用完 tiles_letter 中的所有条目,您的示例就会抛出异常,因此您需要更改逻辑以按您想要的方式处理这种情况.最简单的方法是在 add_letter 的开头添加一个检查以确保列表不为空,如果是,则只需 return :

Note that your example will throw an exception as soon as you've exhausted all the entries in tiles_letter, so you need to change your logic to handle that case whichever way you want. The simplest thing would be to add a check at the beginning of add_letter to make sure the list isn't empty, and just return if it is:

def add_letter():
    if not tiles_letter:
        return
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    root.after(500, add_letter)
    tiles_letter.remove(rand)  # remove that tile from list of tiles


现场演示:repl.it

这篇关于tkinter:如何使用 after 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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