wxPython 控件不可点击

发布于 2024-08-14 11:17:33 字数 1216 浏览 10 评论 0原文

//if I use BoxSizer instead of StaticBoxSizer, the button is clickable.
//if there is a radio button under StaticBoxSizer, it is clickable,
//but the button is not
row1 = wx.StaticBoxSizer(wx.StaticBox(panel, -1, 'this is row one'), orient=wx.HORIZONTAL)
row1.Add(label1,0,wx.TOP | wx.RIGHT,7)
row1.Add(self.fileCtrl)
row2 = wx.BoxSizer(wx.HORIZONTAL)

//for everything in row2, neither buttons nor radio buttons are clickable
actionBox = wx.StaticBoxSizer(wx.StaticBox(panel, -1, 'asdfasdf'), orient=wx.VERTICAL)

actionRow1 = wx.BoxSizer(wx.HORIZONTAL)
actionRow1.Add(wx.StaticText(panel, -1, 'blah blah ', (5, 5)))
actionRow1.Add(self.mailRadio)
actionRow2 = wx.BoxSizer(wx.HORIZONTAL)
actionRow2.Add(wx.StaticText(panel, -1, 'lah dee dah', (5, 5)))
actionRow2.Add(self.uploadRadio,5)
actionBox.Add(actionRow1,0,wx.EXPAND)
actionBox.Add(actionRow2)
row2.Add(actionBox)

wrapper = wx.FlexGridSizer(2,1,5,5)
wrapper.AddGrowableCol(0)
wrapper.Add(row1,0,wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,15)
wrapper.Add(row2,0,wx.ALL | wx.ALIGN_CENTER,15)
panel.SetSizerAndFit(wrapper)
self.Centre()
self.Fit()

我正在 OS X 中测试它,但我也需要它在 Windows 上工作。这对我来说很新,所以这让我很困惑。我必须设置类似 css z-index 的东西吗?

//if I use BoxSizer instead of StaticBoxSizer, the button is clickable.
//if there is a radio button under StaticBoxSizer, it is clickable,
//but the button is not
row1 = wx.StaticBoxSizer(wx.StaticBox(panel, -1, 'this is row one'), orient=wx.HORIZONTAL)
row1.Add(label1,0,wx.TOP | wx.RIGHT,7)
row1.Add(self.fileCtrl)
row2 = wx.BoxSizer(wx.HORIZONTAL)

//for everything in row2, neither buttons nor radio buttons are clickable
actionBox = wx.StaticBoxSizer(wx.StaticBox(panel, -1, 'asdfasdf'), orient=wx.VERTICAL)

actionRow1 = wx.BoxSizer(wx.HORIZONTAL)
actionRow1.Add(wx.StaticText(panel, -1, 'blah blah ', (5, 5)))
actionRow1.Add(self.mailRadio)
actionRow2 = wx.BoxSizer(wx.HORIZONTAL)
actionRow2.Add(wx.StaticText(panel, -1, 'lah dee dah', (5, 5)))
actionRow2.Add(self.uploadRadio,5)
actionBox.Add(actionRow1,0,wx.EXPAND)
actionBox.Add(actionRow2)
row2.Add(actionBox)

wrapper = wx.FlexGridSizer(2,1,5,5)
wrapper.AddGrowableCol(0)
wrapper.Add(row1,0,wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,15)
wrapper.Add(row2,0,wx.ALL | wx.ALIGN_CENTER,15)
panel.SetSizerAndFit(wrapper)
self.Centre()
self.Fit()

I'm testing this in OS X, but I need it to work on windows too. Pretty new to this so this is confusing me. Is there something like css z-index that I have to set?

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

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

发布评论

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

评论(2

蔚蓝源自深海 2024-08-21 11:17:33

这应该有效:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)

        self.label1 = wx.StaticText(self.panel)
        self.fileCtrl = wx.FilePickerCtrl(self.panel)
        self.mailRadio = wx.RadioButton(self.panel)
        self.uploadRadio = wx.RadioButton(self.panel)

        row1 = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, 'this is row one'), orient=wx.HORIZONTAL)
        row1.Add(self.label1,0,wx.TOP | wx.RIGHT,7)
        row1.Add(self.fileCtrl)

        row2 = wx.BoxSizer(wx.HORIZONTAL)
        actionBox = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, 'asdfasdf'), orient=wx.VERTICAL)
        actionRow1 = wx.BoxSizer(wx.HORIZONTAL)
        actionRow1.Add(wx.StaticText(self.panel, -1, 'blah blah ', (5, 5)))
        actionRow1.Add(self.mailRadio)
        actionRow2 = wx.BoxSizer(wx.HORIZONTAL)
        actionRow2.Add(wx.StaticText(self.panel, -1, 'lah dee dah', (5, 5)))
        actionRow2.Add(self.uploadRadio,5)
        actionBox.Add(actionRow1,0,wx.EXPAND)
        actionBox.Add(actionRow2)
        row2.Add(actionBox)

        wrapper = wx.FlexGridSizer(2,1,5,5)
        wrapper.AddGrowableCol(0)
        wrapper.Add(row1,0,wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,15)
        wrapper.Add(row2,0,wx.ALL | wx.ALIGN_CENTER,15)
        self.panel.SetSizerAndFit(wrapper)
        self.Centre()
        self.Fit()

        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

