python – How to manage the same frame like 2 different objects importing it from the same file

My goal is to have stored in file one “Frame_Model.py”, importing it from “Main_Manager.py” and to use it multiple times as many times I need and use they like different objects storing for eachone their own variables some shared and other not shared, without loosing it after self.Destroy().

The reason is because my “Main_Manager.py” is very long and complicated one and I need simplify it saving external modules in external files and also have a lot of frames to import and these frames are used also from others modules

I found only one way to solve:

made a copy of file “Frame_Model.py” as many times as many objects I need chancing only filename “Frame_Model_2.py”; “Frame_Model_3.py” and so on… (This is a little bit crazy but it’s working).

Follow a full working project:

ManageFrame.py # is main manager module file

FrameArrivi_esempio.py # is frame model module file

FrameResa_esempio.py # is frame model module file (identical copy of FrameArrivi_esempio.py)

ManageFrame.py

import FrameArrivi_esempio as fr_1
import FrameResa_esempio as fr_2
f_1 = fr_1.Start_frame()
f_2 = fr_2.Start_frame()

def OnFrame_white():
    v_1 = fr_1.MyFr
    v_1.next_frame="Frame_cyan"
    v_1.frame_color="WHITE"
    v_1.frameName="Range_1"
    f_1.show_frame()
    upper_limit = v_1.upper_limit  # not shared var
    range_val = v_1.range_val  # not shared var
    print('upper_limit', upper_limit)
    print('range_val', range_val)
    exit_val = v_1.exit_val  # shared var
    next_frame = v_1.next_frame  # shared var
    return exit_val, next_frame


def OnFrame_cyan():
    v_2 = fr_2.MyFr
    v_2.next_frame="Frame_white"
    v_2.frame_color="CYAN"
    v_2.frameName="Range_2"
    f_2.show_frame()
    upper_limit = v_2.upper_limit  # not shared var
    range_val = v_2.range_val  # not shared var
    print('upper_limit', upper_limit)
    print('range_val', range_val)
    exit_val = v_2.exit_val  # shared var
    next_frame = v_2.next_frame  # shared var
    return exit_val, next_frame


def main():

    exit_val = False
    next_frame="Frame_white"
    while exit_val is False:
        exit_val = True
        if next_frame == 'Frame_white':
            exit_val, next_frame = OnFrame_white()
            continue
        if next_frame == 'Frame_cyan':
            exit_val, next_frame = OnFrame_cyan()
            continue
    exit()


if __name__ == "__main__":
    print('from keyboard')
    loop_main = main()

else:
    print('as sub')
    loop_main = main()

FrameArrivi_esempio.py

import wx.adv


