Python Forum
Tkinter: How to assign calculated value to a Label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter: How to assign calculated value to a Label
#4
Post the bare minimum runnable code showing the problem at hand.
Dont use * imports see Namespace flooding with * imports
Create the label once and then change it's text value in the buttons event handler.
Use a class to easily keep a reference to GUI widgets.

See below code
import tkinter as tk


class Main:
    def __init__(self, root):
        self.frame = tk.Frame(root)
        lblCo2 = tk.Label(self.frame, text="CO2 (ppm)")
        lblCo2.grid(row=4, sticky=tk.W)
        self.valCo2 = tk.Label(self.frame, bg="white", width=12)
        self.valCo2.grid(row=4, column=1, sticky=tk.W)

        btnStart = tk.Button(self.frame, text="Start",
                             command=self.on_btn_start)
        btnStart.grid(row=5, column=1, sticky=tk.W)
        self.frame.pack()

    def on_btn_start(self):
        Cmd1 = [0xFE, 0x44, 0x00, 0x08, 0x02, 0x9F, 0x25]
        high = Cmd1[3]
        low = Cmd1[4]
        co2 = (high*256) + low
        self.valCo2['text'] = co2


root = tk.Tk()
main = Main(root)
root.mainloop()
Reply


Messages In This Thread
RE: Tkinter: How to assign calculated value to a Label - by Yoriz - Sep-01-2020, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 759 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,604 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,227 Aug-15-2022, 07:10 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 5,054 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter: Image to Label Maryan 10 5,387 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  Tkinter - How can I extend a label widget? TurboC 2 2,829 Oct-13-2020, 12:15 PM
Last Post: zazas321
  changing tkinter label from thread nanok66 3 7,452 Jun-07-2020, 01:37 AM
Last Post: nanok66
  [Tkinter] tkinter How to pass label fiilename to another module? johnjh 0 2,028 Apr-17-2020, 11:34 PM
Last Post: johnjh
  Make Label Text background (default color) transparent using tkinter in python barry76 1 23,968 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Spacing Between two labels are very far and Top label is not in Center using Tkinter barry76 2 7,144 Jul-30-2019, 10:49 AM
Last Post: wuf

Forum Jump:

User Panel Messages

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