Child frame autospawn and not reactive if reopening it after having been closed

1,337 views
Skip to first unread message

chris tophe

unread,
Jul 24, 2019, 11:06:23 AM7/24/19
to wxPython-users
hi all,
i use MVC pattern in which run.py have bind method.
it is great , it helps to organize my project,  but when doing this, child frame spawns when i launch my prog. And when i close the child window, if i want it again from main frame, it's not reactive anymore if i click "ok".

here is my files:

run.py (controller and starter)
import wx

from top import Top
from v_WC import v_WC

class Controller:
   
def __init__(self, app):
       

       
self.ct_top = Top(None)
       
       
self.ct_top.button_viewWC.Bind(wx.EVT_BUTTON, self.ct_top.Onbutton_viewWCButton, id=-1)        

       
self.ct_v_WC = v_WC(parent = self.ct_top)
       
       
self.ct_v_WC.btn_ok.Bind(wx.EVT_BUTTON, self.ct_v_WC.OnClose)
       
app
= wx.App(False)

controller
= Controller(app)
controller
.ct_top.Show()
app
.MainLoop()



here my main frame (top.py)


import wx
import wx.lib.dialogs

import v_WC

class Top(wx.Frame):
   
def __init__(self, parent):
        wx
.Frame.__init__(self, id=-1, parent=parent, size=wx.Size(800, 600), title='Top')
       
self.panel1 = wx.Panel(self, id=-1)      

       
self.button_viewWC = wx.Button(self.panel1, id=-1, label='viewWC', pos=wx.Point(96, 200), size=wx.Size(187, 28), style=0)

   
def Onbutton_viewWCButton(self, event):
       
self.v_WC = v_WC.create(self)
       
self.v_WC.Show()
       
event.Skip()




here is my child frame (v_WC.py)


import wx
 
def create(parent):
   
return v_WC(parent)
 
class v_WC(wx.Frame):
   
def __init__(self, parent):
       
        wx
.Frame.__init__(self, id=-1, parent=parent, title='v_WC', size=(700, 550))
       
self.panel1 = wx.Panel(self, id=-1, size=wx.Size(700, 550))

       
self.btn_ok = wx.Button(self.panel1, label="Ok ", pos=(20, 400), size=(60, -1))
       
self.Centre()
       
self.Show(True)

   
def OnClose(self, e):
       
self.Close(True)


Robin Dunn

unread,
Jul 25, 2019, 11:18:29 PM7/25/19
to wxPython-users
On Wednesday, July 24, 2019 at 8:06:23 AM UTC-7, chris tophe wrote:
hi all,
i use MVC pattern in which run.py have bind method.
it is great , it helps to organize my project,  but when doing this, child frame spawns when i launch my prog. And when i close the child window, if i want it again from main frame, it's not reactive anymore if i click "ok".

If I understand correctly, the problem is that in the Controller you are binding the EVT_BUTTON event for the first child window, but all future instances of the child windows are not getting any event bindings for their Ok button. You will probably have more luck if you do your event bindings in the classes that create the items sending the events. It's much easier to not get lost handling the events locally. If they need to inform other objects that the event happened, then that can be done by just catching the event and sending a message, or perhaps calling a method of the Controller, and let the Controller handle any extra stuff that needs to be done.
 
--
Robin

chris tophe

unread,
Jul 30, 2019, 2:36:02 AM7/30/19
to wxPython-users

thank you Robin
Reply all
Reply to author
Forward
0 new messages