By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

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

P: 7
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:

Expand|Select|Wrap|Line Numbers
  1. from time import *
  2. from math import *
  3. from re import *
  4. from tkinter import *
  5.  
  6.  
  7.  
  8. def getWord():
  9.     rootMot = Toplevel(root)
  10.     motTexte = Entry(rootMot)
  11.     motTexte.pack()
  12.     motTexte.delete(0, END)
  13.     motTexte.insert(0, "Enter your word here")
  14.     boutonMot = Button(rootMot, text="Submit", width=10, command=draw)
  15.     boutonMot.pack()
  16.  
  17. def draw():
  18.     rootMot.destroy()
  19.     drawing = Tk()
  20.     gameDraw = Canvas(drawing, width=200, height=100)
  21.     gameDraw.pack()
  22.     gameDraw.create_line(0, 0, 200, 100)
  23.     gameDraw.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
  24.     gameDraw.create_rectangle(50, 25, 150, 75, fill="blue")
  25.     mainloop()
  26.  
  27.  
  28.  
  29. def exemple():
  30.     print("Word")
  31.  
  32.  
  33. #######################################################################
  34.  
  35. root = Tk()
  36. menu = Menu(root)
  37. root.config(menu=menu)
  38.  
  39.  
  40. gameOptions = Menu(menu)
  41. menu.add_cascade(label="Game", menu=gameOptions)
  42. gameOptions.add_command(label="New Game", command=getWord)
  43. gameOptions.add_command(label="Options...", command=getWord)
  44. gameOptions.add_separator()
  45. gameOptions.add_command(label="Exit", command=getWord)
  46.  
  47.  
  48. helpOptions = Menu(menu)
  49. menu.add_cascade(label="Help", menu=helpOptions)
  50. helpOptions.add_command(label="How to play...", command=getWord)
  51. helpOptions.add_command(label="About...", command=getWord)
  52. mainloop()
Thank you

PS: I'm french so sorry if you don't understand some variables :)
Nov 8 '14 #1

✓ answered by bvdet

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.
Expand|Select|Wrap|Line Numbers
  1. def getWord():
  2.     rootMot = Toplevel(root)
  3.     motTexte = Entry(rootMot)
  4.     motTexte.pack()
  5.     motTexte.delete(0, END)
  6.     motTexte.insert(0, "Enter your word here")
  7.     boutonMot = Button(rootMot, text="Submit", width=10,
  8.                        command=lambda: draw(rootMot))
  9.     boutonMot.pack()
  10.     motTexte.focus_set()
  11.     motTexte.selection_range(0, END)
  12.  
  13. def draw(widget):
  14.     widget.destroy()
  15.     drawing = Tk()
  16.     gameDraw = Canvas(drawing, width=200, height=100)
  17.     gameDraw.pack()
  18.     gameDraw.create_line(0, 0, 200, 100)
  19.     gameDraw.create_line(0, 100, 200, 0,
  20.                          fill="red", dash=(4, 4))
  21.     gameDraw.create_rectangle(50, 25, 150, 75,
  22.                               fill="blue")
  23.     mainloop()

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. def getWord():
  2.     rootMot = Toplevel(root)
  3.     motTexte = Entry(rootMot)
  4.     motTexte.pack()
  5.     motTexte.delete(0, END)
  6.     motTexte.insert(0, "Enter your word here")
  7.     boutonMot = Button(rootMot, text="Submit", width=10,
  8.                        command=lambda: draw(rootMot))
  9.     boutonMot.pack()
  10.     motTexte.focus_set()
  11.     motTexte.selection_range(0, END)
  12.  
  13. def draw(widget):
  14.     widget.destroy()
  15.     drawing = Tk()
  16.     gameDraw = Canvas(drawing, width=200, height=100)
  17.     gameDraw.pack()
  18.     gameDraw.create_line(0, 0, 200, 100)
  19.     gameDraw.create_line(0, 100, 200, 0,
  20.                          fill="red", dash=(4, 4))
  21.     gameDraw.create_rectangle(50, 25, 150, 75,
  22.                               fill="blue")
  23.     mainloop()
Nov 8 '14 #2

P: 7
Thank you very much :D
Nov 9 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.