你好,我是新来的论坛。
我有很多使用python的经验但没有使用tkinter。
这是我的代码:
from tkinter import *
def Done():
celEntry = celTempVar.get()
print(str(celEntry))
f = open("Name", "w")
f.write(str(celEntry))
f.close()
def Yes():
celEntry = Entry (mainframe, width = 10, bd = 5, textvariable = celTempVar)
celEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 )
titleLabel = Label (mainframe, text = "Enter Name", font = ("Arial", 12, "bold"), justify = LEFT)
titleLabel.grid(row = 2, column = 1, columnspan = 3, pady = 10, padx = 20)
DoneButton = Button (mainframe, text = "Done", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "black", activeforeground="blue", command = Done)
DoneButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
###MAIN###
root = Tk()
root.title("Text Adv")
mainframe = Frame(root)
mainframe.grid()
celTempVar = StringVar()
celTempVar.set("")
titleLabel = Label (mainframe, text = "Text adventure", font = ("Arial", 20, "bold"), justify = CENTER)
titleLabel.grid(row = 1, column = 1, columnspan = 3, pady = 10, padx = 20)
f = open("Name", "r")
Name = f.read()
if Name == "":
celEntry = Entry (mainframe, width = 10, bd = 5, textvariable = celTempVar)
celEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 )
titleLabel = Label (mainframe, text = "Enter Name", font = ("Arial", 12, "bold"), justify = LEFT)
titleLabel.grid(row = 2, column = 1, columnspan = 3, pady = 10, padx = 20)
DoneButton = Button (mainframe, text = "Done", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "black", activeforeground="blue", command = Done)
DoneButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
else:
titleLabel = Label (mainframe, text = "Start new game?", font = ("Arial", 12, "bold"), justify = LEFT)
titleLabel.grid(row = 2, column = 1, columnspan = 3, pady = 10, padx = 20)
DoneButton = Button (mainframe, text = "Yes", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "black", activeforeground="blue", command = Yes)
DoneButton.grid(row = 3, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
DoneButton = Button (mainframe, text = "No", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "black", activeforeground="blue", command = Done)
DoneButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
root.mainloop()
所以我想要的是在运行Yes()时擦除屏幕。
简单但有其他答案,我真的不明白。
如果你可以在我的代码中实现一个很棒的擦除功能。
Atm我只是试验其他人的代码,这就是为什么变量名称就像CeltoFar之类的东西。
如果您不理解我的意思是擦除,如删除gui中的所有小部件,或删除特定的小部件。
请不要留下任何仇恨!
由于
是的,我喜欢新的段落:)
答案 0 :(得分:1)
您可以销毁小部件,这会将其从屏幕上删除。只需调用窗口小部件上的destroy
方法即可。销毁窗口小部件时,其所有子窗口小部件也将被销毁。因此,例如,如果要销毁帧中的所有小部件,则只需要销毁该帧。
如果您只想将其从屏幕上删除而不是销毁它们,则可以调用grid_remove
,grid_forget
,pack_forget
或place_forget
小部件,具体取决于方式你把它们放在屏幕上。
当然,您也可以重置窗口小部件的值,而不是删除它们或从屏幕上删除它们。如何执行此操作取决于窗口小部件的类型,但可以重新配置所有窗口小部件而不会被销毁。