2016-09-22 79 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

非常感謝你我愛你。 –