[英]How to change a labels text from outside the function in Python
I'm new to programming and I'm trying to make something as simple as displaying the temperature. 我是编程的新手,我正在尝试使显示温度一样简单。 I can't get the temperature to display until I close the window, it goes into the loop get_outside_temp() but doesn't display anything until I break the loop then the window will be drawn.
在关闭窗口之前,我无法显示温度,它进入了循环get_outside_temp(),但是直到我打破循环,然后窗口才会绘制,它什么也不显示。 See code below.
请参见下面的代码。
#Import needed files
import tkinter as tk
import sys
import time
#define some variables
green = '#2BF720'
blue='#0CCAF0'
#blue = '#427BED'
font10 = "-family Newspaper -size 18 -weight bold -slant roman -underline 0 -overstrike 0"
font2 = "-family Newspaper -size 36 -weight bold -slant roman -underline 0 -overstrike 0"
#get outside temp and display it
def get_outside_temp():
while True:
tfile = open("/sys/bus/w1/devices/28-0416b113e1ff/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
outside_temp = float(temperaturedata[2:])
outside_temp = round(outside_temp / 1000 ,1)
print(outside_temp) #so I can see it's working
#this is wherre I'm trying to display the temperature in the window.
outside_temp_label.config(text = outside_temp)
'''
outside_temp_label = tk.Label(outside_frame, text=outside_temp, bg="Black", fg='White', font=font2)
outside_temp_label.grid(padx=140, pady=75)
'''
time.sleep(5)
#create main window
root = tk.Tk()
root.geometry("800x480")
root.configure(bg="black")
root.title("Go Nad Go IV")
#create the frame for the outside
outside_frame = tk.Frame(root, height=230, width = 390, bg="black",
relief="groove", highlightcolor = blue, highlightbackground=blue, highlightthickness = 2)
outside_frame.grid(row=0, column=1, padx=5, pady=5)
outside_frame.grid_propagate(False)
outside_label = tk.Label(outside_frame, text="Outside Temperature", bg="Black", fg=blue, font=font10)
outside_label.grid(column=0,row=0)
#get the outside temp
tfile = open("/sys/bus/w1/devices/28-0416b113e1ff/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
outside_temp = float(temperaturedata[2:])
outside_temp = round(outside_temp / 1000 ,1)
#display the temp
outside_temp_label = tk.Label(outside_frame, text=outside_temp, bg="Black", fg='White', font=font2)
outside_temp_label.grid(padx=140, pady=75)
get_outside_temp()
root.mainloop()
Your problem isn't with accessing the label, it's that your entire program is unresponsive for 5 seconds during that time.sleep(5)
. 您的问题不在于访问标签,而是在这段时间内整个程序在5秒钟内无响应
time.sleep(5)
。 So, you've updated the label in memory, but it doesn't redrawn on the screen, because nothing at all is getting redrawn. 因此,您已经更新了内存中的标签,但是它不会在屏幕上重绘,因为根本没有重绘标签。
You can't sleep in a GUI program. 您无法在GUI程序中入睡。 If you want something to run every 5 seconds, you have three choices: use a background thread, drive the event loop manually, or just ask the GUI to run your code again in 5 seconds.
如果希望每5秒运行一次,则有以下三种选择:使用后台线程,手动驱动事件循环或仅要求GUI在5秒内再次运行代码。
The last one is by far the simplest. 到目前为止,最后一个是最简单的。 In tkinter, it's spelled
after
, and you use it like this: 在tkinter中,它的拼写是
after
,您可以像这样使用它:
def get_outside_temp():
# No while True loop here!
tfile = open("/sys/bus/w1/devices/28-0416b113e1ff/w1_slave")
text = tfile.read()
tfile.close()
# all your other existing code, except the sleep
root.after(5000, callback=get_outside_temp)
In other words, instead of a function that runs forever, sleeping for 5 seconds at a time, we have a function that runs really quickly, asks to get run again in 5 seconds, and then returns to the event loop so other stuff can run (like redrawing the screen, responding to the mouse, etc.). 换句话说,不是永久运行一个函数,一次休眠5秒钟,而是一个真正运行很快的函数,要求在5秒钟内再次运行,然后返回事件循环,以便其他东西可以运行(例如重画屏幕,响应鼠标等)。
Thanks, I did manage to get it to work. 谢谢,我确实设法使它起作用。
Instead of: root.after(5000, callback=get_outside_temp) I had to use: root.after(5000, get_outside_temp) 我必须使用:root.after(5000,get_outside_temp)而不是:root.after(5000,callback = get_outside_temp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.