Sharing a variable between two frames

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sharing a variable between two frames

TSUBOTA Masami
Hi there,

I want to share a variable between two frames.

The followings are my scripts.
An error occurs in test_sub.py.
-> NameError: global name 'frm_sub' is not defined
I put "global frm_sub" at just after the import lines in test_main.py,
test_sub.py, and both of them. But nothing had changed.
Do you have any advice or comments?

And what I want to do is get a value of the variable (sub_Btn_ID)
from the sub script to the main one.
How can I do SetValue at text_ctrl_1 in frame_1?

Thank you in advance.

Best wishes,
Masa


===== test_main.py ====================

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import test_sub as SUB

# Open a new frame in test_sub.py by clicking mainbtn
def click_mainbtn(event):
    frm_sub = SUB.MyFrameS(None, -1, "")
    answer = frm_sub.Show()


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
        self.mainbtn = wx.Button(self, -1, u"Clisk to pen the other frame")

        self.__set_properties()
        self.__do_layout()

        self.mainbtn.Bind(wx.EVT_BUTTON, click_mainbtn)

    def __set_properties(self):
        self.SetTitle(u"test")

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.FlexGridSizer(1, 2, 2, 1)
        grid_sizer_1.Add(self.text_ctrl_1, 0, 0, 0)
        grid_sizer_1.Add(self.mainbtn, 0, 0, 0)
        sizer_1.Add(grid_sizer_1, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

# end of class MyFrame
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()


===== test_sub.py =====================

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx


def get_btn_values(event):
    ojt = event.GetEventObject()
    sub_Btn_ID = str(event.GetId())
    print sub_Btn_ID
    frm_sub.textctrl.SetValue(sub_Btn_ID) # An error occurs here!!
    frm_sub.Close(True)

class MyFrameS(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.button_1 = wx.Button(self, 1, "button_1")
        self.button_2 = wx.Button(self, 2, "button_2")
        self.button_3 = wx.Button(self, 3, "button_3")
        self.button_4 = wx.Button(self, 4, "button_4")
        self.button_5 = wx.Button(self, 5, "button_5")
        self.textctrl = wx.TextCtrl(self, -1, "")

        self.__set_properties()
        self.__do_layout()

        self.button_1.Bind(wx.EVT_BUTTON, get_btn_values)
        self.button_2.Bind(wx.EVT_BUTTON, get_btn_values)
        self.button_3.Bind(wx.EVT_BUTTON, get_btn_values)
        self.button_4.Bind(wx.EVT_BUTTON, get_btn_values)
        self.button_5.Bind(wx.EVT_BUTTON, get_btn_values)

    def __set_properties(self):
        self.SetTitle("frame_2")

    def __do_layout(self):
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_2 = wx.FlexGridSizer(6, 1, 2, 2)
        grid_sizer_2.Add(self.button_1, 0, 0, 0)
        grid_sizer_2.Add(self.button_2, 0, 0, 0)
        grid_sizer_2.Add(self.button_3, 0, 0, 0)
        grid_sizer_2.Add(self.button_4, 0, 0, 0)
        grid_sizer_2.Add(self.button_5, 0, 0, 0)
        grid_sizer_2.Add(self.textctrl, 0, 0, 0)
        sizer_2.Add(grid_sizer_2, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()

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

--
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wxPython-users?hl=en
Reply | Threaded
Open this post in threaded view
|

Re: Sharing a variable between two frames

Tim Roberts
TSUBOTA Masami wrote:

> I want to share a variable between two frames.
>
> The followings are my scripts.
> An error occurs in test_sub.py.
> -> NameError: global name 'frm_sub' is not defined
> I put "global frm_sub" at just after the import lines in test_main.py,
> test_sub.py, and both of them. But nothing had changed.
> Do you have any advice or comments?
>
> And what I want to do is get a value of the variable (sub_Btn_ID)
> from the sub script to the main one.
> How can I do SetValue at text_ctrl_1 in frame_1?

You are focusing on your implementation, not on the larger task.

What you really want here, I think, is to have one function that you can
call from test_main that displays a dialog, and returns a value when
that dialog closes.  In that case, there's no reason that the main
program has to know anything about the sub frame (which should be a
wx.Dialog and not a wx.Frame, by the way).

So, in test_sub.py, just have a single function that creates the dialog
modally, waits for it to close, then reads the value from the dialog and
returns it.  test_main.py can just call that one function.

Global variables are almost always a bad idea.  Also, your event
handlers should be member functions of the class, not standalone
functions.  Here is one way:

==== test_main.py ====

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import test_sub

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
        self.mainbtn = wx.Button(self, -1, u"Click to open the other frame")

        self.__set_properties()
        self.__do_layout()

        self.mainbtn.Bind(wx.EVT_BUTTON, self.click_mainbtn)

    def click_mainbtn(self,event):
        answer = test_sub.getButtonValue(self)
        self.text_ctrl_1.SetValue(str(answer))

    def __set_properties(self):
        self.SetTitle(u"test")

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.FlexGridSizer(1, 2, 2, 1)
        grid_sizer_1.Add(self.text_ctrl_1, 0, 0, 0)
        grid_sizer_1.Add(self.mainbtn, 0, 0, 0)
        sizer_1.Add(grid_sizer_1, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

# end of class MyFrame
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

=== test_sub.py ====

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx

def getButtonValue(parent):
    dlg = MyFrameS(parent,-1,"Get Button")
    val = dlg.ShowModal()
    dlg.Destroy()
    return val

class MyFrameS(wx.Dialog):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.button_1 = wx.Button(self, 1, "button_1")
        self.button_2 = wx.Button(self, 2, "button_2")
        self.button_3 = wx.Button(self, 3, "button_3")
        self.button_4 = wx.Button(self, 4, "button_4")
        self.button_5 = wx.Button(self, 5, "button_5")
        self.textctrl = wx.TextCtrl(self, -1, "")

        self.__set_properties()
        self.__do_layout()

        self.button_1.Bind(wx.EVT_BUTTON, self.get_btn_values)
        self.button_2.Bind(wx.EVT_BUTTON, self.get_btn_values)
        self.button_3.Bind(wx.EVT_BUTTON, self.get_btn_values)
        self.button_4.Bind(wx.EVT_BUTTON, self.get_btn_values)
        self.button_5.Bind(wx.EVT_BUTTON, self.get_btn_values)

    def get_btn_values(self,event):
        ojt = event.GetEventObject()
        sub_Btn_ID = event.GetId()
        print sub_Btn_ID
        self.EndModal(sub_Btn_ID)

    def __set_properties(self):
        self.SetTitle("frame_2")

    def __do_layout(self):
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_2 = wx.FlexGridSizer(6, 1, 2, 2)
        grid_sizer_2.Add(self.button_1, 0, 0, 0)
        grid_sizer_2.Add(self.button_2, 0, 0, 0)
        grid_sizer_2.Add(self.button_3, 0, 0, 0)
        grid_sizer_2.Add(self.button_4, 0, 0, 0)
        grid_sizer_2.Add(self.button_5, 0, 0, 0)
        grid_sizer_2.Add(self.textctrl, 0, 0, 0)
        sizer_2.Add(grid_sizer_2, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frm_sub = MyFrameS(None, -1, "")
    frm_sub.ShowModal()

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wxPython-users?hl=en
Reply | Threaded
Open this post in threaded view
|

Re: Sharing a variable between two frames

TSUBOTA Masami
Dear Tim,

Thank you so much for your quick reply.
I just confirmed that the scripts which you given worked.
Yes, that was what I wanted to implement.

Well, it's mid night here. So, I'll look at the scripts carefully tomorrow.
Good night. Have a nice day!

Thank you again, Tim.

Wishes,
Masa


2012/4/18 Tim Roberts <[hidden email]>:

> TSUBOTA Masami wrote:
>> I want to share a variable between two frames.
>>
>> The followings are my scripts.
>> An error occurs in test_sub.py.
>> -> NameError: global name 'frm_sub' is not defined
>> I put "global frm_sub" at just after the import lines in test_main.py,
>> test_sub.py, and both of them. But nothing had changed.
>> Do you have any advice or comments?
>>
>> And what I want to do is get a value of the variable (sub_Btn_ID)
>> from the sub script to the main one.
>> How can I do SetValue at text_ctrl_1 in frame_1?
>
> You are focusing on your implementation, not on the larger task.
>
> What you really want here, I think, is to have one function that you can
> call from test_main that displays a dialog, and returns a value when
> that dialog closes.  In that case, there's no reason that the main
> program has to know anything about the sub frame (which should be a
> wx.Dialog and not a wx.Frame, by the way).
>
> So, in test_sub.py, just have a single function that creates the dialog
> modally, waits for it to close, then reads the value from the dialog and
> returns it.  test_main.py can just call that one function.
>
> Global variables are almost always a bad idea.  Also, your event
> handlers should be member functions of the class, not standalone
> functions.  Here is one way:
>
> ==== test_main.py ====
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> import wx
> import test_sub
>
> class MyFrame(wx.Frame):
>    def __init__(self, *args, **kwds):
>        kwds["style"] = wx.DEFAULT_FRAME_STYLE
>        wx.Frame.__init__(self, *args, **kwds)
>        self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
>        self.mainbtn = wx.Button(self, -1, u"Click to open the other frame")
>
>        self.__set_properties()
>        self.__do_layout()
>
>        self.mainbtn.Bind(wx.EVT_BUTTON, self.click_mainbtn)
>
>    def click_mainbtn(self,event):
>        answer = test_sub.getButtonValue(self)
>        self.text_ctrl_1.SetValue(str(answer))
>
>    def __set_properties(self):
>        self.SetTitle(u"test")
>
>    def __do_layout(self):
>        sizer_1 = wx.BoxSizer(wx.VERTICAL)
>        grid_sizer_1 = wx.FlexGridSizer(1, 2, 2, 1)
>        grid_sizer_1.Add(self.text_ctrl_1, 0, 0, 0)
>        grid_sizer_1.Add(self.mainbtn, 0, 0, 0)
>        sizer_1.Add(grid_sizer_1, 1, wx.ALL | wx.EXPAND, 5)
>        self.SetSizer(sizer_1)
>        sizer_1.Fit(self)
>        self.Layout()
>
> # end of class MyFrame
> if __name__ == "__main__":
>    app = wx.PySimpleApp(0)
>    wx.InitAllImageHandlers()
>    frame_1 = MyFrame(None, -1, "")
>    app.SetTopWindow(frame_1)
>    frame_1.Show()
>    app.MainLoop()
>
> === test_sub.py ====
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> import wx
>
> def getButtonValue(parent):
>    dlg = MyFrameS(parent,-1,"Get Button")
>    val = dlg.ShowModal()
>    dlg.Destroy()
>    return val
>
> class MyFrameS(wx.Dialog):
>    def __init__(self, *args, **kwds):
>        kwds["style"] = wx.DEFAULT_FRAME_STYLE
>        wx.Dialog.__init__(self, *args, **kwds)
>        self.button_1 = wx.Button(self, 1, "button_1")
>        self.button_2 = wx.Button(self, 2, "button_2")
>        self.button_3 = wx.Button(self, 3, "button_3")
>        self.button_4 = wx.Button(self, 4, "button_4")
>        self.button_5 = wx.Button(self, 5, "button_5")
>        self.textctrl = wx.TextCtrl(self, -1, "")
>
>        self.__set_properties()
>        self.__do_layout()
>
>        self.button_1.Bind(wx.EVT_BUTTON, self.get_btn_values)
>        self.button_2.Bind(wx.EVT_BUTTON, self.get_btn_values)
>        self.button_3.Bind(wx.EVT_BUTTON, self.get_btn_values)
>        self.button_4.Bind(wx.EVT_BUTTON, self.get_btn_values)
>        self.button_5.Bind(wx.EVT_BUTTON, self.get_btn_values)
>
>    def get_btn_values(self,event):
>        ojt = event.GetEventObject()
>        sub_Btn_ID = event.GetId()
>        print sub_Btn_ID
>        self.EndModal(sub_Btn_ID)
>
>    def __set_properties(self):
>        self.SetTitle("frame_2")
>
>    def __do_layout(self):
>        sizer_2 = wx.BoxSizer(wx.VERTICAL)
>        grid_sizer_2 = wx.FlexGridSizer(6, 1, 2, 2)
>        grid_sizer_2.Add(self.button_1, 0, 0, 0)
>        grid_sizer_2.Add(self.button_2, 0, 0, 0)
>        grid_sizer_2.Add(self.button_3, 0, 0, 0)
>        grid_sizer_2.Add(self.button_4, 0, 0, 0)
>        grid_sizer_2.Add(self.button_5, 0, 0, 0)
>        grid_sizer_2.Add(self.textctrl, 0, 0, 0)
>        sizer_2.Add(grid_sizer_2, 1, wx.ALL | wx.EXPAND, 5)
>        self.SetSizer(sizer_2)
>        sizer_2.Fit(self)
>        self.Layout()
>
> if __name__ == "__main__":
>    app = wx.PySimpleApp(0)
>    wx.InitAllImageHandlers()
>    frm_sub = MyFrameS(None, -1, "")
>    frm_sub.ShowModal()
>
> --
> Tim Roberts, [hidden email]
> Providenza & Boekelheide, Inc.
>
> --
> To unsubscribe, send email to [hidden email]
> or visit http://groups.google.com/group/wxPython-users?hl=en

--
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wxPython-users?hl=en