破坏函数Tkin

2024-10-02 22:25:22 发布

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

在这个程序中,我试图在一个新功能开始时销毁所有屏幕上的小部件,并立即在屏幕上重新创建它们,以复制出现的新页面。当我点击游戏菜单中的“开始”按钮时,我已经用了一次销毁功能来“改变页面”。在

但是,当再次尝试销毁所有页面时,单击画布时出现错误:

_tkinter.TclError: bad window path name ".49314384"

呈现。在

from tkinter import *
    import tkinter

window = tkinter.Tk()                           #Here is where we set up the window and it's aesthetics,
window.title("BINARY-SUMZ!!!")                  #here we give the window a name,
window.geometry("1000x800")                     #here we give the window a size,
window.wm_iconbitmap('flower3.ico')             #and here we give the window an icon.

def Destroy():              #this function destroys any widgets on the current page.
    for widget in window.winfo_children():
        widget.destroy()

def StartButton():                                                                      #This function starts the game after being clicked on.

print ("Game started from beginning.")
Intro()                                                                  #This function starts the game after being clicked on.



def Menu():   #Creating a menu function
    SumsTitle = tkinter.Label(window, text="BINARY-SUMS!!!",                    #Here is where we create the title for the menu screen, we give it a name,               
                        fg = "light Green",                                 #a foreground (text) color
                        bg = "tomato",                                    #a backgorund color
                        font = "'Bauhaus 93 bold italic")
SumsTitle.pack()            #and the text is given a font.

StartButtonWid = tkinter.Button(window, text = "Start Learning!!!",
                                fg = "tomato",
                                command= (StartButton))
StartButtonWid.pack()             #Setting up the button for the start of the game.

TitleCanvas = tkinter.Canvas(window, bg = "light blue" ,
                             height = 1000,
                             width = 1000)
TitleCanvas.pack()

def Intro():
    Destroy()          #This function works fine
    SumsTitle = tkinter.Label(window, text="Welcome!!!",                    #Here is where we create the title for the menu screen, we give it a name,               
                        fg = "light green",                                 #a foreground (text) color
                        bg = "tomato",                                    #a backgorund color
                        height = 1,
                        width = 14,
                        font = "'Bauhaus 93 bold italic")
SumsTitle.pack()
Intro1 = tkinter.Label(window, text='Welcome to BINARY-SUMS!!! The fun, interactive binary learning game! in this game we will be learning about language based topics', 
                               font= "30")
Intro1.pack()
Intro2 = tkinter.Label(window, text='that will be vital to know in your AS computing or computer science exams. Please click the screen to continue.',
                               font= "30")
Intro2.pack()

IntroCanvas = tkinter.Canvas(window, bg = "light blue" ,
                             height = 1500,
                             width = 1000)

IntroCanvas.bind("<Button-1>",  Activ1())
IntroCanvas.pack()


def Activ1():
    Destroy()     #this function crashes.

if __name__ == "__main__":
    Menu()





tkinter.mainloop()

Tags: thetextnamegameforistkinterdef
1条回答
网友
1楼 · 发布于 2024-10-02 22:25:22
IntroCanvas.bind("<Button-1>",  Activ1())
                                      ^^
IntroCanvas.pack()

你在上面的行中得到了错误。在

添加括号意味着,“在编译器到达该函数时立即调用该函数”。在Activ1被调用后,它调用Destroy(),这会破坏IntroCanvas,然后您试图pack销毁小部件。所以你得到了那个错误。在

作为调试说明,如果您看到这样的错误消息,大多数情况下这是因为您正在尝试对已销毁的小部件/对象执行某些操作,因此应该查找销毁调用。在

对于解决方案,
您应该删除括号并向Activ1添加一个参数。在

^{pr2}$

相关问题 更多 >