弹出窗口内的复选框

2024-04-23 22:37:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找出为什么wx.CheckListBox在弹出窗口中不能正常工作。滚动条可以工作,但我无法选择任何内容。我尝试了多种类型的弹出窗口,但仍然得到相同的行为

除了使用框架,还有其他解决方法吗

import wx

class MyFrame(wx.Frame):    
    def __init__(self):
        super().__init__(parent=None, title='Hello World')
        panel = wx.Panel(self)

        

        
        #self.text_ctrl = wx.TextCtrl(panel, pos=(5, 5))
        my_btn = wx.Button(panel, label='Press Me', pos=(5, 55))
        my_btn.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient)

        self.Show()


    def OnShowPopupTransient(self, evt):
        sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
                      'twelve', 'thirteen', 'fourteen']
        win = TestTransientPopup(self,
                                    wx.SIMPLE_BORDER,
                                    sampleList)

        
        # Show the popup right below or above the button
        # depending on available screen space...
        btn = evt.GetEventObject()
        pos = btn.ClientToScreen( (0,0) )
        sz =  btn.GetSize()
        win.Position(pos, (0, sz[1]))

        win.Popup()

class TestTransientPopup(wx.PopupTransientWindow):
    """Adds a bit of text and mouse movement to the wx.PopupWindow"""
    def __init__(self, parent, style, sampleList):
        wx.PopupTransientWindow.__init__(self, parent, style)
    
        panel = wx.Panel(self)
        panel.SetBackgroundColour("#FFB6C1")
        
        lb = wx.CheckListBox(panel, -1, (80, 50), wx.DefaultSize, sampleList)
        
        btn = wx.Button(panel, -1, "Press Me")
        spin = wx.SpinCtrl(panel, -1, "Hello", size=(100,-1))
        #btn.Bind(wx.EVT_BUTTON, self.OnButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lb, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        sizer.Add(spin, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

        sizer.Fit(panel)
        sizer.Fit(self)
        self.Layout()

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()


1条回答
网友
1楼 · 发布于 2024-04-23 22:37:14

您没有指定您的平台或wxPython版本,但在Linux wxPython'4.1.1 gtk3(phoenix)wxWidgets 3.1.5'上,以下代码似乎可以正常工作。(除非你期待其他的东西)

import wx

class MyFrame(wx.Frame):    
    def __init__(self):
        super().__init__(parent=None, title='Hello World')
        panel = wx.Panel(self)
        my_btn = wx.Button(panel, label='Press Me', pos=(5, 55))
        my_btn.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient)
        self.Show()

    def OnShowPopupTransient(self, evt):
        sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
                      'twelve', 'thirteen', 'fourteen']
        win = TestTransientPopup(self, wx.BORDER_SIMPLE, sampleList)
        # Show the popup right below or above the button
        # depending on available screen space...
        btn = evt.GetEventObject()
        pos = btn.ClientToScreen( (0, 0) )
        sz =  btn.GetSize()
        win.Position(pos, (0, sz[1]))
        win.Popup(focus=self)


class TestTransientPopup(wx.PopupTransientWindow):
    """Adds a bit of text and mouse movement to the wx.PopupWindow"""
    def __init__(self, parent, style, sampleList):
        wx.PopupTransientWindow.__init__(self, parent, style)
        self.SetSize((150, 300))    
        panel = wx.Panel(self, size=(150, 300))
        panel.SetBackgroundColour("dark grey")
        
        self.lb = wx.CheckListBox(panel, -1, choices=sampleList)
        
        btn = wx.Button(panel, -1, "Press Me")
        self.spin = wx.SpinCtrl(panel, -1, "10")
        btn.Bind(wx.EVT_BUTTON, self.OnButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.lb, 1, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        sizer.Add(self.spin, 0, wx.ALL, 5)
        panel.SetSizer(sizer)
        self.Layout()

    def OnButton(self, event):
        checked_items = self.lb.GetCheckedStrings()
        spin_value = self.spin.GetValue()
        print("Selected items", checked_items)
        print("Spin value", spin_value)
        self.Dismiss()

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

enter image description here

相关问题 更多 >