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

How to print the Entry() of a function in another functiion?

P: 7
Hi, I'm trying to print the string of: Entry "motTexte" who is located in getWord(), in draw(). But everytime in write something in the entry, it prints an empty line in the Python IDLE.

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.get()
  12.     motTexte.pack()
  13.     getWord.motFinal = motTexte.get()
  14.     boutonMot = Button(rootMot, text="Submit", width=10,
  15.                        command=lambda: draw(rootMot))
  16.     boutonMot.pack()
  17.  
  18.  
  19. def draw(widget):
  20.     print(getWord.motFinal)
  21.     widget.destroy()
  22.     gameDraw = Canvas(root, width=200, height=100)
  23.     gameDraw.pack()
  24.  
  25.  
  26.  
  27. #Create the menu
  28. root = Tk()
  29. menu = Menu(root)
  30. root.config(menu=menu)
  31.  
  32.  
  33. gameOptions = Menu(menu)
  34. menu.add_cascade(label="Game", menu=gameOptions)
  35. gameOptions.add_command(label="New Game", command=getWord)
  36. gameOptions.add_command(label="Options...", command=getWord)
  37. gameOptions.add_separator()
  38. gameOptions.add_command(label="Exit", command=getWord)
  39.  
  40.  
  41. helpOptions = Menu(menu)
  42. menu.add_cascade(label="Help", menu=helpOptions)
  43. helpOptions.add_command(label="How to play...", command=getWord)
  44. helpOptions.add_command(label="About...", command=getWord)
  45. mainloop()
Thank you

PS: I'm french so sorry if you don't understand some variables :)
Nov 8 '14 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
At the time of assignment to getWord.motFinal, there is no text in motTexte. You can add motTexte as an argument in the button callback and call get() in draw().
Expand|Select|Wrap|Line Numbers
  1. def getWord():
  2.     rootMot = Toplevel(root)
  3.     motTexte = Entry(rootMot)
  4.     motTexte.get()
  5.     motTexte.pack()
  6.     boutonMot = Button(rootMot, text="Submit", width=10,
  7.                        command=lambda: draw(rootMot, motTexte))
  8.     boutonMot.pack()
  9.  
  10. def draw(top, widget):
  11.     print(widget.get())
  12.     top.destroy()
  13.     gameDraw = Canvas(root, width=200, height=100)
  14.     gameDraw.pack()
You should consider encompassing your dialogs in a class object where individual elements are created as instance objects and can be accessed by attribute reference (as in self.motTexte).
Nov 9 '14 #2

Post your reply

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