user247705
user247705

Reputation: 21

Passing values between wx.frame wxpython

I have one frame where one TextCtrl and a button. I would like to enter a value in that TextCTrl and to be displayed in another frame TextCTrl and use that value for computation in that new frame as well. Any idea would be appreciated.

Upvotes: 2

Views: 2243

Answers (2)

Graphs Rodriguez
Graphs Rodriguez

Reputation: 30

you could use the pubsub lib in order to so, but you will have to be aware of closing one of the frames so the data could be sent to the other frame. So the way this works is based on the Publish–subscribe pattern where you use a subscriber(EVT_listerner) which receives the message the pusblisher is able to send to it. The event will be listening until the publisher frame is destroyed, you could get the data gathered afterwards.

Here's an example of how to use it:

import wx
from wx.lib.pubsub import pub 

########################################################################
class OtherFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, "Secondary Frame")
        panel = wx.Panel(self)

        msg = "Enter a Message to send to the main frame"
        instructions = wx.StaticText(panel, label=msg)
        self.msgTxt = wx.TextCtrl(panel, value="")
        closeBtn = wx.Button(panel, label="Send and Close")
        closeBtn.Bind(wx.EVT_BUTTON, self.onSendAndClose)

        sizer = wx.BoxSizer(wx.VERTICAL)
        flags = wx.ALL|wx.CENTER
        sizer.Add(instructions, 0, flags, 5)
        sizer.Add(self.msgTxt, 0, flags, 5)
        sizer.Add(closeBtn, 0, flags, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onSendAndClose(self, event):
        """
        Send a message and close frame
        """
        msg = self.msgTxt.GetValue()
        pub.sendMessage("panelListener", message=msg)
        pub.sendMessage("panelListener", message="test2", arg2="2nd argument!")
        self.Close()

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        pub.subscribe(self.myListener, "panelListener")

        btn = wx.Button(self, label="Open Frame")
        btn.Bind(wx.EVT_BUTTON, self.onOpenFrame)

    #----------------------------------------------------------------------
    def myListener(self, message, arg2=None):
        """
        Listener function
        """
        print "Received the following message: " + message
        if arg2:
            print "Received another arguments: " + str(arg2)

    #----------------------------------------------------------------------
    def onOpenFrame(self, event):
        """
        Opens secondary frame
        """
        frame = OtherFrame()
        frame.Show()

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="New PubSub API Tutorial")
        panel = MyPanel(self)
        self.Show()



#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

For more info about the Publish–subscribe pattern check this out: \https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern

For more info about the library: https://wxpython.org/Phoenix/docs/html/wx.lib.pubsub.pub.html#wx.lib.pubsub.pub.getDefaultPublisher

Upvotes: 1

Anurag Uniyal
Anurag Uniyal

Reputation: 88757

Not sure exactly what you want and how you want. if you have two frames in same application , why can't you just copy from one textctrl to other on text change event, or when user presses some button e.g try this example, in this if you type in one frame when will also be displayed in another on wx.EVT_TEXT

import wx

app = wx.PySimpleApp()
frame1 = wx.Frame(None, title="Type Here...", pos=(0,0), size=(300,300))
frame2 = wx.Frame(None, title="...to get value here", pos=(310,0), size=(300,300))

tc1 = wx.TextCtrl(frame1)
tc2 = wx.TextCtrl(frame2)

def textChange(event):
    tc2.SetValue(tc1.GetValue())

tc1.Bind(wx.EVT_TEXT, textChange)

app.SetTopWindow(frame1)
frame1.Show()
frame2.Show()

app.MainLoop()

Upvotes: 1

Related Questions