gpt4 book ai didi

python - 如何创建一个按钮来结束程序而不结束GUI?

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

我正在尝试制作一个 GUI,以使测试台更加用户友好,但他们请求了一个停止按钮,该按钮将结束当前程序而不杀死实际的 GUI 窗口。

我知道线程可能是执行此操作的方法,因为当按下“开始”按钮时,它会接管整个 GUI 窗口,并且无法单击任何内容,但我不确定如何在我的代码中实际实现它。

我希望能够按下停止按钮并使 GPIO 返回到关闭状态,然后能够再次按下启动按钮并让程序再次从头开始。

import tkinter as tk
import RPi.GPIO as GPIO
import time as time
from time import sleep

#sets GPIO pin numbers#
GPIO4 = 4
GPIO17 = 17
GPIO27 = 27
GPIO22 = 22
GPIO23 = 23
GPIO24 = 24

#sets the initial state of the start button#
Start_state = True

#sets GPIOs to GPIO pin numbers#
GPIO.setmode(GPIO.BCM)

#sets warnings to not show#
GPIO.setwarnings(False)

#sets up GPIOs as outputs#
GPIO.setup(GPIO4, GPIO.OUT)
GPIO.setup(GPIO17, GPIO.OUT)
GPIO.setup(GPIO27, GPIO.OUT)
GPIO.setup(GPIO22, GPIO.OUT)
GPIO.setup(GPIO23, GPIO.OUT)
GPIO.setup(GPIO24, GPIO.OUT)

#sets initial states of the pins#
GPIO.output(GPIO4, False)
GPIO.output(GPIO17, False)
GPIO.output(GPIO27, False)
GPIO.output(GPIO22, False)
GPIO.output(GPIO23, False)
GPIO.output(GPIO24, False)

#start of main.loop()#
try:

####sets GUI window####
master = tk.Tk()

####name of GUI####
master.title("SOLENOID CONTROL")

####sets size of window (widthxheight)####
master.geometry ("650x150")

####sets initial states of GPIOs to 'off'####
GPIO4_state = False
GPIO17_state = False
GPIO27_state = False
GPIO22_state = False
GPIO23_state = False
GPIO24_state = False

####defines checkbutton states and relates them to individual GPIOs####
def GPIO4button():
global GPIO4_state
GPIO4_state = not GPIO4_state

def GPIO17button():
global GPIO17_state
GPIO17_state = not GPIO17_state

def GPIO27button():
global GPIO27_state
GPIO27_state = not GPIO27_state

def GPIO22button():
global GPIO22_state
GPIO22_state = not GPIO22_state

def GPIO23button():
global GPIO23_state
GPIO23_state = not GPIO23_state

def GPIO24button():
global GPIO24_state
GPIO24_state = not GPIO24_state

####defines dwell, spray times, and number of repititions (runs) from entry boxes####
def dwell():
dwell = e1.get()

def spray():
spray = e2.get()

def runs():
runs = e3.get()

####defines the start buttons actions####
def Start():

########gets input from entry boxes to be used in while loop#########
dwell = int(e1.get())
print('dwell: ' + str(dwell))
if e1.get() == 0:
time.sleep(e1.get())
else:
pass

spray = int(e2.get())
print('spray: ' + str(spray))
if e2.get() == 0:
time.sleep(e2.get())
else:
pass

runs = int(e3.get())
print('runs: ' + str(runs))
if e3.get() == 0:
runs = e3.get()
else:
pass

########defines the start buttons state and sets i = 0 for the 'runs' counter########
global Start_state
i = 0

########when the button is pushed do:########
if Start_state == True:
############starts loop for 'runs' counter############
while i < runs:

################prints states of GPIOs and sets state based on checkbutton input, sets dwell and spray time based on entry box input, runs sequentially################
print('gpio4: ' + str(GPIO4_state))
if GPIO4_state == True:
GPIO.output(GPIO4, True)
time.sleep(spray)
GPIO.output(GPIO4, False)
Start_state == False
time.sleep(dwell)

print('gpio17: ' + str(GPIO17_state))
if GPIO17_state == True:
GPIO.output(GPIO17, True)
time.sleep(spray)
GPIO.output(GPIO17, False)
Start_state == False
time.sleep(dwell)

