Domagoj
Domagoj

Reputation: 1705

wxPython childframes interaction

I've created a simple program that consists from one mainframe(Frame) and two childframes (ChildFrame1 and ChildFrame2). Mainframe has two buttons one will check if the ChildFrame1 is already created and create the same if not and the other will check if the ChildFrame2 is already created and create the same if not. Now the tricky part (at least tricky for me), ChildFrame1 has a button that needs to check if the ChildFrame2 is already created from mainframe and if not create it. In my code this button just creates another ChildFrame2 alongside previously created ChildFrame2 from mainframe. How can I make this to work? Basically I have two buttons on two frames but one event.

Code

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,'Parent')
        panel = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(panel, -1, 'Open Child1')
        button2 = wx.Button(panel, -1, 'Open Child2')

        sizer.Add(button, 0, wx.CENTER|wx.ALL, 5)
        sizer.Add(button2, 0, wx.CENTER|wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.onButton,  button)
        self.Bind(wx.EVT_BUTTON, self.onButton2,  button2)

    def onButton(self, e):
        try:
            self.ChildF.Show()
        except:
            self.ChildF = ChildFrame1()
            self.ChildF.Show()

        self.ChildF.SetFocus()

    def onButton2(self, e):
        try:
            self.ChildF2.Show()
        except:
            self.ChildF2 = ChildFrame2()
            self.ChildF2.Show()

        self.ChildF2.SetFocus()

class ChildFrame1(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child1')
        panel = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(panel, -1, 'Open Child2')
        sizer.Add(button, 0, wx.CENTER|wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.OnButton, button)

    def OnButton(self,e):
        try:
            self.ChildF.Show()
        except:
            self.ChildF = ChildFrame2()
            self.ChildF.Show()

        self.ChildF.SetFocus()

class ChildFrame2(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child2')

if __name__ == '__main__':
    app = wx.App()
    frame = Frame().Show()
    app.MainLoop()

Upvotes: 0

Views: 113

Answers (1)

Yoriz
Yoriz

Reputation: 3625

You could Bind ChildFrame1's button handler to the parent frames method for opening/showing ChildFrame2.

import wx


class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, 'Parent')
        panel = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(panel, -1, 'Open Child1')
        button2 = wx.Button(panel, -1, 'Open Child2')

        sizer.Add(button, 0, wx.CENTER | wx.ALL, 5)
        sizer.Add(button2, 0, wx.CENTER | wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.onButton, button)
        self.Bind(wx.EVT_BUTTON, self.onButton2, button2)

    def onButton(self, e):
        try:
            self.ChildF.Show()
        except:
            self.ChildF = ChildFrame1()
            self.ChildF.Show()

        self.ChildF.SetFocus()

    def onButton2(self, e):
        try:
            self.ChildF2.Show()
        except:
            self.ChildF2 = ChildFrame2()
            self.ChildF2.Show()

        self.ChildF2.SetFocus()


class ChildFrame1(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child1')
        panel = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(panel, -1, 'Open Child2')
        sizer.Add(button, 0, wx.CENTER | wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.GetParent().onButton2, button)


class ChildFrame2(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child2')

if __name__ == '__main__':
    app = wx.App()
    frame = Frame().Show()
    app.MainLoop()

Upvotes: 1

Related Questions