Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不';这些tkinter程序的行为是否相同?_Python_Tkinter_Raspberry Pi - Fatal编程技术网

Python 为什么不';这些tkinter程序的行为是否相同?

Python 为什么不';这些tkinter程序的行为是否相同?,python,tkinter,raspberry-pi,Python,Tkinter,Raspberry Pi,我做了两个代码。第二个是基于第一个的。不幸的是,由于某种原因,第二个不起作用,即使第一个起作用 第一个代码: import time from Tkinter import * root = Tk() t=StringVar() num=1 t.set(str(num) thelabel = Label(root, textvariable=t).pack() def printnum (x): while x<= 100: t.set(str(x))

我做了两个代码。第二个是基于第一个的。不幸的是,由于某种原因,第二个不起作用,即使第一个起作用

第一个代码:

import time
from Tkinter import *

root = Tk()
t=StringVar()
num=1
t.set(str(num)
thelabel = Label(root, textvariable=t).pack()

def printnum (x):
    while x<= 100:
        t.set(str(x))
        x += 1
        root.update()
        time.sleep(30)

printnum(num)
root.mainloop()
导入时间
从Tkinter进口*
root=Tk()
t=StringVar()
num=1
t、 设置(str(num)
标签=标签(根,textvariable=t).pack()
def printnum(x):

当x在这一行上用read\u retry的温度覆盖之前的t值时:

h,t = dht.read_retry(dht.DHT22, 4)

然后,当您尝试调用
set
时,
t
现在是一个float,因此没有set方法。请为其中一个变量使用不同的变量名,而不是
t

root.update
不起任何作用。您需要添加
()


也就是说,您的算法在tkinter中运行周期性任务的方式是错误的。请参见

float object
t
是一个float,而不是stringvar,因为您在
d,t=dht…
中使用了
t
,并破坏了它以前的stringvar。第一个代码缺少
t.set(str(num)上的最后一个括号
t=StringVar()
但是
newtext
是floattry
str(newtext)
就像你上面所做的:
t.set(str(num)
我修复了覆盖错误,它仍然不起作用。我把它重新编辑到我的问题中。请看@BryanOakley的回复,我相信它正确地识别了你错误的下一部分。
h,t = dht.read_retry(dht.DHT22, 4)
root.update()