我怎样才能让按钮文字的变化,从文字到一个空的名字有延迟? [英] How can I make the button text change, from text to an empty name with a delay?

查看:272
本文介绍了我怎样才能让按钮文字的变化,从文字到一个空的名字有延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Tkinter库的图形用户界面。基本上我创建使用for循环,每一个都具有随机数作为其文本4个按钮。现在,我想实现一个方法,它允许按钮上的文字显示为一个数字,然后变成什么在第二个左右(这是后下一个按钮会做同样的过程)时卡住了。因此该方法允许每个按钮闪烁它的数目并移动到下一个(直到所有的按钮闪蒸它们的数量为一次)。

这是code,我走到这一步

 从Tkinter的进口*
从tkinter.messagebox进口showinfo
从随机进口randint高清set_colors(一):
    如果== 0:
        回归红
    ELIF一个== 1:
        返回绿色
    ELIF一个== 2:
        回归蓝
    ELIF一个== 3:
        回归黄高清set_random():
    random_int = 0
    random_int = randint(0,100)
    回报random_intLARGE_FONT =(宋体,20)
颜色= 0根= TK()
帧=帧(根)
root.title(测试)
root.geometry(200×200)
root.resizable(0,0)Grid.rowconfigure(根,0,重量= 1)
Grid.columnconfigure(根,0,重量= 1)
frame.grid(行= 0,列= 0,粘= N + S + E + W)
格=帧(帧)
grid.grid(粘= N + S + E + W,列= 0,行= 7,columnspan = 2)
Grid.rowconfigure(帧,7,重量= 1)
Grid.columnconfigure(帧,0,重量= 1)在范围X(2):
    对于在范围Y(2):
        rand_no = set_random()
        BTN = rand_no
        BTN =按钮(帧,文字= BTN,BG = set_colors(颜色),字体= LARGE_FONT)
        btn.grid(列= X,排= Y,粘= N + S + E + W)
        色彩+ = 1在范围X(2):
  Grid.columnconfigure(帧中,x,重量= 1)对于在范围Y(2):
  Grid.rowconfigure(帧,Y,重量= 1)root.mainloop()

在output.So的图片为止,我想通了,我需要导入时库,并使用sleep()方法来获得所需的延迟。但我仍然需要:

允许面板与按键加载(没有任何数字)
然后按钮开始闪动头号之后对方,直到所有的按钮闪烁次数为一次。


解决方案

记忆就是一切!

 #!的/ usr / bin中/蟒蛇
进口OS
进口螺纹,时间
进口随机进口数学尝试:
    迪尔斯= os.path.dirname(OS .__文件__)低()
    如果python2,在显示目录:
        从Tkinter的进口*
    在显示目录ELIFpython3:
        从Tkinter的进口*
