How would I modify/add text to a tkinter.Label?
Solution 1
You should use a StringVar for this. And your callback needs to get the current contents of the StringVar, modify it, and use the modified string to set the new value of the StringVar. Like this:
import tkinter as tk
window = tk.Tk()
# Creating main label
display_text = tk.StringVar()
display = tk.Label(window, textvariable=display_text)
display.grid(row=0, columnspan=3)
def add_one():
s = display_text.get()
s += '1'
display_text.set(s)
one = tk.Button(window, text="1", height=10, width=10, command=add_one)
one.grid(row=1, column=0)
window.mainloop()
BTW, you should try to make your program a little more compact by using for
loops to create and lay out your buttons, in accordance with the DRY principle.
Also, it's not a good idea to use from tkinter import *
. It imports over 130 names into your namespace, making it easy to create name collisions if you accidentally use a Tkinter name for one of your own variables or functions.
Solution 2
You can define add_one
like the following, to first get the existing value and then append a new value to it:
def add_one():
current_value = display.cget("text")
new_value = current_value + "1"
display.config(text=new_value)
jzbakos
Updated on June 17, 2022Comments
-
jzbakos almost 2 years
I am in the process of learning basic Python. I am currently attempting to create a simple calculator program that only has addition and subtraction. I have one issue though. I am not sure how I would add text to my Python label upon button press. Right now, upon pressing the '1' button, my program will change the display label to the text "1". However, I want my program to add text, not set it.
For example, if I press 'button 1' 5 times, it currently will reset the label text 5 times and will result with a single 1. I want it to add the number to the label upon press, not replace.
Current Result after pressing button 5 times: 1
Requested result after pressing button 5 times: 11111Here is my current code for the program. If anything is unclear, just ask; thanks.
from tkinter import * window = Tk() # Creating main label display = Label(window, text="") display.grid(row=0, columnspan=3) def add_one(): display.config(text='1') # Creating all number buttons one = Button(window, text="1", height=10, width=10, command=add_one) two = Button(window, text="2", height=10, width=10) three = Button(window, text="3", height=10, width=10) four = Button(window, text="4", height=10, width=10) five = Button(window, text="5", height=10, width=10) six = Button(window, text="6", height=10, width=10) seven = Button(window, text="7", height=10, width=10) eight = Button(window, text="8", height=10, width=10) nine = Button(window, text="9", height=10, width=10) zero = Button(window, text="0", height=10, width=10) # Placing all number buttons one.grid(row=1, column=0) two.grid(row=1, column=1) three.grid(row=1, column=2) four.grid(row=2, column=0) five.grid(row=2, column=1) six.grid(row=2, column=2) seven.grid(row=3, column=0) eight.grid(row=3, column=1) nine.grid(row=3, column=2) # Creating all other buttons add = Button(window, text="+", height=10, width=10) subtract = Button(window, text="-", height=10, width=10) equal = Button(window, text="=", height=10, width=10) # Placing all other buttons add.grid(row=4, column=0) subtract.grid(row=4, column=1) equal.grid(row=4, column=2) window.mainloop()
-
Russell Smith almost 8 yearsI wouldn't say "should use a
StringVar
; can would be more accurate . You can do it with or without aStringVar
. -
jzbakos almost 8 yearsThank you, works perfectly. What do you suggest I change my imports to for this specific project? (Instead of: from tkinter import *)
-
PM 2Ring almost 8 yearsGood point, @BryanOakley. I just figured it's a bit easier to use StringVar & friends. :)
-
PM 2Ring almost 8 years@jzbakos: I recommend that you use
import tkinter as tk
, as shown in my code. And you'll need to usetk.Label
etc instead ofLabel
. It's a tiny bit more typing, but it makes it easier to read your code, and it keeps the namespace clean instead of polluting it with all those Tkinter names. -
Russell Smith almost 8 yearsUsing
StringVar
is fine, it's just that not strictly necessary and it adds an extra object that has to be tracked. It's arguably a tiny bit easier to use when setting a value, so you just have to decide which you prefer -- one extra object to manage, or one extra line of code to change the value. For me, more objects == more complexity.