user1401950
user1401950

Reputation: 995

How to open up a new child window when a button is clicked? (wxpython)

How do you open a child frame when a button is clicked on the main window? The code below creates a drop down box. My problem is I have a separate class for the main window and I don't how to open this new drop down box window in my main application. The main window is just a wx.frame with a button added to it.

import wx
class MyFrame(wx.Frame):

    def __init__(self ):
        wx.Frame.__init__(self, None, -1, 'wxChoice test', size=(300, 150))
        colorList = ['blue','green','yellow','red']
        # create the dropdown box
        self.choice1 = wx.Choice(self, -1, choices=colorList)

        # select item 1 = 'green' to show
        self.choice1.SetSelection(1)
        # set focus to receive optional keyboard input
        # eg. type r for red, y for yellow
        self.choice1.SetFocus()
        # new event handler wxPython version 2.5 and higher
        self.choice1.Bind(wx.EVT_CHOICE, self.onChoice)
    def onChoice(self, event):
        '''get the slected color choice'''
        self.color = self.choice1.GetStringSelection()
        self.SetTitle(self.color) # test



# this is only a small application
application = wx.PySimpleApp()
# call class MyFrame
frame1 = MyFrame()
# show the frame
frame1.Show(True)
# start the event loop
application.MainLoop()

Upvotes: 1

Views: 3915

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114098

simplest example I could make in a few mins import wx

def show_other(evt):
    f2 = wx.Frame(None,-1)
    c = wx.Choice(f2,-1,choices=['red','blue','green'])
    f2.Show()

a = wx.App(redirect = False)


f = wx.Frame(None,-1)
b = wx.Button(f,wx.ID_OK)
b.Bind(wx.EVT_BUTTON,show_other)
f.Show()
a.MainLoop()

Upvotes: 1

Related Questions