Tkinter试图在函数完成后销毁函数中定义的标签

2024-09-28 18:48:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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”的标签时。但当按钮再次点击新的文本我想它摧毁旧的文本,但它不工作。如果我在创建文本(注释掉)后立即销毁它,它就可以工作了。你知道吗

是因为我在函数中声明了它吗?如果是这样的话,我怎样才能在函数完成后销毁它呢?你知道吗

(第一个问题,为将来更好的问题提供建议)


Tags: textnonecolumnwindowlabelgridgreyrow
1条回答
网友
1楼 · 发布于 2024-09-28 18:48:46

如果在类中编写代码,则可以使用类属性存储标签并在以后更新。你知道吗

离开你的代码试图做下面的代码将是类等效的工作。也就是说我认为你应该更新标签而不是销毁它。你知道吗

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()

相关问题 更多 >