除例外,E:打印ELARGE_FONT =(宋体,20)
颜色= 0根= TK()
帧=帧(根)
root.title(很难记忆游戏!)
root.geometry(200×200)
root.resizable(0,0)Grid.rowconfigure(根,0,重量= 1)
Grid.columnconfigure(根,0,重量= 1)
frame.grid(行= 0,列= 0,粘= N + S + E + W)
格=帧(帧)
grid.grid(粘= N + S + E + W,列= 0,行= 8,columnspan = 2)
Grid.rowconfigure(帧,8,重量= 1)
Grid.columnconfigure(帧,0,重量= 1)类_zo:通
ZO = _zo()
zo.foo = []
zo.pick =无
zo.lq = []
zo.a = {}
zo.level = 0
zo.lev_list = [4,9,16,25,36,49,64,81]
zo.miss = 0
高清get_num(NUM):
    H =排序(zo.a.keys())
    P = zo.foo.index(NUM)
    如果num在zo.lq和num == zo.pick:
        zo.a [%0.2d_%D%(P,NUM)。配置(文= NUM​​,状态=已禁用)        如果len(zo.lq)GT; 1:
            zo.lq.remove(NUM)
            zo.pick =(random.choice(zo.lq))
            zo.a [my_lab。配置(文=,其中为%d?%zo.pick)
        ELIF LEN(zo.lq)== 1:
            zo.lq = []
            zo.foo = []
            zo.pick =无
            zo.level = zo.level +1
            对于s在zo.a.keys():
                zo.a [S] .destroy()
            zo.a = {}
            do_cube(zo.lev_list [zo.level])
    其他:
        zo.miss + = 1
        。zo.a [my_lab]配置(文=为(%D)重试(小姐:%S)%(zo.pick,zo.miss))高清timot(胡):
    time.sleep(2)
    N =排序(zo.a.keys())    对于s在N:
        如果没有s.startswith(我):
            Y = zo.foo [n.index(S)]
            zo.a [S]的.config(文= Y)
            time.sleep(1)
            zo.a [S]的.config(文字=)
    zo.pick = random.choice(zo.foo)
    对于N中的记者:zo.a [J]的.config(州=正常)
    zo.a [my_lab。配置(文=,其中为%d?%zo.pick)高清do_cube(很多):
    BX = INT(的Math.sqrt(很多))
    W =(200 +(200 *(* zo.level 0.15)))
    root.geometry(%DX%D + 400 + 200%(W,W +(20 +(5 * zo.level))))
    颜色=橙色,红色,绿色,蓝,黄,棕色,宝蓝,粉红色
    zo.foo = random.sample(范围(100),许多)
    zo.lq = [E为电子在zo.foo]    在范围(BX)X:
        在范围(BX)Y:
            IND =(X * BX)+ Y
            rand_no = zo.foo [IND]            butn =按钮(帧,BG =颜色[X],命令=拉姆达rand_no = rand_no:get_num(rand_no),字体= LARGE_FONT,状态=已禁用)
            butn.grid(列= X,排= Y,粘= N + S + E + W)            zo.a [%0.2d_%D%(IND,rand_no)] = butn    在范围(BX)X:
        Grid.columnconfigure(帧中,x,重量= 1)
    在范围(BX)Y:
        Grid.rowconfigure(帧,Y,重量= 1)    zo.a [my_lab] = LABEL(根,文本=准备好了!)
    zo.a [my_lab]。电网(列= 0,行= BX + 1,粘= N + S + E + W,columnspan = BX)
    thread.start_new_thread(timot,(无,))
do_cube(zo.lev_list [zo.level])
root.mainloop()

I'm using tkinter library for the GUI. Basically I'm creating 4 buttons using a for loop, each having a random number as their text. Now I got stuck when trying to implement a method which allows the button text to appear as a number and then change to nothing in a second or so (After this is done the next button would do the same process). So the method would allow each button to flash it's number and moves on to the next (Until all the buttons flashed their numbers for one time).

This is the code which I got so far

from tkinter import *
from tkinter.messagebox import showinfo
from random import randint

def set_colors(a):
    if a == 0:
        return "red"
    elif a == 1:
        return "green"
    elif a == 2:
        return "blue"
    elif a == 3:
        return "yellow"

def set_random():
    random_int = 0
    random_int = randint(0, 100)
    return random_int

LARGE_FONT = ("Verdana",20)
color = 0

root = Tk()
frame = Frame(root)
root.title("Test")
root.geometry("200x200")
root.resizable(0, 0)

Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0, sticky=N + S + E + W)
grid = Frame(frame)
grid.grid(sticky=N + S + E + W, column=0, row=7, columnspan=2)
Grid.rowconfigure(frame, 7, weight=1)
Grid.columnconfigure(frame, 0, weight=1)



for x in range(2):
    for y in range(2):  
        rand_no = set_random()
        btn = rand_no
        btn = Button(frame, text=btn, bg=set_colors(color) , font=LARGE_FONT)
        btn.grid(column=x, row=y, sticky=N + S + E + W)
        color += 1

for x in range(2):
  Grid.columnconfigure(frame, x, weight=1)

for y in range(2):
  Grid.rowconfigure(frame, y, weight=1)

root.mainloop()

A picture of the output.So far I figured that I need to import the time library and use the sleep() method to get the delay needed. But I still need to:

Allow the panel to load with the buttons (Without any numbers) Then the buttons start to flash the number one after each other until all buttons flashed the number for one time.

解决方案

Memory is everything !

#!/usr/bin/python
import os
import thread,time
import random

import math

try:
    dirs = os.path.dirname(os.__file__).lower()
    if "python2" in dirs:
        from Tkinter import *
    elif "python3" in dirs:
        from tkinter import *
except Exception,e : print e

LARGE_FONT = ("Verdana",20)
color = 0

root = Tk()
frame = Frame(root)
root.title("Very Hard Memory Game !")
root.geometry("200x200")
root.resizable(0, 0)

Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0, sticky=N + S + E + W)
grid = Frame(frame)
grid.grid(sticky=N + S + E + W, column=0, row=8, columnspan=2)
Grid.rowconfigure(frame, 8, weight=1)
Grid.columnconfigure(frame, 0, weight=1)

class _zo : pass
zo = _zo()
zo.foo = []
zo.pick = None
zo.lq =[]
zo.a = {}
zo.level = 0
zo.lev_list = [4,9,16,25,36,49,64,81]
zo.miss = 0


def get_num(num):
    h = sorted(zo.a.keys())
    p = zo.foo.index(num)
    if num in zo.lq and num == zo.pick :
        zo.a["%0.2d_%d"%(p,num)].config(text=num, state="disabled")

        if len(zo.lq) > 1 :
            zo.lq.remove(num)
            zo.pick = ( random.choice(zo.lq))
            zo.a["my_lab"].config(text= "where is %d ?" % zo.pick)
        elif len(zo.lq) == 1:
            zo.lq = []
            zo.foo = []
            zo.pick = None
            zo.level = zo.level +1
            for s in zo.a.keys() :
                zo.a[s].destroy()
            zo.a = {}
            do_cube(zo.lev_list[zo.level])


    else:
        zo.miss += 1
        zo.a["my_lab"].config(text="Try again for (%d)!(Miss:%s)"%(zo.pick,zo.miss))    





def timot(hu):
    time.sleep(2)
    n = sorted(zo.a.keys())   

    for s in n :
        if not s.startswith("my"):
            y = zo.foo[n.index(s)]
            zo.a[s].config(text=y)
            time.sleep(1)
            zo.a[s].config(text="")
    zo.pick = random.choice(zo.foo)
    for j in n: zo.a[j].config(state="normal")
    zo.a["my_lab"].config(text="where is %d ?" %zo.pick)

def do_cube(many):
    bx = int(math.sqrt(many))
    w = (200+(200*(zo.level*0.15)))
    root.geometry("%dx%d+400+200" %(w,w+(20+(5*zo.level))))


    color = "orange","red","green","blue","yellow","brown","dodgerblue","pink"
    zo.foo  = random.sample(range(100),many)
    zo.lq = [e for e in zo.foo]

    for x in range(bx):
        for y in range(bx):
            ind = (x*bx) + y  
            rand_no = zo.foo[ind]

            butn = Button(frame, bg=color[x], command= lambda rand_no=rand_no:get_num(rand_no), font=LARGE_FONT,state="disabled")
            butn.grid(column=x, row=y, sticky=N + S + E + W)

            zo.a["%0.2d_%d"%(ind,rand_no)] = butn

    for x in range(bx):
        Grid.columnconfigure(frame, x, weight=1)
    for y in range(bx):
        Grid.rowconfigure(frame, y, weight=1)

    zo.a["my_lab"] = Label(root,text="ready !")
    zo.a["my_lab"].grid(column=0, row=bx+1, sticky=N + S + E + W,columnspan = bx)


    thread.start_new_thread(timot,(None,))


do_cube(zo.lev_list[zo.level])


root.mainloop()

这篇关于我怎样才能让按钮文字的变化,从文字到一个空的名字有延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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