KKS
KKS

Reputation: 1389

Dropdown of wxPython combobox doesn't work on popup window

When I put a combo box on wxPython popup window, dropdown function doesn't work.

My example code is this.

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()

In the code, combo box in main frame works well. But, in the popup window, which is shown when 'select Comport' button is clicked, combobox doesn't work.

What's wrong with this?

enter image description here

It works well.

enter image description here

It doesn't work.

Upvotes: 0

Views: 711

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22448

The ComboBox certainly works in a popup window under Linux, so it's difficult to address your question directly. However, I would suggest that in this case, you might well be better served, if you were to use a Dialog rather than a PopUpWindow, as it will do the heavy lifting for you.
For example:

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

Upvotes: 1

Related Questions