I have developed many desktop applications in Delphi / Pascal - Here I have used modal forms.
//Mainform
Form1:= TForm1.Create(Self);
If Form1.Showmodal =mrOK then ….
In Form1 you cal call vars in Mainform like mainform.X := 1
(I know – I normaly use try,except, finally)
I will now switch to Python and my problem is the following:
I want an application with a main window (frame) where you can call a number of forms which can have Forms and so on
I can make a program with python and wxpython where a main frame create a new frame and show it – But how do I get back – and how to I have reference to the parent frame – if its possible!
From mainform
Def OnButton1(self, event):
self.main2 = Frame2.create(None)
self.main2.Show()
self.Hide()
When I am done in main2 who do I return to mainform – It's hidden !!
I know I can use dialog, but I need a normal frame!! What I am looking for is a small program where have a mainframe – with a button that calls a Frame (Frame1) who have a button that calls a frame (Frame1A)
I am new to Python but have made many applications in C,C++, Pascal I have looked at almost all demos, but none of them could give me a hint!
Regards Mick
Just curious, why can't you use a dialog?
Anyway, a simple solution would be to provide a callback function to the constructor of Frame2, which is called when Frame2 is about to be closed.
class Frame2(wx.wxFrame):
def __init__(self, parent, callback, ...)
wx.wxFrame(self, parent)
self._callback = callback
self.bind(wx.EVT_CLOSE, self.OnClose(), self)
def OnClose():
self.Destroy()
self._callback()
class Frame1(...):
...
def OnButton1(self, event):
self.main2 = Frame2(self, self.OnButton1Callback)
self.main2.Show()
self.Hide()
def OnButton1Callback(self)
self.Show()
...
The code above is just a hint, has been never tested!
There are several ways to get a reference to the main frame. When you create the secondary frames, you can pass them either None or a parent. If you pass the main frame as the parent, then you have an easy way to reference it.
Alternatively, you can do this:
topFrame = wx.GetTopLevelParent()
As for showing a secondary frame and hiding the main one, what I do is use Pubsub. When I open the secondary frame, I hide the main one. When I close the secondary frame, it sends a pubsub message to the hidden main frame that is caught and the main frame is re-shown.
There are lots of pubsub examples on the wxPython wiki. There's also this article I wrote:
http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
Hope that helps!
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.