How to .destroy() a window by using another function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marcozeus
    New Member
    • Nov 2014
    • 7

    How to .destroy() a window by using another function?

    I want to delete the window "rootMot" by running the function "draw" when the button "boutonMot" is pressed. But this error keep showing up:
    " File "C:\Users\Admin \Desktop\Codes Python\Bonhomme pendu.py", line 18, in draw
    rootMot.destroy ()
    NameError: global name 'rootMot' is not defined "

    Here the code:

    Code:
    from time import *
    from math import *
    from re import *
    from tkinter import *
    
    
    
    def getWord():
        rootMot = Toplevel(root)
        motTexte = Entry(rootMot)
        motTexte.pack()
        motTexte.delete(0, END)
        motTexte.insert(0, "Enter your word here")
        boutonMot = Button(rootMot, text="Submit", width=10, command=draw)
        boutonMot.pack()
    
    def draw():
        rootMot.destroy()
        drawing = Tk()
        gameDraw = Canvas(drawing, width=200, height=100)
        gameDraw.pack()
        gameDraw.create_line(0, 0, 200, 100)
        gameDraw.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
        gameDraw.create_rectangle(50, 25, 150, 75, fill="blue")
        mainloop()
    
    
        
    def exemple():
        print("Word")
    
    
    #######################################################################
    
    root = Tk()
    menu = Menu(root)
    root.config(menu=menu)
    
    
    gameOptions = Menu(menu)
    menu.add_cascade(label="Game", menu=gameOptions)
    gameOptions.add_command(label="New Game", command=getWord)
    gameOptions.add_command(label="Options...", command=getWord)
    gameOptions.add_separator()
    gameOptions.add_command(label="Exit", command=getWord)
    
    
    helpOptions = Menu(menu)
    menu.add_cascade(label="Help", menu=helpOptions)
    helpOptions.add_command(label="How to play...", command=getWord)
    helpOptions.add_command(label="About...", command=getWord)
    mainloop()
    Thank you

    PS: I'm french so sorry if you don't understand some variables :)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    One way is to assign the callback as a lambda and pass the widget as an argument. I would also suggest setting focus to the entry widget and selecting the text so the user only has to start typing the word.
    Code:
    def getWord():
        rootMot = Toplevel(root)
        motTexte = Entry(rootMot)
        motTexte.pack()
        motTexte.delete(0, END)
        motTexte.insert(0, "Enter your word here")
        boutonMot = Button(rootMot, text="Submit", width=10,
                           command=lambda: draw(rootMot))
        boutonMot.pack()
        motTexte.focus_set()
        motTexte.selection_range(0, END)
     
    def draw(widget):
        widget.destroy()
        drawing = Tk()
        gameDraw = Canvas(drawing, width=200, height=100)
        gameDraw.pack()
        gameDraw.create_line(0, 0, 200, 100)
        gameDraw.create_line(0, 100, 200, 0,
                             fill="red", dash=(4, 4))
        gameDraw.create_rectangle(50, 25, 150, 75,
                                  fill="blue")
        mainloop()

    Comment

    • Marcozeus
      New Member
      • Nov 2014
      • 7

      #3
      Thank you very much :D

      Comment

      Working...