首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何取消tkinter中正在运行的脚本

如何取消tkinter中正在运行的脚本
EN

Stack Overflow用户
提问于 2017-02-16 14:39:48
回答 2查看 1K关注 0票数 0

我在试着为我的覆盆子派做个替身。摄影用的干涉仪。我已经用tkinter创建了一个GUI,但是我无法找到取消正在运行的脚本的方法,因为当脚本运行时,所有的按钮都会被阻塞。可能是不可能的,因为我的脚本包含循环(因为我在range()中).我一直在尝试".after“方法,但是这个方法只是停止脚本而没有取消它。谢谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-17 01:44:31

您可以尝试使用线程或多进程。您可以参考this post来了解更多有关它的信息。另外,如果您只想停止脚本的运行,可以从终端向程序发送SIGKILL信号(命令是killall Python -9),或者在程序中添加一个调用sys.exit()的按钮

票数 2
EN

Stack Overflow用户

发布于 2017-02-17 15:47:44

代码语言:javascript
复制
from Tkinter import *

类示例:

代码语言:javascript
复制
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)

root=Tk()示例(根) root.mainloop()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42277018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档