954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Open and close frames

Dear all,
I built 2 script to manage 2 different frames (which I will then convert to exe with py2exe).
Frame 1 (GG1) is like this

import wx

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):
functions here
...

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    GG1 = MyFrame(None, -1, "")
    app.SetTopWindow(GG1)
    GG1.Show()
    app.MainLoop()


Everything is fine. Now I would like to open another independent frame (GG2)

import wx

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):
functions here
...

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    GG2= MyFrame(None, -1, "")
    app.SetTopWindow(GG2)
    GG2.Show()
    app.MainLoop()


GG1 and GG2 are in 2 py files. Is there a ways for them to share values (something I enter in a GG2 box to be copied in one GG1 box)? Or to do something like
GG1 opens GG2
GG1 closes
when I close GG2
GG1 opens

Otherwise, since GG1 has a function that search for a text file when it opens, I can I ask GG1 to search again for that file after GG2 closes?

I know it's a bit confusing... sorry about that and thanks for your help,
Gianluca

giancan
Light Poster
31 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You could import your frame2 file into your frame1 code and instantiate it there.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Another way is to use a third class as a wrapper. The buttons in the following code print the contents of the variable from the other class. Also, it is never a good idea to create two or more instances of a GUI at the same time. It can cause confusion with the interaction from the display manager. Use one of instance of wx and separate frames.

import wx

class Test1():
    def __init__(self, parent, id, title):
        self.ident="Test1 Ident"
        self.frame=wx.Frame(None, id, title, size=(170, 100))
        self.frame.SetPosition((10, 10))

    def exit_button(self, T2):
        panel = wx.Panel(self.frame, -1)
        exit = wx.Button(panel, -1, 'Exit 1'+T2.ident, (10, 10))

        exit.Bind(wx.EVT_BUTTON,  self.exit_this, id=exit.GetId())
        self.frame.Show(True)

    def exit_this(self, event):
        self.frame.Close()

class Test2():
    def __init__(self, parent, id, title, wrap):
        self.ident="Test2"
        self.frame=wx.Frame(None, id, title, size=(170, 100))
        self.frame.SetPosition((10, 150))

        panel = wx.Panel(self.frame, -1)
        exit = wx.Button(panel, -1, 'Exit Two-->'+wrap.T1.ident, (10, 10))
        exit.Bind(wx.EVT_BUTTON,  self.exit_this, id=exit.GetId())

        self.frame.Show(True)

    def exit_this(self, event):
        self.frame.Close()

class Wrapper():
    def __init__(self):
        app = wx.App()
        self.T1=Test1(None, -1, '')
        self.T2=Test2(None, -1, '', self)
        self.T1.exit_button(self.T2)
        app.MainLoop()

W = Wrapper()
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: