1

您好,我是论坛的新手。

我有很多使用 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 中删除所有小部件,或者删除特定小部件。

请不要留下任何仇恨!

谢谢

是的,我喜欢新段落:)

4

1 回答 1

1

您可以销毁一个小部件,这会将其从屏幕上移除。只需调用destroy小部件上的方法。当您销毁一个小部件时,它的所有子小部件也将被销毁。因此,例如,如果您想销毁一个框架中的所有小部件,您只需销毁该框架。

如果您只是想将它们从屏幕上移除而不是销毁它们,您可以调用grid_remove、或小部件grid_forget,具体取决于您将它们放在屏幕上的方式。pack_forgetplace_forget

当然,您也可以重置小部件的值,而不是删除它们或从屏幕上移除它们。你如何做到这一点取决于小部件的类型,但所有小部件都可以重新配置而不会被破坏。

于 2013-10-25T20:08:47.920 回答