2016-09-22 163 views
0

在此程序中,我试图在新功能开始时销毁所有屏幕小部件,并立即在屏幕上重新创建它们以复制出现的新页面。点击游戏菜单中可以正常工作的开始按钮,我已经使用了破坏功能来“更换页面”。销毁功能Tkinter

但是当试图在画布上的错误上点击时销毁所有页面的第二次:

_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() 

回答

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

你得到上面线路错误。

添加括号意味着“调用只要编译器到达那里就会起作用”。 Activ1被调用后,它调用Destroy()销毁IntroCanvas然后你试图pack销毁的部件。因此你得到了那个错误。

作为一个调试的注意,如果你看到这样的错误消息,大多数时候是因为你试图做破坏控件/对象有所行动,所以你应该找你的电话摧毁。

对于解决方案,
您应该删除括号并将参数添加到Activ1

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

def Activ1(event): 
+0

你会如何解决这个问题? @Lafexlos –

+1

@RobBibb编辑。不知道为什么我忘了添加解决方案部分。 – Lafexlos

+0

非常感谢你我爱你。 –