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”的标签时。但当按钮再次点击新的文本我想它摧毁旧的文本,但它不工作。如果我在创建文本(注释掉)后立即销毁它,它就可以工作了。你知道吗
是因为我在函数中声明了它吗?如果是这样的话,我怎样才能在函数完成后销毁它呢?你知道吗
(第一个问题,为将来更好的问题提供建议)
如果在类中编写代码,则可以使用类属性存储标签并在以后更新。你知道吗
离开你的代码试图做下面的代码将是类等效的工作。也就是说我认为你应该更新标签而不是销毁它。你知道吗
我的第二个示例将使用
config()
来更新标签,而不必销毁它。我想这可能更适合你的需要。你知道吗相关问题 更多 >
编程相关推荐