从主 window 调用子 window
Calling the sub window from the main window
我有一个 class 将成为我的校长 window。它被称为main_windows
。在主 window 中,当我单击按钮 button_1
时,程序将打开另一个 GUI,使用户能够输入执行加法过程所需的值。 GUI 的模块称为 sub_windows
,它位于另一个文件中。
我已经创建了一个名为 Model
的 class,因此它将是我的 class,用于计算期间所需的所有实例。
我如何调用模块 sub_windows
这是一个 GUI,同时将 class Model
发送到模块。
下面是代码:
模块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()
模块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()
如果你愿意,你可以把你的子 windows 变成 wx.Dialog
。然后在你用来打开子 window 的事件处理程序中,你可以以模态方式打开对话框,这样它就会阻塞主 window。这将允许用户输入他们的数据并按下提交按钮或其他任何按钮。
按下提交按钮后,控件将切换回主 window 中的事件处理程序。您可以在那里提取用户输入的值:
def open_sub_window(self, event):
dlg = subwindow(*args)
dlg.ShowModal()
# get the values
value_one = dlg.text_ctrl_1.GetValue()
或者,您也可以使用 pubsub 在 类 之间发送数据。我个人更喜欢这种方法,因为它更灵活一些。您可以在以下链接中了解它的工作原理:
我有一个 class 将成为我的校长 window。它被称为main_windows
。在主 window 中,当我单击按钮 button_1
时,程序将打开另一个 GUI,使用户能够输入执行加法过程所需的值。 GUI 的模块称为 sub_windows
,它位于另一个文件中。
我已经创建了一个名为 Model
的 class,因此它将是我的 class,用于计算期间所需的所有实例。
我如何调用模块 sub_windows
这是一个 GUI,同时将 class Model
发送到模块。
下面是代码:
模块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()
模块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()
如果你愿意,你可以把你的子 windows 变成 wx.Dialog
。然后在你用来打开子 window 的事件处理程序中,你可以以模态方式打开对话框,这样它就会阻塞主 window。这将允许用户输入他们的数据并按下提交按钮或其他任何按钮。
按下提交按钮后,控件将切换回主 window 中的事件处理程序。您可以在那里提取用户输入的值:
def open_sub_window(self, event):
dlg = subwindow(*args)
dlg.ShowModal()
# get the values
value_one = dlg.text_ctrl_1.GetValue()
或者,您也可以使用 pubsub 在 类 之间发送数据。我个人更喜欢这种方法,因为它更灵活一些。您可以在以下链接中了解它的工作原理: