Python Forum
[Tkinter] Returning Entry as a Label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Returning Entry as a Label
#1
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
Reply
#2
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!")
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Returning always the last image into Label Lucas_Ribeiro 1 662 May-08-2020, 05:56 PM
Last Post: deanhystad
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 590 Apr-28-2020, 06:50 AM
Last Post: iconit
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 1,628 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 1,458 Mar-18-2019, 05:36 PM
Last Post: woooee
  [WxPython] bind label and entry text with return key metulburr 1 1,464 Aug-14-2018, 10:02 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020