gpt4 book ai didi

python - 在 Tkinter 中使用 after() 循环

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:17 24 4
gpt4 key购买 nike

我是 Tkinter 的新手,仍然非常不确定我想要做的事情,希望这不是愚蠢的。欢迎每一个帮助。

我想用我的树莓派来控制一些电机。这些电机将成分组合在一起。它在 Python 中工作得很好,但我想要一个带有几个按钮的 GUI。每个按钮都应将一个配方放入函数 makerecipe 中。配方由不同电机应启动的时间列表组成。 Makerecipe 将激活 GPIO 引脚。

然后一个新的功能电机应该启动。此处它检查何时应停用电机。这是一个在 Python 中有效的简单技巧,但我不知道如何使其在 Tkinter 中工作。每秒循环检查所耗时是否等于配方中的时间。如果是这样,则电机被停用。

from Tkinter import *
import ttk
import time

root = Tk()
var = StringVar()
uitput = StringVar() #I want to print what is happening to label L2
uitput.set('Nothing') #when program starts it says
conversie = [7, 11, 15] #I will use pins 7, 11 and 15 on the RPi,
moj = [0, 0, 2] #recipe 1, through Button 1, number of seconds the pins are True
sob = [4, 0, 0] #recipe 2, through Button 2, number of seconds the pins are True

#The following function activates the pins which are used in making the recipe
#later this will just activate the pins, for now it shows it in a label on screen.
#this seems to work

def makerecipe(argu):
aa=[]
for i in range(len(argu)):
if argu[i]==0:
a=('GPIO.output(', str(conversie[i]), 'False)')
aa.append(a)
else:
b=('GPIO.output(', str(conversie[i]), 'True)')
aa.append(b)
uitput.set(aa)
root.update_idletasks()
root.motor(argu)

#Next I want to have a function that looks at recipe and reads how long the
#GPIO pins should be active. Then turns them off one at a time. I just don't
#understand the after function.
#I think, but probably wrong, that my recipe was loaded in makerecipe and argu
#has the value of the recipe because of the button, and I hoped I could pass argu
#along to the function motor.


def motor(motorinput):
resultaat=('bla')
uitput.set(resultaat)
`enter code here` cc=[]
for MotorNum in range(max(motorinput)+1):
if MotorNum in motorinput:
if motorinput.index(MotorNum)==0:
c=("GPIO.output(",conversie[motorinput.index(MotorNum)],", False)")
cc.append(c)
elif motorinput.index(MotorNum)==1:
d=("GPIO.output(",conversie[motorinput.index(MotorNum)],", False)")
cc.append(d)
elif motorinput.index(MotorNum)==2:
e=("GPIO.output(",conversie[motorinput.index(MotorNum)],", False)")
cc.append(e)
uitput.set(cc)
root.update_idletasks()
#root.after(1000, motor(alfa)) #This goes wrong.

B= Button(root, text="optie 1", command=lambda: makerecipe(moj))
B.pack()
L2=Label(root, textvariable=uitput, width=100)
L2.pack()
root.mainloop()

我在这里打印整个代码的原因是,它可能有助于了解我到底在尝试什么,它可能看起来很可怕,但我正在努力做得更好。

第一个问题是我显然不明白如何在第一个函数中调用下一个函数电机。它停在那里并给出: AttributeError: motor

第二个问题是我知道如何使用 time.sleep,但我在这个论坛上到处都读到你不应该这样做。所以我尝试使用之后,但不知道如何正确使用它。

我希望有人可以帮助这个新手。我对Python的逻辑相当了解,但Tkinter对我来说是一种新的思维方式。

最佳答案

First question is I apparently don't understand how to call the next function motor inside my first function. It stops there and gives me: AttributeError: motor

问题出在makerecipe函数的最后一行:

root.motor(argu)

变量root是一个不具有运动功能的TK对象。这就是 AttributeError 的原因。将此行更改为:

motor(argu)

将消除此错误。

Second question is I know how to work with time.sleep, but I read everywhere on this forum that you should not do this. So I am trying to use after, but don't know how to use this properly.

您应该使用 after,因为 Tk 有一个正在运行的事件循环(root.mainloop() 调用)来根据事件使用react(例如,在单击按钮时调用您的函数,或者在某个时间已过)通过)。但如果您在代码中使用time.sleep,则可能会干扰此事件循环。

解决方法是您应该将函数引用传递给 after,这样 Tk eventloop 就会在正确的时间到来时调用该函数。但在这里,您立即调用该函数:

root.after(1000, motor(alfa)) #This goes wrong.

这一行调用motor(并传递alfa作为参数),然后将motor的返回值(可以是任何值)传递给root.after

这一行应该是这样的:

root.after(1000, motor, alfa)

现在我们告诉 root 在 1 秒后使用 alfa 参数调用 motor

关于python - 在 Tkinter 中使用 after() 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com