Tkinter尝试在函数完成后销毁函数内定义的Label

时间:2018-05-17 14:11:16

标签: python tkinter

from tkinter import *

def spaces(int1,int2):
    if int1 == 1:
        return(int2*"\n")
    else:
        return(int2*" ")
def submitButton():
    subButton_text = subButton.get()
    try:
        informationText.destroy()
    except UnboundLocalError:
        print("an error has occured.")
        print("Attempting to run with the error.")
        pass
    informationText = Label(window, text=subButton_text, bg = "grey",
      fg = "white", font = "none 12 bold")
    informationText.grid(row=4,column=0,sticky=W)
    #informationText.destroy()
    #return informationText
def exit_Button():
    window.destroy()

window = Tk()
window.title("Learning tkinter")
window.configure(background="grey")
subButton = StringVar()



#pic1 = PhotoImage(file="test.png")
#Label(window, image=pic1, bg = "grey").grid(row = 0, column = 0, sticky=W)

Label(window, text="Please enter a value", bg = "grey",
      fg = "white", font = "none 12 bold").grid(row=0,column=0,sticky=W)

Entry(window, width=50, bg="white",textvariable=subButton).grid(row=1,column=0,stick=W)
subButton.set("default value")

Button(window, text="Submit", width=10,
       command = submitButton).grid(row=2,column=0, sticky=W)

Label (window, text="\nInformation:", bg="grey", fg="white",
       font="none 12 bold").grid(row=3,column=0, sticky=W)

Button (window, text="Save\nand exit", width=8,
        command=exit_Button).grid(row=0,column=2,sticky=NW)

Label(window, text=spaces(1,10), bg = "grey",
      fg = "white", font = "none 12 bold").grid(row=100,column=0,sticky=W)
Label(window, text=spaces(2,20), bg = "grey",
      fg = "white", font = "none 12 bold").grid(row=0,column=1,sticky=W)
window.mainloop()

这是我的简单tkinter程序的代码。当" submitButton"它创建了一个名为" informationText"的标签。但是当再次使用新文本单击按钮时,我希望它能够销毁旧文本,但它不起作用。如果我在创建文本后立即销毁文本(已注释掉),则可以正常工作。

是因为我在一个函数中声明了它吗?如果是这样,我怎么能在功能完成后将其销毁?

(首先询问,将来对于更好的问题表示赞赏)

1 个答案:

答案 0 :(得分:0)

如果您在类中编写代码,您将能够使用类属性来存储标签并在以后更新。

关闭你的代码试图做下面的代码将是等效的类。这就是说我认为你应该更新标签而不是破坏标签。

import tkinter as tk


class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.information_text = None
        self.title("Learning tkinter")
        self.configure(background="grey")
        self.subButton = tk.StringVar()

        tk.Label(self, text="Please enter a value", bg = "grey", fg = "white", font = "none 12 bold").grid(row=0,column=0,sticky="w")
        tk.Entry(self, width=50, bg="white",textvariable=self.subButton).grid(row=1,column=0,stick="w")

        self.subButton.set("default value")

        tk.Button(self, text="Submit", width=10, command = self.submitButton).grid(row=2,column=0, sticky="w")
        tk.Label (self, text="\nInformation:", bg="grey", fg="white", font="none 12 bold").grid(row=3,column=0, sticky="w")
        tk.Button (self, text="Save\nand exit", width=8, command=self.exit_Button).grid(row=0,column=2,sticky="nw")
        tk.Label(self, text=self.spaces(1,10), bg = "grey", fg = "white", font = "none 12 bold").grid(row=100,column=0,sticky="w")
        tk.Label(self, text=self.spaces(2,20), bg = "grey", fg = "white", font = "none 12 bold").grid(row=0,column=1,sticky="w")

    def spaces(self, int1, int2):
        if int1 == 1:
            return(int2*"\n")
        else:
            return(int2*" ")
    def submitButton(self):

        try:
            self.information_text.destroy()
            self.information_text = None
        except:
            print("an error has occured.")
            print("Attempting to run with the error.")

        self.information_text = tk.Label(self, text=self.subButton.get(), bg = "grey", fg = "white", font = "none 12 bold")
        self.information_text.grid(row=4,column=0,sticky="w")

    def exit_Button(self):
        self.destroy()


if __name__ == "__main__":
    app = MyApp()
    app.mainloop()

我的第二个例子将使用config()来更新标签,而不是必须销毁它。我认为这可能会更好地满足您的需求。

import tkinter as tk


class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)


        self.title("Learning tkinter")
        self.configure(background="grey")
        self.subButton = tk.StringVar()

        tk.Label(self, text="Please enter a value", bg = "grey", fg = "white", font = "none 12 bold").grid(row=0,column=0,sticky="w")
        tk.Entry(self, width=50, bg="white",textvariable=self.subButton).grid(row=1,column=0,stick="w")

        self.subButton.set("default value")

        tk.Button(self, text="Submit", width=10, command = self.submitButton).grid(row=2,column=0, sticky="w")
        tk.Label (self, text="\nInformation:", bg="grey", fg="white", font="none 12 bold").grid(row=3,column=0, sticky="w")
        tk.Button (self, text="Save\nand exit", width=8, command=self.exit_Button).grid(row=0,column=2,sticky="nw")
        tk.Label(self, text=self.spaces(1,10), bg = "grey", fg = "white", font = "none 12 bold").grid(row=100,column=0,sticky="w")
        tk.Label(self, text=self.spaces(2,20), bg = "grey", fg = "white", font = "none 12 bold").grid(row=0,column=1,sticky="w")

        self.information_text = tk.Label(self, text="", bg = "grey", fg = "white", font = "none 12 bold")
        self.information_text.grid(row=4,column=0,sticky="w")

    def spaces(self, int1, int2):
        if int1 == 1:
            return(int2*"\n")
        else:
            return(int2*" ")
    def submitButton(self):
        self.information_text.config(text=self.subButton.get())

    def exit_Button(self):
        self.destroy()


if __name__ == "__main__":
    app = MyApp()
    app.mainloop()