我有一个将成为我主要窗口的课程。它被称为main_windows
。在主窗口中,当我单击按钮button_1
时,程序将打开另一个GUI,使用户能够输入执行添加过程所需的值。 GUI的模块名为sub_windows
,它位于另一个文件中。
我已经创建了一个名为Model
的类,因此在计算过程中它将成为我所需要的所有实例的类。
如何调用模块sub_windows
这是一个GUI,同时将类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()
答案 0 :(得分:1)
如果需要,可以将子窗口设为[1, 2].min_by { |v| [v.abs, -v] } # => 1
[1, -1].min_by { |v| [v.abs, -v] } # => 1
。然后在用于打开子窗口的事件处理程序中,可以以模态方式打开对话框,以阻止主窗口。这将允许用户输入他们的数据并按下提交按钮或任何时候。
按下提交按钮后,控件将切换回主窗口中的事件处理程序。在那里,您可以提取用户输入的值:
wx.Dialog
或者,您也可以使用pubsub在类之间发送数据。我个人更喜欢这种方法,因为它更灵活一些。您可以通过以下链接了解它的工作原理: