Tkinter 背景 while 循环 [英] Tkinter background while loop

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

问题描述

好的,我以非常具体的方式问过这个问题:https://stackoverflow.com/questions/21667119/tkinter-increment-a-varaible-while-still-running-code

Okay I have asked this in a very specific way : https://stackoverflow.com/questions/21667119/tkinter-increment-a-varaible-while-still-running-code

但是现在用更少的话来解释它.

But now to explain it in many less words.

我有一个使用 tkinter 运行的程序.当一个按钮被按下时,它会将一个值放入一个队列中.

I have a program running using tkinter. When a button is pressed it puts a value into a queue.

我想要的只是能够使用 while 循环来操作队列中的数据,同时代码仍然允许将更多数据添加到队列中.

All I want is to be able to use a while loop to manipulate the data in the queue while the code still allows for more data to be added to the queue.

所以基本上重复:

Check see if button pressed
if yes : add to queue
if no : do nothing
manipulate queue data. 

如果您需要查看代码,请检查另一个问题,全部在那里.

Check the other question if you need to see code, its all in there.

我知道很多其他帖子都有这个,但我找不到一个对我来说足够容易解释的答案.

I know many other posts have this but I cannot find an answer that explains it easily enough for me.

简单的代码,我可以将其放入项目中 =D

Simple code I can jar into a project please =D

谢谢

推荐答案

您的 tkinter 程序已经运行了一个while"循环——mainloop.在大多数情况下,您不需要该循环内的另一个循环.

Your tkinter program already has a "while" loop running -- mainloop. In most cases you don't need another loop inside that loop.

使用此循环的模式是创建一个函数,该函数为循环体执行您想要的操作.它应该只执行一次循环迭代.完成后,它需要使用 after 安排自己在将来的某个时间再次被调用.未来多远定义了循环运行的速度.

The pattern for using this loop is to create a function that does what you want for the body of the loop. It should do exactly one iteration of the loop. Once it is done, it needs to arrange for itself to be called again some time in the future using after. How far in the future defines how fast your loop runs.

这是每秒检查一次队列的示例:

Here's an example that checks the queue once per second:

import Tkinter as tk
import Queue as queue

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.queue = queue.Queue()

        buttonFrame = tk.Frame(self)
        for i in range(10):
            b = tk.Button(buttonFrame, text=str(i), 
                          command=lambda button=i: self.press(button))
            b.pack(side="top", fill="x")
        self.lb = tk.Listbox(self, width=60, height=20)
        self.vsb = tk.Scrollbar(self, command=self.lb.yview)
        self.lb.configure(yscrollcommand=self.vsb.set)

        buttonFrame.pack(side="left", fill="y")
        self.vsb.pack(side="right", fill="y")
        self.lb.pack(side="left", fill="both", expand=True)

        self.manage_queue()

    def press(self, i):
        '''Add a button to the queue'''
        item = "Button %s" % i
        self.queue.put(item)
        self.log("push", item)

    def log(self, action, item):
        '''Display an action in the listbox'''
        message = "pushed to queue" if action == "push" else "popped from queue"
        message += " '%s' (queue size %s)" % (item, self.queue.qsize())
        self.lb.insert("end", message)
        self.lb.see("end")

    def manage_queue(self):
        '''pull an item off the queue and act on it'''
        try:
            item = self.queue.get_nowait()
            self.log("pop", item)
        except queue.Empty:
            pass

        # repeat again in 1 second
        self.after(1000, self.manage_queue)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

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

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