如何在Python中从函数外部更改标签文本

时间:2018-04-01 18:48:56

标签: python function tkinter label

我是编程新手,我尝试制作像显示温度一样简单的东西。我无法显示温度,直到我关闭窗口,它进入循环get_outside_temp()但是没有显示任何内容,直到我打破循环然后窗口将被绘制。请参阅下面的代码。

 #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()

2 个答案:

答案 0 :(得分:0)

您的问题不在于访问标签,而是在time.sleep(5)期间您的整个程序没有响应5秒。所以,你已经在内存中更新了标签,但它没有在屏幕上重新绘制,因为根本没有重新绘制。

您无法在GUI程序中睡觉。如果你想要每5秒运行一次,你有三个选择:使用后台线程,手动驱动事件循环,或者只是让GUI在5秒内再次运行你的代码。

最后一个是迄今为止最简单的。在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)

换句话说,不是一个永远运行的函数,一次睡5秒,我们有一个运行得非常快的函数,要求在5秒内再次运行,然后返回事件循环,以便其他东西可以运行(比如重新绘制屏幕,​​响应鼠标等)。

答案 1 :(得分:0)

谢谢,我确实设法让它发挥作用 代替:     root.after(5000,callback = get_outside_temp) 我不得不使用:     root.after(5000,get_outside_temp)