UpdateLabel()
You forgot to pass the temp value to the UpdateLabel
function. It should be:
update_label_1(temp)
[edit]
Corrected the function name.
[/edit]
[edit version="2"]
I have reviewed your code and the issue is slightly more complicated. The reason that the label never gets update is because the
while
loop is blocking the GUI and so preventing any changes. There are two ways to resolve this:
1. Run the temperature capture code in a background thread, thus allowing the GUI to update the window.
2. Move all the code into the GUI class so it runs itself.
Option 2 is the simplest to implement, and the following code can form the basis of what you need.
import time
import random
import tkinter as tk
class TempView(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.label1_text = tk.StringVar()
self.label1_text.set("initial value")
self.label1=tk.Label(self.master, textvariable=self.label1_text,
fg='blue', font=("Arial", 18, "bold"),
background='#CDC5D9')
self.label1.grid(row=0,column=0)
self.master.grid_columnconfigure(1, minsize=100)
tk.Button(self.master, text="Quit", command=self.master.destroy,
bg="red").grid(row=1, column=0)
self.getTemp()
def getTemp(self):
temp = str(random.randint(10, 100))
self.update(temp)
self.master.after(2000, self.getTemp)
def update(self, temp):
self.label1_text.set(temp)
print(F"The temperature is {temp}")
random.seed()
root = tk.Tk()
app = TempView(master=root)
app.mainloop()
I have used the
random
class to capture some values, just as a sample. You need to change the
getTemp
method to get the actual values from your device.
[/edit]