maximus
maximus

Reputation: 131

Calling the sub window from the main window

I have a class that will be my principal window. It is called main_windows. In the main window, when I click on the button button_1, the programme will open another GUI which enable the user to enter the values needed to do the addition process. The module of the GUI is called sub_windows and it is located in another file.

I've created a class called Model, so that it will be my class for all the instances need during the calculation.

How can I called the module sub_windows which is a GUI and at the same time send the class Model to the module.

below is the code :

module main_windows :

#!/usr/bin/env python
# -*- coding: CP1252 -*-
#
# generated by wxGlade 0.6.8 (standalone edition) on Thu Apr 21 09:14:23    2016
#

import wx

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade
class Model():
    def __init__ (self)
        self.a = None
        self.b = None

class MyParentFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyParentFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, wx.ID_ANY, _("Main_Windows"))
        self.button_1 = wx.Button(self, wx.ID_ANY, _("Enter Value"))
        self.button_2 = wx.Button(self, wx.ID_ANY, _("Add\n"))
        self.label_4 = wx.StaticText(self, wx.ID_ANY, _("Results"),    style=wx.ALIGN_CENTRE)
        self.button_3 = wx.Button(self, wx.ID_ANY, _("button_3"))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.to_sub_windows, self.button_1)
        self.Bind(wx.EVT_BUTTON, self.addition, self.button_2)



    # end wxGlade

def __set_properties(self):
    # begin wxGlade: MyParentFrame.__set_properties
    self.SetTitle(_("frame_1"))
    self.label_1.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
    self.label_4.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
    # end wxGlade

def __do_layout(self):
    # begin wxGlade: MyParentFrame.__do_layout
    sizer_1 = wx.BoxSizer(wx.VERTICAL)
    grid_sizer_1 = wx.GridSizer(5, 5, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.label_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.button_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.label_4, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.button_3, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
    self.SetSizer(sizer_1)
    sizer_1.Fit(self)
    self.Layout()

    def to_sub_windows(self)

        #What shoud I put here to call the sub_windows?
        __________

    # end wxGlade

    def addition(self, Model)

        self.c = Model.a.GetValue() + Model.b.GetValue()



    # end wxGlade

    # end of class MyParentFrame
if __name__ == "__main__":
    gettext.install("app") # replace with the appropriate catalog name

    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyParentFrame(None, wx.ID_ANY, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

module sub_windows :

import wx

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class SubFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: SubFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, wx.ID_ANY, _("Sub Window"), style=wx.ALIGN_CENTRE)
         self.label_2 = wx.StaticText(self, wx.ID_ANY, _("A"), style=wx.ALIGN_CENTRE)
         self.text_ctrl_1 = wx.TextCtrl(self, wx.ID_ANY, "")
         self.label_3 = wx.StaticText(self, wx.ID_ANY, _("B"), style=wx.ALIGN_CENTRE)
         self.text_ctrl_2 = wx.TextCtrl(self, wx.ID_ANY, "")
         self.button_1 = wx.Button(self, wx.ID_ANY, _("OK"))

         self.__set_properties()
         self.__do_layout()

         self.Bind(wx.EVT_BUTTON, self.submit, self.button_1)
         # end wxGlade

def __set_properties(self):
    # begin wxGlade: SubFrame.__set_properties
    self.SetTitle(_("frame_1"))
    self.label_1.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
    self.label_2.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
    self.label_3.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
    # end wxGlade

def __do_layout(self):
    # begin wxGlade: SubFrame.__do_layout
    sizer_1 = wx.BoxSizer(wx.VERTICAL)
    grid_sizer_1 = wx.GridSizer(5, 5, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.label_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.label_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.text_ctrl_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.label_3, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.text_ctrl_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add(self.button_1, 0, 0, 0)
    # grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    grid_sizer_1.Add((20, 20), 0, 0, 0)
    sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
    self.SetSizer(sizer_1)
    sizer_1.Fit(self)
    self.Layout()
    # end wxGlade

    def submit(self, event):  # wxGlade: SubFrame.<event_handler>

          What should I put here to send the value a, b from this module to   the module main_windows
          print "Event handler 'submit' not implemented!"
          event.Skip()

  # end of class SubFrame
if __name__ == "__main__":
     gettext.install("app") # replace with the appropriate catalog name

    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = SubFrame(None, wx.ID_ANY, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

Upvotes: 0

Views: 133

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33101

If you want, you can make your sub windows a wx.Dialog. Then in your event handler that you use to open the sub-window, you can open the dialog modally such that it blocks the main window. This will allow the user to enter their data and press the Submit button or what ever.

When the submit button is pressed, control will switch back to the event handler in your main window. There you can pull the values the user entered:

def open_sub_window(self, event):
    dlg = subwindow(*args)
    dlg.ShowModal()
    # get the values
    value_one = dlg.text_ctrl_1.GetValue()

Alternatively, you can also use pubsub to send data between classes. I personally prefer this method as it's a bit more flexible. you can read about how it works in the following links:

Upvotes: 1

Related Questions