print('gpio27: ' + str(GPIO27_state))
if GPIO27_state == True:
GPIO.output(GPIO27, True)
time.sleep(spray)
GPIO.output(GPIO27, False)
Start_state == False
time.sleep(dwell)

print('gpio22: ' + str(GPIO22_state))
if GPIO22_state == True:
GPIO.output(GPIO22, True)
time.sleep(spray)
GPIO.output(GPIO22, False)
Start_state == False
time.sleep(dwell)

print('gpio23: ' + str(GPIO23_state))
if GPIO23_state == True:
GPIO.output(GPIO23, True)
time.sleep(spray)
GPIO.output(GPIO23, False)
Start_state == False
time.sleep(dwell)

print('gpio24: ' + str(GPIO24_state))
if GPIO24_state == True:
GPIO.output(GPIO24, True)
time.sleep(spray)
GPIO.output(GPIO24, False)
Start_state == False
time.sleep(dwell)
else:
pass

################prints end of run + run number for east debug################
print('end: ' + str(i))

################adds 1 to the 'runs' to reach entry box value################
i += 1

############end of while loop############
else:
pass

####creates checkbutton and links it to button command####
####positions checkbutton in GUI window####
checkbutton1 = tk.Checkbutton(master, text="1", command=GPIO4button)
checkbutton1.grid(row=1, column=1)

checkbutton2 = tk.Checkbutton(master, text="2", command=GPIO17button)
checkbutton2.grid(row=1, column=2)

checkbutton3 = tk.Checkbutton(master, text="3", command=GPIO27button)
checkbutton3.grid(row=1, column=3)

checkbutton4 = tk.Checkbutton(master, text="4", command=GPIO22button)
checkbutton4.grid(row=1, column=4)

checkbutton5 = tk.Checkbutton(master, text="5", command=GPIO23button)
checkbutton5.grid(row=1, column=5)

checkbutton6 = tk.Checkbutton(master, text="6", command=GPIO24button)
checkbutton6.grid(row=1, column=6)

####labels checkbutton rows####
tk.Label(master, text="SOLENOIDS").grid(row=1, column=0)

####creates entry boxes and links them to entry variable####
####positions entry boxes in GUI window####
tk.Label(master, text="DWELL TIME").grid(row=6,column=7)
e1 = tk.Entry(master)
e1.grid(row=6, column=8)

tk.Label(master, text="SPRAY TIME").grid(row=7,column=7)
e2 = tk.Entry(master)
e2.grid(row=7, column=8)

tk.Label(master, text="RUNS").grid(row=8,column=7)
e3 = tk.Entry(master)
e3.grid(row=8, column=8)

####creates start, stop, and exit buttons and links them to respective commands####
####starts while loop####
Startbutton = tk.Button(master, text="START", bg="green", command=Start)
Startbutton.grid(row=10, column=7)

####stops current 'run'####
Stopbutton = tk.Button(master, text="STOP", bg="red", command=Start)
Stopbutton.grid(row=10, column=8)

####exits GUI window####
Exitbutton = tk.Button(master, text="EXIT", bg="red", command=master.destroy)
Exitbutton.grid(row=10, column=9)

####still no real idea####
master.mainloop()

#sets all GPIO back to original 'off' state#
finally:
GPIO.cleanup()

我希望能够按下停止按钮并使 GPIO 返回到关闭状态,然后能够再次按下启动按钮并让程序再次从头开始。

最佳答案

如果您启动长时间运行的函数,那么您必须在线程中运行它,但它可能无法访问 GUI。

如果您可以将函数拆分为更小的部分,那么您可以使用 master.after(time, next_part) 运行下一部分,然后 mainloop() 有时间更新GUI - 即。它有时间运行分配给“停止”按钮的功能 - 并且不会卡住。可以使用 master.after() 代替 sleep()

或者您可以在函数中的许多地方使用 master.update() ,这样它将更新 GUI - mainloop() 将有时间运行分配给 STOP 的函数按钮。它不需要线程。

要结束程序,您可以在很多地方使用 bool 变量(即if is_running:)。按钮 STOP 应设置 is_running = False,并且应在 if is_running: 上停止运行函数。

关于python - 如何创建一个按钮来结束程序而不结束GUI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872022/

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