How do I update a Label with a numerical value with python?

Fenjalien

I have tried a lot of different things but nothing has worked so far.

I am trying to a Label(l1) increase or decrease by one each time the "+1"(b1) or "-1"(b2) button is pressed. But at the moment the Label just sits there and does nothing.

Here is the code in it's basic form without trying to update the label:

from tkinter import *
root = Tk()

p1 = 0

l1 = Label(root, textvariable = p1)
f1 = Frame(root)
b1 = Button(root, text = "+1", command = p1 + 1)
b2 = Button(root, text = "-1", command = p1 - 1)

l1.grid(row=0, column=0)
f1.grid(row=1, column=0)

b1.pack(side="top")
b2.pack(side="top")

root.mainloop() 
j_4321

There are several problems in your code:

  • The command option of a Button takes a function and p1 +/- 1 is not a function.
  • The textvariable option of a Label takes a Tkinter variable, usually a StringVar, but here an IntVar is more appropriate.
  • You are mixing pack and grid in root, which is not possible.

To do what you want, you nedd to define two functions, one that increases the value of the IntVar by 1 and the other that decreases it. Then, pass the functions to the command option of the buttons and pass the IntVar to the textvariable argument of the label:

import tkinter as tk

def increase():
    p1.set(p1.get() + 1)

def decrease():
    p1.set(p1.get() - 1)

root = tk.Tk()

p1 = tk.IntVar(root, 0)

l1 = tk.Label(root, textvariable=p1)

b1 = tk.Button(root, text="+1", command=increase)
b2 = tk.Button(root, text="-1", command=decrease)

l1.pack()
b1.pack()
b2.pack()

root.mainloop() 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I update a Label when a TextBox value changes?

How do I update a variable value in python?

How do I update a text label in SwiftUI?

pyQt: How do I update a label?

How do I call elements of an array as their index numerical value

How do I place a slider value in label?

How do I gather user numerical input into a list in Python?

How do I update python list without overwriting value?

How do i refresh the label in python with Tkinter

How do I update the text in label widget within the for loop?

How do I update a Label from within a BackgroundWorker thread?

How do I use UIActionSheet choice to update a dynamic label?

How do I update a label after clicking a Button in Tkinter?

How to do numerical value comparison in Teradata SQL?

How do I update a value in a dataframe in a loop?

How do I format the numerical labels in a legend?

How do I flip numerical data in R?

How to get the highest numerical value in an array in python

How do I animate the numerical value using a Google Charts animation? Specifically the Gauage

How do I get an enum element's numerical value using libclang?

How do I get the label from the value models.IntegerChoices

How do I increment a label value with a button press in Swift

How do I write a Prometheus query that returns the value of a label?

How do I label the nth observation of a specific value in a pandas dataframe?

How do you check if input is numerical in Python?

How do I make python take multiple horizontal lines of numerical input and output the same lines?

How do I update a Python package?

How do I keep label text side by side with label value in tkinter

How do I update a table with old value + the new value?