我如何功能之后停止Tkinter的? [英] How do I stop tkinter after function?

查看:320
本文介绍了我如何功能之后停止Tkinter的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有停止喂的问题;取消的说法似乎并不具备方法后的任何影响。虽然饲料停止打印到控制台。

我试图有一个按钮,用于启动饲料和另一个将停止进料。

 从Tkinter的进口TK,按钮
进口随机    高清goodbye_world():
        打印停止频道
        button.configure(文=开始频道,命令=参考hello world)
        print_sleep(真)    DEF参考hello world():
        打印启动频道
        button.configure(文=停止频道,命令= goodbye_world)
        print_sleep()    高清print_sleep(取消= FALSE):
        如果取消==错误:
            富= random.randint(4000,7500)
            打印睡觉,富
            root.after(富,print_sleep)
        其他:
            打印频道停止
    根= TK()
    按钮=按钮(根,文本=开始频道,命令=参考hello world)    button.pack()
    root.mainloop()

通过输出:

 启动给
睡眠4195
4634睡觉
6591睡觉
睡眠7074
停止进料
4908睡觉
饲料停止
睡眠6892
沉睡5605


解决方案

的问题是,即使你调用 print_sleep 停止循环,还有早已等候火待处理的工作。 pressing停止按钮不会造成新的工作开火,但以前的工作仍然存在,当它调用自身,它通过假这将导致循环继续。

您需要取消挂起的作业,以便它不会运行。例如:

  DEF取消():
    如果self._job不是无:
        root.after_cancel(self._job)
        self._job =无高清goodbye_world():
    打印停止频道
    取消()
    button.configure(文=开始频道,命令=参考hello world)DEF参考hello world():
    打印启动频道
    button.configure(文=停止频道,命令= goodbye_world)
    print_sleep()高清print_sleep():
    富= random.randint(4000,7500)
    打印睡觉,富
    self._job = root.after(富,print_sleep)

请注意:确保你初始化 self._job 的地方,比如在你的应用程序对象的构造函数。

I'm having a problem stopping the 'feed'; the cancel argument doesn't seem to have any impact on the after method. Although "feed stopped" is printed to the console.

I'm attempting to have one button that will start the feed and another that will stop the feed.

from Tkinter import Tk, Button
import random

    def goodbye_world():
        print "Stopping Feed"
        button.configure(text = "Start Feed", command=hello_world)
        print_sleep(True)

    def hello_world():
        print "Starting Feed"
        button.configure(text = "Stop Feed", command=goodbye_world)
        print_sleep()

    def print_sleep(cancel=False):
        if cancel==False:
            foo = random.randint(4000,7500)
            print "Sleeping", foo
            root.after(foo,print_sleep)
        else:
            print "Feed Stopped"


    root = Tk()
    button = Button(root, text="Start Feed", command=hello_world)

    button.pack()


    root.mainloop()

With the output:

Starting Feed
Sleeping 4195
Sleeping 4634
Sleeping 6591
Sleeping 7074
Stopping Feed
Sleeping 4908
Feed Stopped
Sleeping 6892
Sleeping 5605

解决方案

The problem is that, even though you're calling print_sleep with True to stop the cycle, there's already a pending job waiting to fire. Pressing the stop button won't cause a new job to fire but the old job is still there, and when it calls itself, it passes in False which causes the loop to continue.

You need to cancel the pending job so that it doesn't run. For example:

def cancel():
    if self._job is not None:
        root.after_cancel(self._job)
        self._job = None

def goodbye_world():
    print "Stopping Feed"
    cancel()
    button.configure(text = "Start Feed", command=hello_world)

def hello_world():
    print "Starting Feed"
    button.configure(text = "Stop Feed", command=goodbye_world)
    print_sleep()

def print_sleep():
    foo = random.randint(4000,7500)
    print "Sleeping", foo
    self._job = root.after(foo,print_sleep)

Note: make sure you initialize self._job somewhere, such as in the constructor of your application object.

这篇关于我如何功能之后停止Tkinter的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