class MyFr(wx.Frame):
    x_p, y_p = -1, -1  # not shared var
    w_a, h_a = -1, -1  # not shared var
    upper_limit, range_val = 1, 1  # not shared var
    frameName = __name__
    frame_color="WHITE"

    def __init__(self, *args, **kw):
        super(MyFr, self).__init__(*args, **kw)
        self.InitUI()
        MyFr.exit_val = False

    def InitUI(self):

        self.CreateWidgets()
        self.panel.SetBackgroundColour(MyFr.frame_color)

        self.SetTitle(MyFr.frameName)
        if MyFr.x_p == -1:
            self.Centre()
        else:
            self.SetPosition((MyFr.x_p, MyFr.y_p))
            self.SetSize((MyFr.w_a, MyFr.h_a))
        self.Show(True)

    def CreateWidgets(self):
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.panel = wx.ScrolledWindow(self)

        txt = f'Open frame {MyFr.next_frame}'
        self.open_next_frame = wx.Button(
            self.panel, size=(-1, -1), label=txt)
        self.open_next_frame.Bind(wx.EVT_BUTTON, self.OnOpenNextFrame)

        self.exit_valBtn = wx.Button(
            self.panel, size=(-1, -1), label="EXIT")
        self.exit_valBtn.Bind(wx.EVT_BUTTON, self.OnExit)

        upperLimit_lbl = wx.StaticText(
            self.panel, label="UPPER LIMIT:", size=(160, -1))
        # upperLimit_lbl.SetForegroundColour(MyFr.frameTxtCol)
        self.upperLimit = wx.TextCtrl(
            self.panel, size=(100, -1), style=wx.TE_PROCESS_ENTER)
        self.upperLimit.SetValue(str(MyFr.upper_limit))
        self.upperLimit.Bind(wx.EVT_TEXT_ENTER, self.OnUpperLimit)

        range_lbl = wx.StaticText(
            self.panel, label="RANGE:", size=(160, -1))
        self.range_val = wx.TextCtrl(
            self.panel, size=(100, -1), style=wx.TE_PROCESS_ENTER)
        self.range_val.SetValue(str(MyFr.range_val))
        self.range_val.Bind(wx.EVT_TEXT_ENTER, self.OnRange_val)

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(upperLimit_lbl, 0, wx.ALL, 1)
        sizer.Add(self.upperLimit, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(range_lbl, 0, wx.ALL, 1)
        sizer.Add(self.range_val, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.open_next_frame, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.exit_valBtn, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        self.panel.SetSizer(mainSizer)
        mainSizer.Fit(self)
        self.panel.Layout()
        w, h = self.panel.GetSize()
        self.panel.SetScrollbars(1, 1, w, h)
        self.panel.SetScrollRate(1, 1)  # Pixels per scroll increment

    def OnUpperLimit(self, event):
        """Set upper limit"""
        field = event.GetEventObject()
        field_val = field.GetValue()
        MyFr.upper_limit = int(field_val)  # not shared var

    def OnRange_val(self, event):
        """Set range"""
        field = event.GetEventObject()
        field_val = field.GetValue()
        MyFr.range_val = int(field_val)  # not shared var

    def Save_Position(self):
        MyFr.x_p, MyFr.y_p = self.GetPosition()  # not shared var
        MyFr.w_a, MyFr.h_a = self.GetSize()  # not shared var

    def OnExit(self, event):
        MyFr.exit_val = True
        self.Save_Position()
        self.Destroy()

    def OnOpenNextFrame(self, event):
        # do something
        self.Save_Position()
        self.Destroy()

    def OnClose(self, event):
        MyFr.exit_val = True
        self.Save_Position()
        self.Destroy()


class Start_frame():

    def __init__(self, *args):
        pass

    def show_frame(self, *args):
        ex = wx.App()
        MyFr(None)
        ex.MainLoop()


if __name__ == "__main__":
    print('from keyboard')
else:
    print('as sub')

FrameResa_esempio.py

import wx.adv


class MyFr(wx.Frame):
    x_p, y_p = -1, -1  # not shared var
    w_a, h_a = -1, -1  # not shared var
    upper_limit, range_val = 1, 1  # not shared var
    frameName = __name__
    frame_color="WHITE"

    def __init__(self, *args, **kw):
        super(MyFr, self).__init__(*args, **kw)
        self.InitUI()
        MyFr.exit_val = False

    def InitUI(self):

        self.CreateWidgets()
        self.panel.SetBackgroundColour(MyFr.frame_color)

        self.SetTitle(MyFr.frameName)
        if MyFr.x_p == -1:
            self.Centre()
        else:
            self.SetPosition((MyFr.x_p, MyFr.y_p))
            self.SetSize((MyFr.w_a, MyFr.h_a))
        self.Show(True)

    def CreateWidgets(self):
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.panel = wx.ScrolledWindow(self)

        txt = f'Open frame {MyFr.next_frame}'
        self.open_next_frame = wx.Button(
            self.panel, size=(-1, -1), label=txt)
        self.open_next_frame.Bind(wx.EVT_BUTTON, self.OnOpenNextFrame)

        self.exit_valBtn = wx.Button(
            self.panel, size=(-1, -1), label="EXIT")
        self.exit_valBtn.Bind(wx.EVT_BUTTON, self.OnExit)

        upperLimit_lbl = wx.StaticText(
            self.panel, label="UPPER LIMIT:", size=(160, -1))
        # upperLimit_lbl.SetForegroundColour(MyFr.frameTxtCol)
        self.upperLimit = wx.TextCtrl(
            self.panel, size=(100, -1), style=wx.TE_PROCESS_ENTER)
        self.upperLimit.SetValue(str(MyFr.upper_limit))
        self.upperLimit.Bind(wx.EVT_TEXT_ENTER, self.OnUpperLimit)

        range_lbl = wx.StaticText(
            self.panel, label="RANGE:", size=(160, -1))
        self.range_val = wx.TextCtrl(
            self.panel, size=(100, -1), style=wx.TE_PROCESS_ENTER)
        self.range_val.SetValue(str(MyFr.range_val))
        self.range_val.Bind(wx.EVT_TEXT_ENTER, self.OnRange_val)

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(upperLimit_lbl, 0, wx.ALL, 1)
        sizer.Add(self.upperLimit, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(range_lbl, 0, wx.ALL, 1)
        sizer.Add(self.range_val, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.open_next_frame, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.exit_valBtn, 0, wx.ALL, 1)
        mainSizer.Add(sizer, 0, wx.ALL, 1)

        self.panel.SetSizer(mainSizer)
        mainSizer.Fit(self)
        self.panel.Layout()
        w, h = self.panel.GetSize()
        self.panel.SetScrollbars(1, 1, w, h)
        self.panel.SetScrollRate(1, 1)  # Pixels per scroll increment

    def OnUpperLimit(self, event):
        """Set upper limit"""
        field = event.GetEventObject()
        field_val = field.GetValue()
        MyFr.upper_limit = int(field_val)  # not shared var

    def OnRange_val(self, event):
        """Set range"""
        field = event.GetEventObject()
        field_val = field.GetValue()
        MyFr.range_val = int(field_val)  # not shared var

    def Save_Position(self):
        MyFr.x_p, MyFr.y_p = self.GetPosition()  # not shared var
        MyFr.w_a, MyFr.h_a = self.GetSize()  # not shared var

    def OnExit(self, event):
        MyFr.exit_val = True
        self.Save_Position()
        self.Destroy()

    def OnOpenNextFrame(self, event):
        # do something
        self.Save_Position()
        self.Destroy()

    def OnClose(self, event):
        MyFr.exit_val = True
        self.Save_Position()
        self.Destroy()


class Start_frame():

    def __init__(self, *args):
        pass

    def show_frame(self, *args):
        ex = wx.App()
        MyFr(None)
        ex.MainLoop()


if __name__ == "__main__":
    print('from keyboard')
else:
    print('as sub')

Leave a Comment