简体   繁体   中英

how to add a window to a frame in wxpython

How to open the window inside a frame in wxpython. I want to open a window on clicking File->SubFile .The code is given below.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class MainMenu(wx.Frame):

def __init__(self, *args, **kwargs):
   # ID_F = 1
    super(MainMenu, self).__init__(*args, **kwargs) 
    self.Maximize(True)    

    self.InitUI()

def InitUI(self):
    #self.Bind(wx.EVT_MENU, self.OnAbout, None,id= 1)
    fileMenu1 = wx.Menu() 
    menubar = wx.MenuBar()
    fitem1 = fileMenu1.Append(1, '&Sub File\tCtrl+B', 'Sub File..')
    menubar.Append(fileMenu1,  '&File')
    self.SetMenuBar(menubar)
    self.SetTitle('Simple menu')
    self.Centre()
    self.Show(True)

def OnAbout(self, event):
     AboutFrame().Show()



class AboutFrame(wx.Frame):


def __init__(self):
     wx.Frame.__init__(self, parent, 5, 'New Window', size=(400,300))
     wx.Frame.CenterOnScreen(self)


def main():
    ex = wx.App()
    MainMenu(None)
    ex.MainLoop()    

if __name__ == '__main__':
    main()

I thought it would be simple. How to center the subwindow on the screen. I'm incredibly new to this,

Note To respond to a menu selection, provide a handler for EVT_MENU [for] the frame that contains the menu.
http://wxpython.org/Phoenix/docs/html/MenuBar.html#menubar

...

wx.Frame methods:
Center()
http://wxpython.org/Phoenix/docs/html/Frame.html?highlight=frame#Frame.Centre

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class MainMenu(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(MainMenu, self).__init__(*args, **kwargs) 
        self.Maximize(True)    

        self.InitUI()

    def InitUI(self):
        fileMenu1 = wx.Menu() 
        menubar = wx.MenuBar()

        #********HERE************
        fitem1 = fileMenu1.Append(-1, '&Sub File\tCtrl+B', 'Sub File..')
        fitem2 = fileMenu1.Append(-1, '&Dog\tCtrl+D', 'Sub Dog..')

        self.Bind(wx.EVT_MENU, self.onclick_subfile, fitem1)
        #*************************

        menubar.Append(fileMenu1,  '&File')
        self.SetMenuBar(menubar)
        self.SetTitle('Simple menu')
        self.Centre()
        self.Show(True)

    def OnAbout(self, event):
         AboutFrame().Show()

    #**********HERE*************
    def onclick_subfile(self, event):
        frame = wx.Frame(None, -1, "My Second Frame")
        frame.Center()
        frame.Show()




class AboutFrame(wx.Frame):

    def __init__(self):
         wx.Frame.__init__(self, parent, 5, 'New Window', size=(400,300))
         wx.Frame.CenterOnScreen(self)


    def main():
        ex = wx.App(redirect=False)
        MainMenu(None)
        ex.MainLoop()    

    if __name__ == '__main__':
        main()

If you want the inner window to be destroyed when you close the outer window, then make the outer window the parent of the inner window:

    def onclick_subfile(self, event):
        frame = wx.Frame(self, -1, "My Second Frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM