<Configure>的连续调用tkinter 中的事件(跟进) [英] Continuous call of the <Configure> event in tkinter (follow up)

查看:23
本文介绍了<Configure>的连续调用tkinter 中的事件(跟进)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python/Tkinter 的新手,我真的被困在这里......我正在尝试设计一个相当灵敏的 GUI,并且在其中一个框架中我需要显示一个垂直的小部件列表,这些小部件可能不适合所有同时.如果用户调整窗口大小,我想计算可以适应新大小的项目数量,并最终重绘可见"列表.我正在使用 root.bind('<Configure>', do_something) 来触发重绘.

I'm new to Python/Tkinter and I'm really stuck here... I am trying to design a fairly responsive GUI and in one of the frames I need to display a vertical list of widgets that might not fit all at the same time. If the user resizes the window, I'd like to compute the amount of items that can fit in the new size and finally redraw the "visible" list. I'm using the root.bind('<Configure>', do_something) to trigger the redraw.

问题:绑定到 事件会为每个调整大小操作多次触发命令.我希望能够确定调整大小何时完成(并释放鼠标按钮),并且只重绘一次.

PROBLEM: binding to the <configure> event triggers the command many times for each single resize action. I'd like to be able to determine when the resizing have been completed (and the mouse button released), and redraw just once.

我已阅读此之前发布的答案问题(与我的案例非常相关且基本相同),但第一个答案没有提供任何代码,我真诚地无法弄清楚如何遵循建议,而第二个答案提供了代码但它没有不解决问题.对第二个答案的评论之一已经表明它可能不起作用,因为 ButtonRelease-1 事件不在窗口内(因为它是调整大小).事实上在我的操作系统上不起作用(Win10).

I have read the answers to this previously posted question (very relevant and basically identical to my case), but the first answer doesn't provide any code and I sincerely couldn't figure out how to follow the advice, while the second answer gives the code but it doesn't solve the problem. One of the comments to this second answer already suggests that it might not work because the ButtonRelease-1 event is not inside the window (since it is a resize). And in fact on my OS doesn't work (Win10).

有人可以向我提供解决方案或变通方法来帮助我吗?如果可能,我希望该解决方案适用于 Windows 和 Linux/Debian 系统.您可以在下面看到我的代码的样子.

Could someone provide me with a solution, or a workaround, to help me? If possible, I'd like the solution to be applicable to either Windows and Linux/Debian systems. Below you can see how my code looks like.

非常感谢您的帮助.

import tkinter as tk

class MyListOfStuff(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.redraw()

def redraw(self):
    # (re)populate the frame with as many widgets as possible

class MyApp(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.pack(fill=tk.BOTH, expand=1)
        self.child_frame = MyListOfStuff(self)
        self.bind('<Configure>', self.resize_detected)

def resize_detected(self, *args):
    self.child_frame.redraw()
    # HERE LIES THE PROBLEM... 
    # This method gets called multiple times per second during the resizing 
    # of the main window, and the user experience is very bad because the 
    # resize action is not smooth and fluid. I'd like to call it only AFTER 
    # the resize has been completed (and quite possibly after the mouse 
    # button has been released).

def main():
    root = tk.Tk()
    my_app = MyApp(root)
    my_app.mainloop()

if __name__ == '__main__':
    main()

推荐答案

您是否尝试过已接受的答案中的建议?

Did you try the suggestion in accepted answer?

绑定的回调可以使用time.time()来存储当前调用的时间,如果在两秒内再次调用,则不执行.

The binded callback could employ time.time() to store the time of current call and if it is called again say within two seconds, then don't take action.

import time

def your_class:
    def __init__(self, ...):
        self.last_callback_time = time.time()
        # ... other init
# More stuff in your class
# ...
    def configure_callback(self, event):
        cur_time = time.time()
        if (cur_time - self.last_callback_time) > 2:
             # Do intended stuff
        self.last_callback_time = time.time()

这篇关于&lt;Configure&gt;的连续调用tkinter 中的事件(跟进)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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