wx.RB_SINGLE使按钮在使用wxPython时无法取消选择。

huangapple go评论56阅读模式
英文:

wx.RB_SINGLE makes button unable to be deselected with wxPython

问题

根据 wxWidgets 文档1wxRB_SINGLE 要求程序员从按钮的事件处理程序中手动处理选择和取消选择单选按钮。然而,尽管我试过了各种方法,但 wxPython 不允许我这样做。我尝试直接在单选按钮上调用 SetValue(False),还尝试从 wx.EVT_RADIOBUTTON 处理程序中调用相同的函数,但无论如何,单选按钮仍然保持选中状态,GetValue() 返回 True。发生了什么?我是否对 wx.RB_SINGLE 有基本误解?

为了澄清,我的目标是让单选按钮在程序员的角度看起来像复选框一样运行。我希望自己处理它们的确切行为。我不希望 wxWidgets 自动分组。

以下是我用于调试的代码:

import wx

class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        panel = wx.Panel(self)
        rb1 = wx.RadioButton(panel,
                                  label = 'Button 1',
                                  pos = (30, 10),
                                  style=wx.RB_SINGLE
                                  )
        rb2 = wx.RadioButton(panel,
                                  label = 'Button 2',
                                  pos = (30, 30),
                                  style=wx.RB_SINGLE
                                  )
        rb3 = wx.RadioButton(panel,
                                  label = 'Button 3',
                                  pos = (30, 50),
                                  style=wx.RB_SINGLE
                                  )
        rb2.Bind(wx.EVT_RADIOBUTTON, lambda x: rb2.SetValue(False))
        rb2.SetValue(False)


def main():
    app = wx.App()
    ex = Example(None)
    ex.Show(True)
    app.MainLoop()

if __name__ == '__main__':
    main()

值得一提的是,我正在使用 Ubuntu Linux 22.04 LTS,使用 GTK(版本 3.24.33-1ubuntu2)后端运行 wxWidgets(通过 wxPython 4.2.0),Python 版本为 3.10.6。

英文:

According to the wxWidgets docs1, wxRB_SINGLE requires the programmer to manually handle selecting and deselecting radio buttons from the event handler for the button. However, try as I might, wxPython doesn't let me do that. I tried both directly calling SetValue(False) on the radio button and calling the same function from the wx.EVT_RADIOBUTTON handler, but regardless the radio button stays selected and GetValue() returns True. What gives? Am I fundamentally misunderstanding something about wx.RB_SINGLE?

To clarify, my goal is to have radio buttons that — from the programmer's perspective — function like checkboxes. I want to handle their exact behaviour myself. I don't want any automatic grouping done by wxWidgets.

Here is the code I've been using to debug this:

import wx

class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        panel = wx.Panel(self)
        rb1 = wx.RadioButton(panel,
                                  label = 'Button 1',
                                  pos = (30, 10),
                                  style=wx.RB_SINGLE
                                  )
        rb2 = wx.RadioButton(panel,
                                  label = 'Button 2',
                                  pos = (30, 30),
                                  style=wx.RB_SINGLE
                                  )
        rb3 = wx.RadioButton(panel,
                                  label = 'Button 3',
                                  pos = (30, 50),
                                  style=wx.RB_SINGLE
                                  )
        rb2.Bind(wx.EVT_RADIOBUTTON, lambda x: rb2.SetValue(False))
        rb2.SetValue(False)


def main():
    app = wx.App()
    ex = Example(None)
    ex.Show(True)
    app.MainLoop()

if __name__ == '__main__':
    main()

For what it's worth, I'm using Ubuntu Linux 22.04 LTS with the GTK (version 3.24.33-1ubuntu2) backend for wxWidgets (via wxPython 4.2.0) running on Python 3.10.6.

答案1

得分: 0

看起来你运气不太好,那个样式似乎是一个没有经过深思熟虑的选项,因为显然它不像你期望的那样工作。
你的选择被限制在 wx.CheckBox 或者你可以查看这里提供的备选项 onoffbutton.py
https://discuss.wxpython.org/t/an-on-off-button-alternative-to-checkbox/36304/19

剧透一下,我就是作者。
它有各种类型的单选开关按钮 - 从主题的最后一个帖子中获取zip文件。

英文:

It looks like you're out of luck, that style seems like an ill thought out option, because it clearly doesn't work, as you'd expect it to.
Your options are limited to a wx.CheckBox or you could look at an alternative onoffbutton.py available here:
https://discuss.wxpython.org/t/an-on-off-button-alternative-to-checkbox/36304/19

Spolier alert, I'm the author.
It has a variety of Radio type on/off buttons - get the zip file from the last post on the thread.

答案2

得分: 0

这是由于wxGTK中的一个错误。我提交了一个pull request来修复这个问题,根据维护者的评论,似乎将在wxWidgets 3.3.0中合并。如果您正在使用较旧的版本,很遗憾我不知道有任何解决方法。

英文:

This is due to a bug in wxGTK. I submitted a pull request to fix this, which according to the maintainer's comments seems like it will be merged for wxWidgets 3.3.0. If you're using an older version, I don't know of any workaround unfortunately.

huangapple
  • 本文由 发表于 2023年6月18日 21:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定