2017-10-09 75 views
0

我试图做pack_forget()后忘记了该标签的标签,然后输出新的价值,但是我不知道该把它放在“最后”功能内在Tkinter的每一个按钮推

userinput = Entry() 
userinput.pack(side=TOP) 
text = str(userinput) 


def final(): 
    choices = ["It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", "As I see it, yes", 
     "Most likely", "Outlook good", "Yes", 'Signs point to yes', "Reply hazy try again", "Ask again later", "Better not tell you now", 
     "Cannot predict now", "Concentrate and ask again", "Dont count on it", "My reply is no", "My sources say no", "Outlook not good", 'Very doubful'] 

    if len(text) > 0 : 
     response = Label(root, text = random.choice(choices), bg = "snow") 
     response.pack(side=TOP) 




decision = Button(root, text = "Go", command = final) 
decision.configure(font=(28)) 
decision.pack(side=TOP) 

我相信, pack.forget()需要在函数内部(至少据我所知),但是,我不知道如何在每次按下按钮后忘记响应标签并让它输出新的响应。

+0

你'而TRUE'循环永远不会因为'text'出口将永远不会改变里面的循环。 –

+0

@BryanOakley我删除了循环,问题仍然存在 –

回答

0

你实际上并不需要使用.pack_forget()您可以使用标签上.configure()代替,像下面:

from tkinter import * 
import random 

class App: 
    def __init__(self, root): 
     self.root = root 
     self.choices = ["It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Yes", 'Signs point to yes', "Reply hazy try again", "Ask again later", "Better not tell you now","Cannot predict now", "Concentrate and ask again", "Dont count on it", "My reply is no", "My sources say no", "Outlook not good", 'Very doubful'] 
     self.label = Label(self.root) 
     self.button = Button(self.root, text="Ok", command=self.shuffle) 
     self.label.pack() 
     self.button.pack() 
    def shuffle(self): 
     self.label.configure(text=random.choice(self.choices)) 

root = Tk() 
App(root) 
root.mainloop()