【问题标题】:Class inheritance for GUI using wxpython使用 wxpython 的 GUI 类继承
【发布时间】:2010-10-18 23:32:46
【问题描述】:

我有一个非常简单的 GUI,它接受两个参数,然后调用其他三个名为 DigitalFilter()、BeatByBeatVariables() 和 GetSummaryOfWholeTest() 的类。这是我第一次编写类,我需要语法方面的帮助。

具体来说,你能帮我继承吗?我希望在应用程序启动时调用 GUI 的类 MainWindow(wx.Frame)。然后,当用户在 GUI 中单击 self.button 时,我希望应用程序首先运行 myFilter=DigitalFilter(TestID,FilterTimePeriod),然后第二次运行 myBeatByBeat = BeatByBeatVariables(arg1,arg2),最后第三次运行 myTestSummary = GetSummaryOfWholeTest( argA,argB)

请注意,myBeatByBeat 在 myFilter 创建完成之前无法开始运行,因为 myFilter 的实例化会创建作为 myBeatByBeat 输入所需的 csv 文件。同样,在 myBeatByBeat 创建完成之前,myTest Summary 无法开始运行,因为 myBeatByBeat 的实例化会创建 csv 文件,这些文件需要作为 myTestSummary 的输入。

谁能告诉我用适当的继承来编写/实例化这些类的正确语法,以便每个类的工作将按照尊重其输入/输出关系的顺序完成?

我假设这里应该使用继承,但我不知道应该从什么继承。我也不知道我是否没有看到为代码提供必要的相互关系所必需的其他概念。

下面是相关代码的概要:

class DigitalFilter():
    def __init__(self,TestID,FilterTimePeriod):
    # All the code for the digital filter goes here.
    # I am omitting this class' code for simplicity.

class BeatByBeatVariables():
    def __init__(self,arg1,arg2):
    # I am omitting this class' code for simplicity

class GetSummaryOfWholeTest():
    def __init__(self,argA,ArgB):
    # This class' code is omitted for simplicity

class MainWindow(wx.Frame):
    def __init__(self, parent,id,title):
        wx.Frame.__init__(self,parent,wx.ID_ANY,title, size = (500,500), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)

        # Create self.editname and self.edithear in code that I am omitting for simplicity
        self.button =wx.Button(self, label="Click here to filter the data", pos=(200, 125))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)

    def OnClick(self,event):
        FilterTimePeriod = self.editname.GetValue()
        TestID = self.edithear.GetValue()
        myFilter=DigitalFilter(TestID,FilterTimePeriod)
        myBeatByBeat = BeatByBeatVariables(arg1,arg2)
        myTestSummary = GetSummaryOfWholeTest(argA,argB)

app = wx.PySimpleApp()
frame = MainWindow(None,-1,"Filtering Software.  Version 1.0")
app.MainLoop()

注意:我使用的是 Python 2.6,因为我也在使用 numpy 和 sciepy

【问题讨论】:

    标签: user-interface oop class inheritance wxpython


    【解决方案1】:

    听起来您对继承的含义有些困惑。从基类继承的类为该基类添加了额外的功能,它并不暗示任何关于实例化顺序或数据传递的内容。如果您只想强制执行对象创建的顺序,您应该使用wx.lib.pubsub 知道每个步骤何时结束。

    您可以在过滤完成后从 DigitalFilter 发送一条消息,然后在已经实例化的 BeatByBeatVariables 实例中接收该消息,或者在您的主​​类中接收它并在那时创建 BeatByBeatVariables 实例。两者都有效。如果我知道我需要所有这些实例,我可能会提前创建它们,如下所示:

    from wx.lib.pubsub import setupkwargs # needed only in version 2.8.11
    from wx.lib.pubsub import pub
    
    class DigitalFilter():
        def __init__(self,TestID,FilterTimePeriod):
            pass
        def do_something(self, data):
            # do something
            pub.sendMessage('filter.done', data=some_data)
    
    class BeatByBeatVariables():
        def __init__(self,arg1,arg2):
            pub.subscribe(self.do_something, 'filter.done')
        def do_something(self, data):
            # do something
            pub.sendMessage('beatbybeat.done', data=some_data)
    
    class GetSummaryOfWholeTest():
        def __init__(self,argA,ArgB):
            pub.subscribe(self.do_something, 'beatbybeat.done')
        def do_something(self, data):
            # do something
            pub.sendMessage('summary.done', data=some_data)
    

    然后在你的主类中,你可以有:

        def OnClick(self,event):
            FilterTimePeriod = self.editname.GetValue()
            TestID = self.edithear.GetValue()
            myFilter=DigitalFilter(TestID,FilterTimePeriod)
            myBeatByBeat = BeatByBeatVariables(arg1,arg2)
            myTestSummary = GetSummaryOfWholeTest(argA,argB)
    
            myFilter.do_something()
            # all the other do_somethings are triggered by message passing
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2015-08-25
      • 2014-10-08
      相关资源
      最近更新 更多