如何删除以前的标签阴影

时间:2018-02-22 07:17:24

标签: python tkinter

我的代码应该像这样工作:

  1. 当我点击开始按钮时,它会显示一组不断变化的四个单词。

  2. 目前我点击停止按钮,程序将立即停止,屏幕上的四个单词将是幸运字样。

  3. 我的问题是:由于单词变化非常快,新单词不会完全替换旧单词,旧单词的阴影仍然可见。如何删除阴影?

  4. enter image description here

    import tkinter
    import random
    
    words='''ant baboon badger bat bear beaver camel cat clam cobra cougar 
    coyote crow deer dog
    donkey duck eagle ferret fox frog goat goose hawk lion lizardllama mole 
    monkey moose mouse
    mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino 
    salmon seal shark
    sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle 
    weasel whale wolf
    wombat zebra
    '''.split()
    a=[]
    label=dict.fromkeys(['a','b','c','d','e'],tkinter.Label)
    after_id=None
    root=tkinter.Tk()
    frame=tkinter.Frame(root)
    frame.pack()
    
    
    
    def first_price():
        global after_id
        a.clear()
        while len(a)<5:
            rand=random.randint(0,len(words)-1)
            if rand not in a:
               a.append(rand)
    
       for i in range(0,len(a)):
           label[i] = tkinter.Label(frame, text=words[a[i]])
           label[i].grid(row=i,)
       after_id = frame.after(100,first_price)
    
    
    
    
    def stop():
       global after_id
       if after_id:
           print(after_id)
           print(a)
           frame.after_cancel(after_id)
           after_id=None
    
    
    button_1= tkinter.Button(frame,text='Start',bg='red',fg='yellow',command=first_price)
    button_2 =tkinter.Button(frame,text="Stop",bg='yellow',fg='red',command=stop)
    
    button_1.grid(row=6,)
    button_2.grid(row=7)
    
    
    root.title('Luck Draw')
    root.mainloop()
    

1 个答案:

答案 0 :(得分:0)

在tkinter中你有忘记方法,如果你使用.grid你使用.grid_forget(),我不会很好,但在.pack是.pack_forget()我想和.place don&# 39;有一个方法(我猜)。我在代码中做了一些更改,但这可能会有效

a=[]
labelList = []
after_id=None

root=tkinter.Tk()
frame=tkinter.Frame(root)
frame.pack()

def first_price():
    global after_id, labelList
    a = []
    while len(a)<5:
        rand=random.randint(0,len(words)-1)
        if rand not in a:
            a.append(rand)
    if labelList != 0: # this is for no delete the firsts words
        for i in labelList: # run a variable on the label list
            i.grid_forget() # call the .grid_forget() of the labels of list
    labelList = [] # return to nothing
    for i in range(0,len(a)):
        label = tkinter.Label(frame, text=words[a[i]]) # create your label
        label.grid(row=i,) # place label
        labelList.append(label) #add the label object in the label list
    after_id = frame.after(100,first_price)