Python Forum

Full Version: Returning Entry as a Label
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I am trying to create a Darts Scoreboard Application using tkinter. I am very new to Python so just learning the ropes at the moment but having some trouble with something which is probably quite a simple issue. I am trying to display an entry as a label, but i am getting the following error :



import tkinter

window = tkinter.Tk()

window.title("Darts Score Board")


def returnEntry(arg=None):
    """Gets the result from Entry and return it to the Label"""

    result = Player1Entry.get()
    resultLabel.config(text=result)


#Player Names
Player1Label = tkinter.Label(window, text = "Enter Player 1 Name:").grid(row = 0)
Player1Entry = tkinter.Entry(window).grid(row = 0, column = 1)
Player2Label = tkinter.Label(window, text = "Enter Player 2 Name:").grid(row=0,column =3)
Player2Entry = tkinter.Entry(window).grid(row=0,column = 4)

#Buttons
submit_players = tkinter.Button(window, text='submit', command=returnEntry).grid(row=0,column=5)

# Create and emplty Label to put the result in
resultLabel = tkinter.Label(window, text = "").grid(row=1,column=0)


window.mainloop()
Would anyone be able to help, any advise is appreciated Naughty

Thanks!
Ash Cool
You have to use a textvariable instead of text for the label
http://effbot.org/tkinterbook/label.htm

v = tkinter.StringVar()
tkinter.Label(master, textvariable=v).pack()

v.set("New Text!")
As mentioned by metulburr, textvariable is another way but not the only way.

The actual problem is that you have no reference to the control itself, the grid method has been called which returns None and this is what is stored in the variables.
Create the labels and pack them separately as shown below to keep a reference to the controls themselves.
import tkinter

window = tkinter.Tk()

window.title("Darts Score Board")


def returnEntry(arg=None):
    """Gets the result from Entry and return it to the Label"""

    result = Player1Entry.get()
    resultLabel.config(text=result)

As mentioned by metulburr using textvariable is another way but not the only way.


# Player Names
Player1Label = tkinter.Label(window, text="Enter Player 1 Name:")
Player1Label.grid(row=0)
Player1Entry = tkinter.Entry(window)
Player1Entry.grid(row=0, column=1)
Player2Label = tkinter.Label(window, text="Enter Player 2 Name:")
Player2Label.grid(row=0, column=3)
Player2Entry = tkinter.Entry(window)
Player2Entry.grid(row=0, column=4)

# Buttons
submit_players = tkinter.Button(window, text='submit', command=returnEntry)
submit_players.grid(row=0, column=5)

# Create and emplty Label to put the result in
resultLabel = tkinter.Label(window, text="")
resultLabel.grid(row=1, column=0)


window.mainloop()