Am building a gui app using wxpython,i have created two separate windows in two different files,i.e app1.py and app2.py,I want to open the 2nd window(app2.py) using a button click on first window(app1.py). How do i achieve this. It would be great if someone can help,Thanks!!
1 Answers
I am assuming you're using wxPython's definition of "window" (i.e. anything that is actually viewable in the GUI), not the regular definition of "separate box with stuff in it and an X button in the corner" (which wxPython calls a "frame")
When I was writing my first wxPython GUI I was faced with a similar probem. I wanted actions in one panel to affect the data shown in another. My first solution was to write methods that blindly passed the request to the panel's parent until it reached the top level (my "main frame", if you will). This worked, but obviously it's a terrible solution.
My second solution was to use wxPython Events, just like catching button presses for a wx.Panel. My idea was I would create a button in one frame and bind wx.EVT_BUTTON with that button's ID in another. I made custom Events and CommandEvents. While trying to implement this solution I discovered that do not propogate and that CommandEvents will only propogate through the parents. So, if left uncaught a CommandEvent will eventually hit your "main frame" but it will never hit the second panel unless that panel happens to be a parent of where the Event came from. Obviously, this won't work for you since the parent can't be hidden but a child be visible. Plus it would leave your main frame cluttered with code and methods to delegate commands.
Finally, I found the answer! wx.lib.pubsub! Basically, you bind the button press in app1.py, just like normal. Now, in the handler you use pubsub to publish a custom message. Next, in the init for app2.py, you use pubsub to subscribe to your custom message (I like having a "message.py" where I declare all my messages as constants). When you subscribe to a message you assign a handler method, just like for events.\
Read this: http://wiki.wxpython.org/WxLibPubSub
Since you said you wanted the window to APPEAR, maybe in the init method for app2.py you call the Hide() method then when you receive the message, call Show().
When I implemented this in my application I was using wxPython2.8. I am now using 2.9 so I don't know if this is still necessary but I found that I need the top import in order to call pubsub.subscribe() and pubsub.sendMessage(). I kept getting errors otherwise. I can't remember how I figured this out, I think I saw it in a code sample and added it in a desperate attempt to make my code work. I suggest you read the documentation I linked above and try to implement your code WITHOUT that import first.
import wx
from wx.lib.pubsub import setupkwargs #I need this to force pubsub to work. I don't know why.
from wx.lib.pubsub import pub
ID_MYBUTTON = wx.NewId()
class App1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init___(self, parent)
button = wx.Button(self, ID_MYBUTTON, "Show App2")
self.Bind(wx.EVT_BUTTON, self.handleButton, id=ID_MYBUTTON
def handleButton(self, event):
pubsub.sendMessage("mybutton.pressed") #send the message
class App2(wx.Window):
def __init__(self, parent):
wx.Window.__init__(self, parent)
self.Hide() #I don't want to be seen yet
pubsub.subscribe(self.gotMessage, "mybutton.pressed") #listen for the message
def gotMessage(self):
self.Show() #Now I want to be seen!
-----EDIT-----
I found this SO question that might help: Creating child frames of main frame in wxPython
Please tell us in detail what you're trying to do? wxPython also has "dialogs" which would be better than a frame if all you're doing is showing the user a message or asking for a bit more information. http://wxpython.org/docs/api/wx.Dialog-class.html
It really depends on what you're trying to accomplish. We can't help you unless you explain what problem you're trying to solve.
-----EDIT AGIAN-----
It's looking like the person asking the question did indeed want a wx.Dialog. For a tutorial, see http://zetcode.com/wxpython/dialogs/ Also see Python WX - Returning user input from wx Dialog
-
i tried with this method,it dint work okay about my application i have created a frame in app1.py ,it has button.when i click the button it should open a 2nd frame ,for which i have written the code in a separate file app2.py my problem is how do call the 2nd frame to show up when i click the button on the 1st frame? i cant link the two separate gui files please Help!!!! – Black May 20 '12 at 06:55
-
am creating an application in which, user enters a value ,if its true,the 2nd window shows up,where he again has to enter few values in text fields,so i have created the two frames as i told u before,app1.py and app2.py both in different programs and files, but i cant open the second frame through the 1st frame. i tried searching for the solution,but everywhere i find the child frame and parent frame's code written in the same single program, i want to put child and parent frame's code in separate files,and opening the child frame through parent frame!!! – Black May 20 '12 at 11:00
-
Will the second window close AFTER they enter the values? If so, the answer is wx.Dialog: http://zetcode.com/wxpython/dialogs/ – acattle May 20 '12 at 13:44
-
but can i store the values in database entered in dialog? – Black May 20 '12 at 15:38
-
also i tried putting my code in this column , but it was posted as a paragraph!!!how do you post codes?? so that i can show you what i have written!! – Black May 20 '12 at 15:42
-
You can't post code in comments. You should edit your main post above with all he latest information so that everyone else can help without having to read your comments here. Also, look at the "Returning user input from a wx.Dialog" link I posted above. Once you assign the data to a variable you can do whatever you want with it, including submitting it to a database. Do some googling for "
python" to find out how to do it. – acattle May 21 '12 at 00:37 -
okay..ill do it!!!thanks for taking your time and helping me out! am sure gonna ask more questions in future :) – Black May 21 '12 at 03:05