您的小部件似乎以错误的方式设置了父级。更改为以下内容时,我得到了“不可点击”的行为:

...
self.label1 = wx.StaticText(self)
self.fileCtrl = wx.FilePickerCtrl(self)
self.mailRadio = wx.RadioButton(self)
self.uploadRadio = wx.RadioButton(self)
...

This should work:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)

        self.label1 = wx.StaticText(self.panel)
        self.fileCtrl = wx.FilePickerCtrl(self.panel)
        self.mailRadio = wx.RadioButton(self.panel)
        self.uploadRadio = wx.RadioButton(self.panel)

        row1 = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, 'this is row one'), orient=wx.HORIZONTAL)
        row1.Add(self.label1,0,wx.TOP | wx.RIGHT,7)
        row1.Add(self.fileCtrl)

        row2 = wx.BoxSizer(wx.HORIZONTAL)
        actionBox = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, 'asdfasdf'), orient=wx.VERTICAL)
        actionRow1 = wx.BoxSizer(wx.HORIZONTAL)
        actionRow1.Add(wx.StaticText(self.panel, -1, 'blah blah ', (5, 5)))
        actionRow1.Add(self.mailRadio)
        actionRow2 = wx.BoxSizer(wx.HORIZONTAL)
        actionRow2.Add(wx.StaticText(self.panel, -1, 'lah dee dah', (5, 5)))
        actionRow2.Add(self.uploadRadio,5)
        actionBox.Add(actionRow1,0,wx.EXPAND)
        actionBox.Add(actionRow2)
        row2.Add(actionBox)

        wrapper = wx.FlexGridSizer(2,1,5,5)
        wrapper.AddGrowableCol(0)
        wrapper.Add(row1,0,wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,15)
        wrapper.Add(row2,0,wx.ALL | wx.ALIGN_CENTER,15)
        self.panel.SetSizerAndFit(wrapper)
        self.Centre()
        self.Fit()

        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

It seems that your widgets are parented in a wrong way. I got the "not-clickable" behavior when changing to this:

...
self.label1 = wx.StaticText(self)
self.fileCtrl = wx.FilePickerCtrl(self)
self.mailRadio = wx.RadioButton(self)
self.uploadRadio = wx.RadioButton(self)
...
冷月断魂刀 2024-08-21 11:17:33

我遇到了同样的问题,我刚刚找到了解决方案。在 MacOS X 中,小部件的创建顺序非常重要。

StaticBoxSizer 必须在所有包含的小部件之前创建。否则这些元素可能无法单击。

不可点击按钮示例:

import wx

class MainWindow(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, None, -1, 'not-clickable')

    self.panel = wx.Panel(self)

    button = wx.Button(self.panel, -1, "Can't click me")

    sttBox = wx.StaticBox(self.panel, -1, "Created after button")
    sizer = wx.StaticBoxSizer(sttBox, wx.HORIZONTAL)
    sizer.Add(button)

    self.panel.SetSizerAndFit(sizer)
    self.Centre()
    self.Fit()


if __name__ == '__main__':
  app = wx.PySimpleApp()
  frame = MainWindow()
  frame.Show()
  app.MainLoop()

可点击按钮示例:

import wx

class MainWindow(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, None, -1, 'clickable')

    self.panel = wx.Panel(self)
    sttBox = wx.StaticBox(self.panel, -1, "Created before button")

    button = wx.Button(self.panel, -1, "I'm clickable")

    sizer = wx.StaticBoxSizer(sttBox, wx.HORIZONTAL)
    sizer.Add(button)

    self.panel.SetSizerAndFit(sizer)
    self.Centre()
    self.Fit()


if __name__ == '__main__':
  app = wx.PySimpleApp()
  frame = MainWindow()
  frame.Show()
  app.MainLoop()

I had the same problem and I've just found the solution. It comes that in MacOS X the order of creation of the widgets is very important.

The StaticBoxSizer must be created before all the contained widgets. Otherwise those elements would probably be not-clickable.

Not-clickable button example:

import wx

class MainWindow(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, None, -1, 'not-clickable')

    self.panel = wx.Panel(self)

    button = wx.Button(self.panel, -1, "Can't click me")

    sttBox = wx.StaticBox(self.panel, -1, "Created after button")
    sizer = wx.StaticBoxSizer(sttBox, wx.HORIZONTAL)
    sizer.Add(button)

    self.panel.SetSizerAndFit(sizer)
    self.Centre()
    self.Fit()


if __name__ == '__main__':
  app = wx.PySimpleApp()
  frame = MainWindow()
  frame.Show()
  app.MainLoop()

Clickable button example:

import wx

class MainWindow(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, None, -1, 'clickable')

    self.panel = wx.Panel(self)
    sttBox = wx.StaticBox(self.panel, -1, "Created before button")

    button = wx.Button(self.panel, -1, "I'm clickable")

    sizer = wx.StaticBoxSizer(sttBox, wx.HORIZONTAL)
    sizer.Add(button)

    self.panel.SetSizerAndFit(sizer)
    self.Centre()
    self.Fit()


if __name__ == '__main__':
  app = wx.PySimpleApp()
  frame = MainWindow()
  frame.Show()
  app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文