这是我正在努力解决的程序的第二个版本。我在程序的第一个版本上发布了一个早期的问题,但没有运气。
这是我的“大图片创意”,以防它帮助您找到我偏离轨道的位置。我有很多植物浇水,我想用TKinter制作一个GUI,它将出现在Raspberry Pi的触摸屏上。 Pi控制110V继电器开关,该开关连接到水泵。它只是z#植物的基本“开/关”循环。
用户有三个要调整的滑块,X,Y和Z值。 X是水泵需要打开多长时间,Y是水泵需要关闭的时间(因此您有时间将软管移动到下一个工厂),Z是您需要的植物总数水。我曾经只是让循环永远运行,因为我总是可以CTRL-C程序结束,但现在我试图在触摸屏上完成所有操作而不需要键盘 - 比不教导某人更方便用户知道Linux意味着如何使用终端。它还需要像人们习惯看到的程序一样运行,因此GUI触摸屏按钮(如智能手机和平板电脑)。
代码在大多数情况下运行良好,它会停止并启动而不会崩溃。我找到了开始按钮,并且有一个重置按钮来清除滑块值,以防你想要以零开始值。但是,这个似乎影响很多人的大问题是“退出”按钮。
我需要用户能够在循环过程中随时按下TKinter EXIT按钮,让程序知道按下按钮并立即服从它,基本上会中断循环。我尝试了很多东西,没有任何作用。检查按钮只会等到循环结束,然后更新切换值。那时为时已晚;在整个浇水循环期间我需要用户控制。
我也不希望程序在按下EXIT时退出,而是希望GPIO循环停止在“OFF”值并基本上将程序重置为开始状态。
我知道time.sleep()
非常粗糙,但我还没有找到任何其他办法。我见过人们谈论'线程',我不知道这意味着什么,听起来我永远不会。我也看到人们谈论TKinters的“after()”函数,同样,我需要非常明确的答案示例。只是说“使用线程”没有任何帮助 - 我是一个菜鸟!
from tkinter import *
import RPi.GPIO as GPIO
import time
master = Tk()
def onoffcycle():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.OUT)
GPIO.setup(14, GPIO.LOW)
y=off.get()
y=float(y)
x=on.get()
x=float(x)
GPIO.output(14, True)
print("On")+str(x)
time.sleep(x)
GPIO.output(14, False)
print("Off")+str(y)
time.sleep(y)
def start():
print("Prepare to water in 10 seconds...")
time.sleep(10)
z=cycle.get()
z=float(z)
while z > 0:
onoffcycle()
z=z-1
print("Cycles remaining:")+str(z)
###tog()
###if t==1:
###reset()
else:
reset()
### this program runs onoffcycle() for z number of cycles as set by the slider.
### It should check for an exit toggle, then run the program for one cycle,
### then check for exit, then another round. countdown from cycle.get variable
def reset():
GPIO.cleanup()
on.set(0)
off.set(0)
cycle.set(0)
t=IntVar()
###def tog():
### HELP! This is where I need the program to be looking for the EXIT button being pressed
### in order to stop the program. Everything I have tried so far waits until the z value drops
### to zero, basically until the program stops on its own. I need a button that interrupts
### everything.
on = Scale(master, label="Set # Seconds Water ON:", from_=0, to=180, orient=HORIZONTAL, length=400, width=35,
troughcolor="red", bg="SteelBlue1", fg="black", bd=6, sliderlength=90, sliderrelief=RIDGE,
font = '-weight bold')
on.grid(column=1, row=1, columnspan=3)
off = Scale(master, label="Set # Seconds Water OFF:", from_=0, to=30, orient=HORIZONTAL, length=400, width=35,
troughcolor="yellow", bg="SteelBlue1", fg="black", bd=6, sliderlength=90, sliderrelief=RIDGE,
font = '-weight bold')
off.grid(column=1, row=2, columnspan=3)
cycle = Scale(master, label="Set # of Plants to Water:", from_=0, to=200, orient=HORIZONTAL, length=400, width=35,
troughcolor="green", bg="SteelBlue1", fg="black", bd=6, sliderlength=90, sliderrelief=RIDGE,
font = '-weight bold')
cycle.grid(column=1, row=3, columnspan=3)
go = Button(master, text="START", command=start, bg="SteelBlue3", width=10, height=2, font='-weight bold')
go.grid(column=1, row=4)
adios = Checkbutton(master, text="EXIT", variable=t, indicatoron=0, bd=6, bg="SteelBlue3", width=11, height=2, font='-weight bold')
adios.grid(column=3, row=4)
###adios should have a command to run an exit program - with toggle values? or something else?
resetbutton = Button(master, text="RESET", command=reset, bg="SteelBlue3", width=10, height=2, font='-weight bold')
resetbutton.grid(column=2, row=4)
mainloop()
答案 0 :(得分:0)
使用布尔变量True/False
来控制循环和其他元素
例如,创建全局变量
time_to_exit = False
并在按True
按钮
EXIT
并在函数中使用
while z > 0 and not time_to_exit:
它应该停止循环。
您可以使用其他布尔变量来通知程序是否循环停止并且可以退出。
# inform function to use global variable
global loop_is_working
loop_is_working = True
while z > 0 and not time_to_exit:
# ...
def reset():
# inform function to use global variable
global loop_is_working
loop_is_working = False
def on_exit:
if loop_is_working:
reset()
# check again afte 100ms
after(100, on_exit)
else:
# stop `mainloop()` and close tkinter window
root.destroy()
您的另一个问题是:您使用sleep()
和while
,因此Tkinter
必须等到它结束。
您可以使用master.after(miliseconds, function_name)
和Tkinter/mainloop
执行此功能,同时可以检查您的按钮。
例如
time_to_exit = False
def start():
print("Prepare to water in 10 seconds...")
# mainloop will run `start_loop` after `10s`
master.after(10000, start_loop) # 10s = 10 000ms
def start_loop():
z = float(cycle.get())
loop(z)
# or
#master.after(0, loop, z)
def loop(z):
if z > 0 and not time_to_exit:
onoffcycle()
z -= 1
print("Cycles remaining:", z)
###tog()
###if t==1:
###reset()
# mainloop will run `loop` again as soon as possible
master.after(0, loop, z)
else:
reset()
甚至
# create global variables
time_to_exit = False
loop_is_working = False
def start():
# inform function to use global variable
global loop_is_working
loop_is_working = True
print("Prepare to water in 10 seconds...")
# mainloop will run `start_loop` after `10s`
master.after(10000, start_loop) # 10s = 10 000ms
def start_loop():
z = float(cycle.get())
loop(z)
# or
#master.after(0, loop, z)
def loop(z):
if z > 0 and not time_to_exit and loop_is_working:
onoffcycle()
z -= 1
print("Cycles remaining:", z)
###tog()
###if t==1:
###reset()
# mainloop will run `loop` again as soon as possible
master.after(0, loop, z)
else:
reset()
def reset():
# inform function to use global variable
global loop_is_working
# ... your code ...
loop_is_working = False
答案 1 :(得分:0)
所以这是新代码与furas'很好的建议。我还发现了如何在每次迭代时使z
值减小。现在,RESET按钮将在完成当前循环后暂停程序,并等待您开始新的循环。这是因为我仍然有time.sleep()
处理ON和OFF时序。我尝试使用after()
,但它一直导致其他更大的问题。在这一点上,该程序在技术上是完全可用的,所以我不介意它挂一个周期;它不会影响现实世界的应用程序!我仍然有其他问题需要他们自己的问题,例如配置触摸屏以正确校准,以及在自动启动LDXE后自动启动程序。我计划将所有内容正确地放在class()
中。还希望能够在卡住if:
语句时更改Label小部件文本。谢谢大家!
#!/usr/bin/python
from Tkinter import *
import ttk
import RPi.GPIO as GPIO
import time
master = Tk()
master.title("Obligatory Title")
master.attributes("-fullscreen", True)
time_to_exit = False
loop_is_working = False
def onoffcycle():
global t
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.LOW)
y=float(off.get())
x=float(on.get())
GPIO.output(18, True)
print("On")+str(x)
t.set("Water ON")
t.get()
time.sleep(x)
GPIO.output(18, False)
print("Off")+str(y)
t.set("Water OFF")
time.sleep(y)
def start():
global loop_is_working
global t
loop_is_working = True
print("Prepare to water in 10 seconds...")
t.set("Prepare to water in 10 seconds...")
master.after(10000, loop)
def loop():
z=float(cycle.get())
global loop_is_working
global time_to_exit
global t
t.set("Now Watering...")
if z > 0 and not time_to_exit and loop_is_working:
onoffcycle()
z=z-1
cycle.set(z)
print("Cycles remaining: ")+ str(z)
master.after(0, loop)
else:
reset()
def reset():
global loop_is_working
GPIO.cleanup()
on.set(0)
off.set(0)
cycle.set(0)
t.set("Welcome! Drag the sliders to set values. Tap left or right of the sliders to fine tune values.")
loop_is_working = False
t = StringVar()
t.set("Welcome! Drag the sliders to set values. Tap left or right of the sliders to fine tune values.")
welcome = Label(master, bg="Steelblue3", bd=6, relief=RAISED, width=92,
height=2, textvariable=t)
welcome.grid(column=1, row=1, columnspan=3)
on = Scale(master, label="Set # Seconds Water ON:", from_=0, to=120, orient=HORIZONTAL, length=735,
width=50, troughcolor="steelblue", bg="SteelBlue1", fg="black", bd=6, sliderlength=90,
sliderrelief=RIDGE)
on.grid(column=1, row=2, columnspan=3)
off = Scale(master, label="Set # Seconds Water OFF:", from_=0, to=30, orient=HORIZONTAL, length=735,
width=50,troughcolor="steelblue", bg="SteelBlue1", fg="black", bd=6, sliderlength=90,
sliderrelief=RIDGE)
off.grid(column=1, row=3, columnspan=3)
cycle = Scale(master, label="Set # of Plants to Water:", from_=0, to=200, orient=HORIZONTAL, length=735,
width=50,troughcolor="steelblue", bg="SteelBlue1", fg="black", bd=6, sliderlength=90,
sliderrelief=RIDGE)
cycle.grid(column=1, row=4, columnspan=3)
go = Button(master, text="START", command=start, bg="green3", width=19, height=3,
bd=4, font='-weight bold')
go.grid(column=1, row=5)
adios = Button(master, text="EXIT", command=exit, bg="red3", width=19, height=3,
bd=4, font='-weight bold')
adios.grid(column=3, row=5)
resetb = Button(master, text="RESET", command=reset, bg="yellow3", width=19,
height=3, bd=4, font='-weight bold')
resetb.grid(column=2, row=5)
mainloop()