2016-09-22 1 views
0

Dans ce programme, j'essaye de détruire tous les widgets à l'écran au début d'une nouvelle fonction et de les recréer instantanément à l'écran pour reproduire une nouvelle page apparaissant. J'ai déjà utilisé la fonction détruire une fois pour "changer de page" en cliquant sur le bouton de démarrage dans le menu du jeu qui a bien fonctionné.Détruire la fonction Tkinter

Cependant lors d'une tentative de détruire toutes les pages une seconde fois en cliquant sur la toile à l'erreur:

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

est présenté.

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

Répondre

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

Vous obtenez l'erreur dans les lignes ci-dessus. Cela signifie que "appelle qui fonctionne dès que le compilateur y atteint". Après Activ1 est appelé, il appelle Destroy() qui détruit IntroCanvas alors vous essayez de pack widget détruit. D'où vous obtenez cette erreur. Comme une note de débogage, si vous voyez un message d'erreur comme celui-ci, la plupart du temps c'est parce que vous essayez de faire une action sur détruit widget/objet alors vous devriez rechercher vos appels destructeurs. Pour la solution,
Vous devez supprimer les parenthèses et ajouter un argument à Activ1.

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

def Activ1(event): 
+0

Comment suggérez-vous de corriger ce problème? @Lafexlos –

+1

@RobBibb Édité. Aucune idée pourquoi j'ai oublié d'ajouter une partie de la solution. – Lafexlos

+0

Merci beaucoup je t'aime. –