Python tkinter函数冻结其他函数

Python tkinter函数冻结其他函数,python,python-3.x,function,tkinter,freeze,Python,Python 3.x,Function,Tkinter,Freeze,我是python新手,对tkinter有点问题。 是的,我知道如何使用谷歌,但我还没有找到解决方案。也许是因为我真的不知道该怎么找,也不知道该找什么,也许是因为我的英语不好 我有3个功能 勾选() refreshT()#刷新温度(从3个文件读取) refreshW()#刷新天气小部件(图像) 他们在200米、5米和30米后自称 问题是,如果调用Refresh,时间在2秒内不会更新 我读过一些关于线程的文章,但我不懂。 所以,我的问题是,我如何给一个特定的函数一个更高的优先级,或者做任何其他事

我是python新手,对tkinter有点问题。
是的,我知道如何使用谷歌,但我还没有找到解决方案。也许是因为我真的不知道该怎么找,也不知道该找什么,也许是因为我的英语不好

我有3个功能

  • 勾选()
  • refreshT()
    #刷新温度(从3个文件读取)
  • refreshW()
    #刷新天气小部件(图像)
他们在200米、5米和30米后自称

问题是,如果调用Refresh,时间在2秒内不会更新

我读过一些关于线程的文章,但我不懂。 所以,我的问题是,我如何给一个特定的函数一个更高的优先级,或者做任何其他事情,无论发生什么情况,它都会继续运行

我不期望完成代码,只是一些关于一些模块的信息,或者是我想要的

如果相关的话,我会在RASPI3B上使用raspbian

下面是代码中有趣的部分:

#Python 3.5.3:

#(...)
def tick():
    try:                           #tests if there is an old time to compare
        timeOld                    #if not, set it to none
    except NameError:
        timeOld = None
    timeNew = getTime(1)           #a module ive written, returning the time 
                                   #in a specific order
    if timeOld != timeNew:         #compare (this is to avoid running the
                                   #function to often, better for performance
        clock.config(text=timeNew) #display the new time
        timeOld = timeNew          #set the old time to the actual time
    clock.after(200, tick)         #call itself after 200ms

def refreshT():                        #refreshing temperatures
    tempCpu.config(text=(round(float(open('/sys/class/thermal/thermal_zone0/temp').read())/1000, 1),'°C'))
    tempIns.config(text=(round(float(open('/sys/bus/w1/devices/28-0417a312a0ff/w1_slave').read().split('t=')[1])/1000, 1),'°C'))
    tempOut.config(text=(round(float(open('/sys/bus/w1/devices/28-0517a27018ff/w1_slave').read().split('t=')[1])/1000, 1),'°C'))
    tempCpu.after(5000, refreshT)  #call itself after 5 sec

def refreshW(refresh=True):
    if refresh == True:
        weather_raw = createImg(Ids[1])
        weather.config(image=weather_raw) #reload image
        weather.image = weather_raw
    weather.after(1800000, refreshW) #call itself after 30min
#(...)
root = Tk()
#(...)
clock = Label(
    root,
    font=('Helvetica', 50),
    text=texts[0],
    fg='white',
    bg='black',
    cursor='none'
)
clock.place(
    relx=0.5,
    y=242,
    anchor='n'
)

tempCpu = Label(
    root,
    font=('Helvetica', 40),
    text='$CPU°C',
    fg='white',
    bg='black',
    cursor='none'
)
tempCpu.place(
    relx=0.2,
    y=100,
    anchor='n'
)
tempIns = Label(
    root,
    font=('Helvetica', 40),
    text='$INS°C',
    fg='white',
    bg='black',
    cursor='none'
)
tempIns.place(
    relx=0.5,
    y=100,
    anchor='n'
)
tempOut = Label(
    root,
    font=('Helvetica', 40),
    text='$OUT°C',
    fg='white',
    bg='black',
    cursor='none'
)
tempOut.place(
    relx=0.8,
    y=100,
    anchor='n'
)

tick()
refreshT()
refreshW(False)
root.mainloop()
如果此代码不起作用,则是Github上的完整代码


希望获得帮助或答案链接,如果这是重复的,很抱歉。

似乎有很多问题,那么您当前遇到的问题是什么?例如,
timeOld
是一个局部变量,因此不存在现有的
timeOld
。此外,天气图像可能正在被垃圾收集。这三个小部件中的哪一个会导致问题,具体是以什么方式造成的?此外,我强烈建议您也使用该代码,因为还有很多地方可以改进,例如添加一些空行和注释、用于常见标签格式和放置的帮助函数、避免
如果x:return True否则返回False
,在
getTime
等中混合返回类型。关于似乎存在的问题:读取温度可能需要大约2秒钟,阻塞了那么长的tkinter线程。您可以使用线程来实现这一点,但我认为这有点过分了。你真的必须以秒为单位显示时间吗?如果是,你必须以秒为单位以1/5秒的间隔更新时间吗?或者,您可以在三个温度读数之间调用
tick
函数。
weather
未定义。
text
getTime
也未定义。投票结束,因为没有。你的工作是扔掉所有与你所问问题无关的代码。