Iancovici
Iancovici

Reputation: 5731

How to make additional window in wxpython on a click of a button

I need to make an additional new window in wxpython on a click of a button (which is physically seperate from primary window). I want to do this without closing the last window.

This is what I have so far:

class Prototype(wx.Frame):
     def __init__(self, parent, title):
          wx.Frame.__init__(self, None, size=(1240,705))
          self.UI()
          self.Centre()
          self.Show()

     def UI(self):
          self.panel1 = wx.Panel(self, -1)
          self.sizer = wx.BoxSizer()
          self.sizer.Add(self.panel1, 1, flag=wx.EXPAND) 
          b = wx.Button(self.panel1, label='second window', size=(180,100), pos=(650,25))
          b.Bind(wx.EVT_BUTTON, self.OnB)        

          self.panel2 = wx.Panel(self, -1)
          self.sizer.Add(self.panel2,1,flag=wx.EXPAND)
          self.panel2.Hide()
          self.panel2.SetSizer(self.vbox)
          self.SetSizer(self.sizer)  

     def OnB(self, event):
          self.panel2.Show()
          self.sizer.Layout() 

app = wx.App()
Prototype(None, title='')
app.MainLoop()

Upvotes: 3

Views: 6152

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

Here's a working example:

 import wx

 ########################################################################
 class SecondFrame(wx.Frame):
      """"""

      #----------------------------------------------------------------------
      def __init__(self):
           """Constructor"""
           wx.Frame.__init__(self, None, title="Second Frame")
           panel = wx.Panel(self)
           txt = wx.StaticText(panel, label="I'm the second frame!")

 ########################################################################
 class Prototype(wx.Frame):

      #----------------------------------------------------------------------
      def __init__(self, parent, title):
           wx.Frame.__init__(self, None, title="First Frame", size=(1240,705))
           self.UI()
           self.Centre()
           self.Show()

      #----------------------------------------------------------------------
      def UI(self):
           self.panel1 = wx.Panel(self, -1)
           self.sizer = wx.BoxSizer()
           self.sizer.Add(self.panel1, 1, flag=wx.EXPAND) 
           b = wx.Button(self.panel1, label='second window', size=(180,100), pos=(650,25))
           b.Bind(wx.EVT_BUTTON, self.OnB)        

           self.SetSizer(self.sizer)  

      #----------------------------------------------------------------------
      def OnB(self, event):
           frame = SecondFrame()
           frame.Show()

 #----------------------------------------------------------------------
 app = wx.App(False)
 Prototype(None, title='')
 app.MainLoop()

I don't really recommend mixing sizers and absolute positioning though. You should use one or the other. I recommend using sizers unless you plan to make frames that cannot be resized.

Upvotes: 4

Fenikso
Fenikso

Reputation: 9451

Just create a new wx.Frame as you already did in your app. Add something along the lines to your button event handler:

self.second_window = wx.Frame(None)
self.second_window.Show()

Upvotes: 0

Related Questions