相互依赖的 PyGtk SpinButton

发布于 2024-11-09 14:22:49 字数 471 浏览 0 评论 0原文

我有三个相互依赖的 PyGTK SpinButton:将它们称为 A、B 和 C。还有另一个按钮 D,可以在 C 的两个相互定义之间切换,这样 C 的值被定义为 A/B,除非 D已被按下奇数次。

例如,我希望能够根据这种关系更改 A 并让 B C 立即更新。我还希望能够更改 B 并立即更新 A 或 C。 C 也是如此。我决定,当其中一个发生更改时,应更新最近最少更改的 SpinButton,并且如果自程序开始以来没有更改过 SpinButton,则应假定 C 最近已更改。

以上一切均已实施。我的应用程序侦听每个 SpinButton 的值更改信号,执行上述计算,然后按所述更新其他两个的值。但是,应用程序会获取自己的更改并对其做出响应。当我更改 A 时,我的程序会更改 B,而 B 是通过更改 C 来处理的,依此类推。我也尝试过更改值信号,但它不响应 SpinButton 的更改。

如何以不会由程序本身触发的方式监听 SpinButton 值的变化?

I have three PyGTK SpinButtons that are mutually dependent on each other: call them A, B, and C. There is another button D that toggles between two reciprocal definitions of C, such that the value of C is defined as A/B unless D has been pressed an odd number of times.

I want to be able, for example, to change A and have B or C update immediately according to this relationship. I also want to be able to change B and have A or C update immediately. Same goes for C. I've decided that when one is changed, the SpinButton that was least recently changed should be updated, and that if no SpinButtons have been changed since the program began, it should be assumed that C was changed most recently.

Everything above has been implemented. My application listens for the value-changed signals for each of the SpinButtons, performs the calculations above, and then updates the value of the other two as described. However, the application picks up its own changes and responds to them. When I change A, my program changes B, which is handled by changing C, and so on. I have tried the change-value signal too but it does not respond to changes to the SpinButton.

How do I listen for a change in value of a SpinButton in a way that will not be triggered by the program itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-11-16 14:22:49

您可以在调用 set_value 之前禁用给定微调器的信号处理程序。这确保了信号仅在响应鼠标和键盘事件时触发,而不是在编程更改时触发。这是一个简单的例子:

import pygtk, gtk
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect('destroy', lambda w: gtk.main_quit())

def _add_spinner():
    adj = gtk.Adjustment(0.0, -100.0, 100.0, 1.0, 2.0, 0.0)
    spin = gtk.SpinButton(adj, 0, 1)
    spin.set_wrap(True)
    hbox.pack_start(spin, True, True, 10)
    return spin

vbox = gtk.VBox(True, 10)
hbox = gtk.HBox(True, 10)
vbox.pack_start(hbox, True, True, 10)
spin1 = _add_spinner()
spin2 = _add_spinner()
win.add(vbox)

def set_spin(src, dst):
    # ensure that calling `set_value` does not fire events
    hid = handlers.get(dst)
    dst.handler_block(hid)
    dst.set_value(src.get_value() + 1.5)
    dst.handler_unblock(hid)

# when 'spinN' is changed, call 'set_spin'
hid1 = spin1.connect('value-changed', set_spin, spin2)
hid2 = spin2.connect('value-changed', set_spin, spin1)
handlers = {spin1: hid1, spin2: hid2}
win.show_all()
gtk.main()

You can disable the signal handler for a given spinner before calling set_value on it. This ensures that the signal is only fired in reaction to mouse and keyboard events, not programmatic changes. Here is a quick example:

import pygtk, gtk
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect('destroy', lambda w: gtk.main_quit())

def _add_spinner():
    adj = gtk.Adjustment(0.0, -100.0, 100.0, 1.0, 2.0, 0.0)
    spin = gtk.SpinButton(adj, 0, 1)
    spin.set_wrap(True)
    hbox.pack_start(spin, True, True, 10)
    return spin

vbox = gtk.VBox(True, 10)
hbox = gtk.HBox(True, 10)
vbox.pack_start(hbox, True, True, 10)
spin1 = _add_spinner()
spin2 = _add_spinner()
win.add(vbox)

def set_spin(src, dst):
    # ensure that calling `set_value` does not fire events
    hid = handlers.get(dst)
    dst.handler_block(hid)
    dst.set_value(src.get_value() + 1.5)
    dst.handler_unblock(hid)

# when 'spinN' is changed, call 'set_spin'
hid1 = spin1.connect('value-changed', set_spin, spin2)
hid2 = spin2.connect('value-changed', set_spin, spin1)
handlers = {spin1: hid1, spin2: hid2}
win.show_all()
gtk.main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文