在tkinter中连续调用Configure事件 [英] continuous call of the Configure event in tkinter

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

问题描述

我正在尝试编写一个函数来动态调整tkinter窗口中显示的图像的大小. 因此,我将此函数绑定到Configure事件:

I am trying to write a function to dynamically resize an image displayed in a tkinter window. Therefore I bound this function to the Configure event:

connroot.bind( "<Configure>", connresiz)

我的问题是

1)connresiz()函数在程序启动时被调用3次(为什么3?),并且

1) that the connresiz() function gets called 3 times (why 3?) at program start, and

2)更麻烦的是,当我拖动鼠标时,动态调整窗口大小会连续调用该函数!如何避免这种情况?

2) more troublesome, that dynamically resizing the window calls the function continuously as I drag the mouse! How can avoid this?

我考虑过要同时检查<Configure><ButtonRelease-1>事件是否存在,但是我不知道如何编码

I thought about checking at the simultaneous presence of a <Configure> and <ButtonRelease-1> events, but I dont know how to code it

推荐答案

根据官方的tk文档,<Configure>事件会在其大小,位置或边框宽度发生更改时,有时甚至在其位置更改时触发"堆叠顺序."在启动过程中,这种情况可能会发生多次.

According to the official tk documentation, <Configure> events fire "whenever its size, position, or border width changes, and sometimes when it has changed position in the stacking order." This can happen several times during startup.

由于窗口小部件的大小正在改变,因此在调整窗口大小时会连续调用它.这就是定义要执行的操作.尽管可以修改回调中的操作,但不能阻止它被调用.例如,您可以延迟调整图像的大小,直到一两秒钟都没有收到另一个<Configure>事件为止-这很可能意味着用户已停止交互式调整大小.

It is called continuously while you resize the window because the size of the widget is changing. That's what it's defined to do. You can't prevent it from being called, though you can certainly modify what you do in the callback. For example, you could delay resizing the image until you've not received another <Configure> event for a second or two -- which likely means the user has stopped interactive resizing.

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

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