How would I modify/add text to a tkinter.Label?
Asked Answered
D

3

6

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: 11111

Here 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()
Delorasdelorenzo answered 5/8, 2016 at 15:24 Comment(2)
Just delete all the previous label text and then add new text.Vaginate
Use a variable to control the value of the label text.Vaginate
P
8

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.

Personalism answered 5/8, 2016 at 15:35 Comment(5)
I wouldn't say "should use a StringVar; can would be more accurate . You can do it with or without a StringVar.Freezedry
Thank you, works perfectly. What do you suggest I change my imports to for this specific project? (Instead of: from tkinter import *)Delorasdelorenzo
Good point, @BryanOakley. I just figured it's a bit easier to use StringVar & friends. :)Personalism
@jzbakos: I recommend that you use import tkinter as tk, as shown in my code. And you'll need to use tk.Label etc instead of Label. 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.Personalism
Using 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.Freezedry
F
1

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)
Freezedry answered 5/8, 2016 at 15:55 Comment(0)
V
0

Is this what you're looking for:

from tkinter import *
root = Tk()

var = StringVar()

def f1():
    var.set(" ")
    var.set("1")

def f2():
    var.set(" ")
    var.set("2")

label = Label(root, textvariable=var)
label.pack()

button1 = Button(root, text="One", command=f1)
button1.pack()

button2 = Button(root, text="Two", command=f2)
button2.pack()

?

Vaginate answered 5/8, 2016 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.