忘记标签,然后在tkinter

时间:2017-10-09 23:09:53

标签: python-3.x tkinter

我尝试pack_forget()但是我不知道将它放在“最终”功能中

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()需要在函数内部(至少据我所知),但是,我不知道如何在按下每个按钮后忘记响应标签并让它输出一个新的回应。

1 个答案:

答案 0 :(得分: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()