wxPython组合框的下拉菜单在弹出窗口中不起作用

问题描述 投票:0回答:1

当我在wxPython弹出窗口上放置一个组合框时,下拉功能不起作用。

我的示例代码是这个。

import wx

class TestPopup(wx.PopupWindow):

    def __init__(self, parent):
        """Constructor"""
        wx.PopupWindow.__init__(self, parent = parent)

        self.popUp = wx.Panel(self, size = (200,200))
        self.popUp.SetBackgroundColour("white")

        self.st = wx.StaticText(self.popUp, -1, " Select Comport", pos=(10,10))
        self.selCom = wx.ComboBox(self.popUp, -1, pos=(85, 50), choices=["Com1", "Com2"])

        self.mySizer = wx.BoxSizer(wx.VERTICAL)
        self.mySizer.Add(self.popUp)
        self.SetSizerAndFit(self.mySizer)


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title= "ComboBox Test", size = (300,200))
        self.panel = wx.Panel(self)
        self.selComButton = wx.Button(self.panel, -1, "Select Comport")
        self.selComButton.Bind(wx.EVT_BUTTON, self.selectPopUp)
        self.selCom = wx.ComboBox(self.panel, -1, pos = (85, 50),choices=["Com1", "Com2"])


    def selectPopUp(self, event):

        win = TestPopup(self.GetTopLevelParent())
        btn = event.GetEventObject()
        pos = btn.ClientToScreen((0, 0))
        sz = btn.GetSize()
        win.Position(pos, (0, sz[1]))

        win.Show(True)


if __name__ == "__main__":
    app = wx.App()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

在代码中,主机中的组合框效果很好。但是,在单击“选择Comport”按钮时显示的弹出窗口中,组合框不起作用。

这怎么了?

enter image description here

效果很好。

enter image description here

不起作用。

python wxpython
1个回答
1
投票

ComboBox当然可以在Linux下的弹出窗口中使用,因此很难直接解决您的问题。但是,我建议在这种情况下,如果您使用Dialog而不是PopUpWindow,则可能会更好地为您服务,因为这会给您带来沉重的负担。例如:

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title= "Communication Port", size = (300,200))
        self.panel = wx.Panel(self)
        self.selComButton = wx.Button(self.panel, -1, "Select Comport")
        self.selComButton.SetToolTip("Select Comport")
        self.selComButton.Bind(wx.EVT_BUTTON, self.selectPopUp)

    def selectPopUp(self, event):
        dlg = wx.SingleChoiceDialog(None,"Pick a com port", "Com ports",["Com1","Com2","Com3","Com4"],wx.CHOICEDLG_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            res = dlg.GetStringSelection()
            self.selComButton.SetLabel(res)
        dlg.Destroy()

if __name__ == "__main__":
    app = wx.App()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.