Tkinter的多个操作 [英] Tkinter multiple operations

查看:179
本文介绍了Tkinter的多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是与此类似: Python的使用TKinter多个操作。然而,所提供的答案并不能帮助我,因为它指向与可能的功能使用列表的文章。我想看到一个实际执行的解决方案,请的。

我的问题:我有一个框架上两个按钮。只要拨动变量设置为true,一键调用执行的功能。第二按钮切换值设置为False。我想要的执行功能,以保持持续,一旦我preSS执行按钮,但停止时,我preSS第二(切换假)按钮。然而,帧卡住一次我preSS执行。我理解它的回调,因为。我怎样才能解决这个问题?我的继承人样本code:

 从Tkinter的进口*
从进口的时间睡眠类应用程序:    高清__init __(个体经营,硕士):        self.toggle =假
        帧=帧(主)
        frame.pack()        self.exeButton =按钮(帧,文本=执行,FG =蓝色,命令= self.execute)
        self.exeButton.pack(侧= LEFT)        self.tOffButton =按钮(帧,文本=切换关,命令= self.toggleOff)
        self.tOffButton.pack(侧= LEFT)    DEF执行(个体经营):
        self.toggle = TRUE
        而(self.toggle):
            打印大家好!
            睡眠状态(2)    高清toggleOff(个体经营):
    self.toggle =假根= TK()
应用程序=应用程序(根)
root.mainloop()


解决方案

简短的回答,你不能这样做正是你想要的。 Tkinter的是单线程的 - 当你调用睡眠(2)它,你就问到底是什么:它睡觉。

如果你的目标是只要一个布尔标志设置为True来执行的东西每2秒,可以使用的来安排工作,在将来运行。如果作业(再)时间表本身也经过使用,你已经创建了有效的无限循环,其中实际的循环机制是事件循环本身。

我已经采取了你的code和做了一些轻微的修改,向您展示,直到一个标志告诉它停止如何继续执行的东西。我把重命名切换到运行,以让它稍微容易理解的自由。我也使用只是一个单一的方法来既打开和关闭的执行。

 从Tkinter的进口*
从进口的时间睡眠类应用程序:    高清__init __(个体经营,硕士):        self.master =主
        self.running =假
        帧=帧(主)
        frame.pack()        self.exeButton =按钮(帧,文本=执行,FG =蓝色,
            命令=拉姆达:self.execute(真))
        self.exeButton.pack(侧= LEFT)        self.tOffButton =按钮(帧,文本=切换关,
            命令=拉姆达:self.execute(假))
        self.tOffButton.pack(侧= LEFT)    DEF执行(个体经营,运行=无):
        如果运行的是不是无:
            self.running =运行
        如果self.running:
            打印大家好!
            self.master.after(2000年,self.execute)根= TK()
应用程序=应用程序(根)
root.mainloop()

My question is similar to this: Python TKinter multiple operations. However, The answer provided does not help me since it points to an article with a list of possible functions to use. I want to see an actual implementation of the solution please.

My question: I have two buttons on a frame. one button calls the 'execute' function as long as the toggle variable is set to true. The 2nd button sets the toggle value to False. I want the 'execute' function to keep going once i press the execute button but stop when i press the 2nd (toggle false) button. However, the frame gets stuck once i press 'execute'. I understand its because of callbacks. How can i fix this? Heres my sample code:

from Tkinter import *
from time import sleep

class App:

    def __init__(self, master):

        self.toggle = False
        frame = Frame(master)
        frame.pack()

        self.exeButton = Button(frame, text="Execute", fg="blue", command=self.execute)
        self.exeButton.pack(side=LEFT)

        self.tOffButton = Button(frame, text="Toggle Off", command=self.toggleOff)
        self.tOffButton.pack(side=LEFT)

    def execute(self):
        self.toggle = True
        while(self.toggle):
            print "hi there, everyone!"
            sleep(2)

    def toggleOff(self):
    self.toggle = False

root = Tk()
app = App(root)
root.mainloop()

解决方案

Short answer, you can't do exactly what you want. Tkinter is single threaded -- when you call sleep(2) it does exactly what you ask it to: it sleeps.

If your goal is to execute something every 2 seconds as long as a boolean flag is set to True, you can use after to schedule a job to run in the future. If that job also uses after to (re)schedule itself, you've effectively created an infinite loop where the actual looping mechanism is the event loop itself.

I've taken your code and made some slight modifications to show you how to execute something continuously until a flag tells it to stop. I took the liberty of renaming "toggle" to "running" to make it a little easier to understand. I also use just a single method to both turn on and turn off the execution.

from Tkinter import *
from time import sleep

class App:

    def __init__(self, master):

        self.master = master
        self.running = False
        frame = Frame(master)
        frame.pack()

        self.exeButton = Button(frame, text="Execute", fg="blue", 
            command=lambda: self.execute(True))
        self.exeButton.pack(side=LEFT)

        self.tOffButton = Button(frame, text="Toggle Off", 
            command=lambda: self.execute(False))
        self.tOffButton.pack(side=LEFT)

    def execute(self, running=None):
        if running is not None:
            self.running = running
        if self.running:
            print "hi there, everyone!"
            self.master.after(2000, self.execute)

root = Tk()
app = App(root)
root.mainloop()

这篇关于Tkinter的多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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