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

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

    #1

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

    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:

    Code:
    from time import *
    from math import *
    from re import *
    from tkinter import *
    
    
    
    def getWord():
        rootMot = Toplevel(root)
        motTexte = Entry(rootMot)
        motTexte.get()
        motTexte.pack()
        getWord.motFinal = motTexte.get()
        boutonMot = Button(rootMot, text="Submit", width=10,
                           command=lambda: draw(rootMot))
        boutonMot.pack()
       
    
    def draw(widget):
        print(getWord.motFinal)
        widget.destroy()
        gameDraw = Canvas(root, width=200, height=100)
        gameDraw.pack()
        
    
    
    #Create the menu
    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
    At the time of assignment to getWord.motFina l, there is no text in motTexte. You can add motTexte as an argument in the button callback and call get() in draw().
    Code:
    def getWord():
        rootMot = Toplevel(root)
        motTexte = Entry(rootMot)
        motTexte.get()
        motTexte.pack()
        boutonMot = Button(rootMot, text="Submit", width=10,
                           command=lambda: draw(rootMot, motTexte))
        boutonMot.pack()
    
    def draw(top, widget):
        print(widget.get())
        top.destroy()
        gameDraw = Canvas(root, width=200, height=100)
        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).

    Comment

    Working...