gpt4 book ai didi

python - Tkinter 屏幕删除,具体

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:35 30 4
gpt4 key购买 nike

您好,我是论坛新手。

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

请不要留下任何仇恨!

谢谢

是的,我喜欢新的段落:)

最佳答案

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

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

当然,您也可以重置小部件的值,而不是删除它们或将它们从屏幕上删除。如何执行此操作取决于小部件的类型,但所有小部件都可以重新配置而不会被破坏。

关于python - Tkinter 屏幕删除,具体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19597857/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com