Tkinter:在不触发回调的情况下设置“比例"值? [英] Tkinter: Set a 'scale' value without triggering callback?

查看:23
本文介绍了Tkinter:在不触发回调的情况下设置“比例"值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Tkinter GUI,其中有一个 Scale 对象.我分配了一个回调(通过 command 构造函数参数)在用户更改比例位置时执行操作.但是,也有外部修改比例尺表示的值的情况,所以我使用Scale.set()来设置比例尺位置.在这种情况下,我想设置比例,但触发回调,因为程序的其余部分已经知道更改.但是,我注意到回调确实是由 set() 触发的.

I have a Tkinter GUI where there is a Scale object. I have a callback assigned (by the command constructor parameter) to perform an action when the user changes the scale position. However, there is also a case where the value represented by the scale is modified externally, and so I set the scale position using Scale.set(). In this case, I want to set the scale, but not trigger the callback, since the rest of the program already knows about the change. However, I notice that the callback is indeed triggered by set().

是否可以执行以下操作之一:

Is it possible to do one of:

  1. 在不触发回调的情况下设置比例值.

  1. Set the scale value without triggering the callback.

区分回调是由用户交互触发还是由 Scale.set() 被调用.

Differentiate in the callback whether it was triggered by user interaction or by Scale.set() being called.

谢谢.

推荐答案

Tkinter 没有专门内置任何东西来解决这个问题.不过,这确实是一个要解决的简单问题:删除回调,设置值,添加回调.或者,设置一个全局标志并在回调中检查该标志.

There is nothing specifically built-in to Tkinter to solve this. It's really a simple problem to solve though: remove the callback, set the value, add the callback. Or, set a global flag and check for that flag in the callback.

有一些方法可以解决这个问题——例如,子类化小部件——但这并不能真正给你带来任何好处.只需采用简单的解决方案,然后继续解决更有趣的问题.

There are ways to solve the problem -- subclass the widget, for example -- but that doesn't really buy you anything. Just go with the simple solution and move on to more interesting problems.

这篇关于Tkinter:在不触发回调的情况下设置“比例"值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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