【发布时间】:2020-03-23 17:46:26
【问题描述】:
我有以下类层次结构:
wx.Frame 派生类;在该框架内,我有一个拆分器窗口,以及一个链接到该拆分器窗口的基于 wx.Panel 的类。在面板中,我有一个按钮。我正在将事件处理程序绑定到按钮以执行一些操作。问题是大多数动作都应该在框架类中完成。所以,不知何故,我必须从按钮偶数处理程序中的框架类中调用一个方法。我怎样才能做到这一点?相关部分代码如下。
干杯
class EPSPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
lbl_SGCR = wx.StaticText(self, label="SGCR", pos=(20, 20))
self.ent_SGCR = wx.TextCtrl(self, value="", pos=(100,20), size=(200,-1))
lbl_SWCR = wx.StaticText(self, label="SWCR", pos=(20, 60))
self.ent_SWCR = wx.TextCtrl(self, value="", pos=(100,60), size=(200,-1))
lbl_SWU = wx.StaticText(self, label="SWU", pos=(20, 100))
self.ent_SWU = wx.TextCtrl(self, value="", pos=(100,100), size=(200,-1))
lbl_SGU = wx.StaticText(self, label="SGU", pos=(20, 140))
self.ent_SGU = wx.TextCtrl(self, value="", pos=(100,140), size=(200,-1))
lbl_SWL = wx.StaticText(self, label="SWL", pos=(20, 180))
self.ent_SWL = wx.TextCtrl(self, value="", pos=(100,180), size=(200,-1))
lbl_SGL = wx.StaticText(self, label="SGL", pos=(20, 220))
self.ent_SGL = wx.TextCtrl(self, value="", pos=(100,220), size=(200,-1))
calc_button = wx.Button(self, label="Calculate", pos=(110,260))
calc_button.Bind(wx.EVT_BUTTON, self.on_btn)
def on_btn(self, event):
self.set_SGCR = float(self.ent_SGCR.GetValue())
self.set_SWCR = float(self.ent_SWCR.GetValue())
self.set_SGU = float(self.ent_SGU.GetValue())
self.set_SWU = float(self.ent_SWU.GetValue())
# these four values in this method I need to pass to the KrFrame class
# instance to process in one of its methods. I also need somehow
# let the frame class know that that button was pressed. How can I do it?
class KrFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None,
title='Gas Relative Permeability Editor', size=(900, 800))
self.sp = wx.SplitterWindow(self)
self.rightSplitter = wx.SplitterWindow(self.sp) #Another splitter to split right panel into two vertical ones
self.leftSplitter = wx.SplitterWindow(self.sp)
self.panel01 = KrPanel(self.leftSplitter)
self.panel02 = PlotPanel(self.rightSplitter)
self.panel03 = EPSPanel(self.rightSplitter) #Third panel for scaled end point entry
self.panel04 = KrPanel(self.leftSplitter)
self.rightSplitter.SplitHorizontally(self.panel02, self.panel03, 400) #Splitting right panel into two horizontally
self.leftSplitter.SplitHorizontally(self.panel01, self.panel04, 400)
self.sp.SplitVertically(self.leftSplitter, self.rightSplitter, 450)
self.create_menu()
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = KrFrame()
app.MainLoop()
del app
【问题讨论】: