我正在尝试为我的Raspberry Pi做一个应用程序。用于摄影目的的间隔计。我用tkinter创建了一个GUI但我找不到取消正在运行的脚本的方法,因为脚本运行时所有按钮都被阻止了。 也许这是不可能的,因为我的脚本包含循环(对于范围内的i()... 我一直在尝试“.after”方法,但是这个方法只是停止脚本而不取消它。 感谢您的帮助
答案 0 :(得分:1)
您可以尝试使用线程或多进程。您可以参考this post了解有关它的更多信息。此外,如果您只想停止脚本运行,您可以从终端向命令发送SIGKILL信号(命令为killall Python -9
)或在程序中添加一个调用sys.exit()
的按钮
答案 1 :(得分:0)
from Tkinter import *
类示例:
def __init__(self, master):
self.etfilm = Label(root,width=12, font=('arial narrow', 14, 'normal'),fg="white", bg="green")
self.etfilm.grid(row=0, column=0,columnspan=1, padx=3, pady=2, sticky=NSEW)
self.etstatus = Label(root,width=12, font=('arial narrow', 14, 'normal'),bg="yellow")
self.etstatus.grid(row=0, column=1,columnspan=1, padx=3, pady=2, sticky=NSEW)
self.textBox = Text(root,height= 1,width=2, relief=SUNKEN, font=('arial', 18, 'normal'),)
self.textBox.grid(row=0, column=2, ipadx=13, padx=0, sticky=NSEW)
self.botshoot = Button(root, width=18, font=('arial narrow', 30, 'normal'), text="START ", activebackground="#00dfdf")
self.botshoot.grid(row=4, rowspan=2, column=0,columnspan=3,ipady=15,pady=1, sticky=NSEW)
self.botshoot.configure(command=self.start)
self.botkam = Button(root,width=10, font=('arial', 24, 'normal'), text="VIDEO SETTINGS", activebackground="#00dfdf")
self.botkam.grid(row=6, rowspan=3, column=0,columnspan=2, pady=1, sticky=NSEW)
self.botkamStop = Button(root,width=3, font=('arial', 24, 'normal'), text="STOP", activebackground="#00dfdf")
self.botkamStop.grid(row=6, rowspan=3, column=2, pady=1, sticky=NSEW)
self.botSelf = Button(root,width=10, font=('arial', 24, 'normal'), text="ACTIVATE SELFTIMER", activebackground="#00dfdf")
self.botSelf.grid(row=9, rowspan=3, column=0,columnspan=2, pady=1, sticky=NSEW)
self.botSelf1 = Button(root,width=3, font=('arial', 24, 'normal'), text="STOP", activebackground="#00dfdf")
self.botSelf1.grid(row=9, rowspan=3, column=2, pady=1, sticky=NSEW)
self.botConf = Button(root,heigh=2, font=('arial', 18, 'normal'), text="CONFIGURE", activebackground="red")
self.botConf.grid(row=12, rowspan=3, column=0,columnspan=1, pady=1, sticky=NSEW)
self.botStop = Button(root,heigh=2, font=('arial', 18, 'normal'), text="STOP/RESET", activebackground="red")
self.botStop.grid(row=12, rowspan=3, column=1,columnspan=2, pady=1, sticky=NSEW)
self.botStop.configure(state=DISABLED,command=self.stop)
def start(self):
self.count = 0
self.cancel_id = None
self.botConf.configure(state=DISABLED)
self.botshoot.configure(state=DISABLED)
self.botStop.configure(state=NORMAL)
self.counter()
def counter(self):
self.textBox.delete("1.0", END)
if self.count < 10:
self.count += 1
self.textBox.insert(END, str(self.count)+'\n\n')
self.cancel_id = self.textBox.after(1000, self.counter)
root.update_idletasks()
print(self.count)
def stop(self):
if self.cancel_id is not None:
self.textBox.after_cancel(self.cancel_id)
self.cancel_id = None
self.textBox.insert(END, 0)
self.textBox.delete("1.0", END)
self.botConf.configure(state=NORMAL)
self.botshoot.configure(state=NORMAL)
根= Tk的() 实施例(根) root.mainloop()