Python Forum
[Tkinter] Trying to display a variable in a label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Trying to display a variable in a label
#1
When I run this the answer prints into the python console but not in the label:

import tkinter as tk

HEIGHT = 300
WIDTH = 300
answer = ""



def calculate():
    global answer
    answer = str(eval(entry.get()))
    print(answer)
    return answer


root = tk.Tk()
# Creates canvas
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

# Background
# background_image = tk.PhotoImage(file="landscape.png")
# background_label = tk.Label(root, image=background_image)
# background_label.place(relwidth=1, relheight=1)

# Creates frame and shows it
frame = tk.Frame(root, bg="gray")
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)

# Creates label and shows it
label = tk.Label(frame, text="CALCULATOR V0.1")
label.pack(side="bottom")
# Creates entry and shows it
entry = tk.Entry(frame, bd=5)
entry.place(relx=0, rely=0, relwidth=1, relheight=0.2)

# Creates button and shows it
button = tk.Button(frame, text="CALCULATE", bg="white", fg="blue", command=calculate)
button.pack(side="bottom", fill="both")
# Answer label  shows and places it, I made it text=answer why isnt it showing it on the label?
answer_label = tk.Label(frame, text=answer)
answer_label.place(rely=0.5, relwidth=1)

root.mainloop()
I get no errors from this.
Reply
#2
(Oct-31-2019, 03:44 PM)Jordansonatina Wrote: answer = str(eval(entry.get()))
Because you bind the variable answer to a new value, which is unrelated to the old value that was assigned to answer. In order to have a bind-able variable that can change, you should use something like StringVar: https://effbot.org/tkinterbook/variable.htm

Here's a short example showing IntVar with a label showing it's value:
>>> import tkinter as tk
>>> root = tk.Tk()
>>> def increment(var):
...     var.set(var.get() + 1)
...
>>> answer = tk.IntVar()
>>> tk.Button(root, text="Click Me!", command=lambda: increment(answer)).pack()
>>> tk.Label(root, textvariable=answer).pack()
>>> root.mainloop()
Reply
#3
Thank you very much! I got it to work and added a clear function so you can clear the answer variable. Pretty sure I understand it now. Thanks again for answering so quickly :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  update text variable on label with keypress knoxvilles_joker 3 4,841 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  Printing Variable into the label ardvci 5 3,036 Mar-19-2020, 09:35 PM
Last Post: joe_momma
  [Tkinter] HOW TO: Set [Label].config() by variable name reference? gazoxtapod 4 4,883 Apr-17-2019, 09:57 PM
Last Post: gazoxtapod
  Display and update the label text which display the serial value jenkins43 5 8,995 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 3,836 Jan-05-2019, 01:48 PM
Last Post: wuf
  [Tkinter] can't update label to include variable foxtreat 2 3,605 Dec-01-2018, 07:16 AM
Last Post: jfong
  printing option menu variable in label in Tkinter SmokerX 1 6,591 Jan-18-2018, 07:36 PM
Last Post: SmokerX
  [Tkinter] Trying to display a changing variable AnonymousNobody 9 7,005 Aug-10-2017, 03:52 AM
Last Post: AnonymousNobody

Forum Jump:

User Panel Messages

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