【问题标题】:Creating an instance of a class in wx Python like normal OOP classes像普通的 OOP 类一样在 wx Python 中创建类的实例
【发布时间】:2018-06-05 23:06:38
【问题描述】:

我正在尝试同时学习 OOP 和 wx Python,我发现在使用 wx 时创建类的实例很困难。我不知道我错过了什么,但请参阅下面的代码,您可以看到我对循环前最后一行的困惑。在正常的继承场景中,我可以创建 DailyTasks 类的实例,然后使用该实例调用方法。为什么不在wx中?是不是因为我没有使用父类中的对象类,而是必须使用 wx.Frame 和 wx.Panel?我希望这是有道理的,我在问什么。代码的第一部分是我在没有 wx 的情况下所期望的行为,第二组代码是让我感到困惑的 wx 代码。提前感谢您的帮助。

----------

class Animals(object):
    def __init__(self, name):
        self.name = name

    def eat(self, food):
        print '%s eats the %s'% (self.name, food)

    def breath(self):
        print '%s breaths through lungs'%(self.name)

class Dog(Animals):
    def gets_mad(self):
        print 'When %s gets mad, %s bites'%(self.name, self.name)

    def show_affection(self):
        print '%s shows affection and wags his tail'% (self.name)

    def fetch(self,thing):
        print '%s chases and brings back the %s'% (self.name, thing)

class Cat(Animals):
    def gets_mad(self):
        print 'When %s gets mad, %s will scratch you'%(self.name, self.name)

    def show_affection(self):
        print '%s shows affection and purrs'% (self.name)

    def fetch(self):
        print "%s doesn't fetch anything" % (self.name)

r = Dog('Rover')  #instantiating to Dog class 
c = Cat('Fluffy')
c.show_affection()
r.show_affection()
c.fetch()
r.fetch('ball')
r.eat('dogfood')
c.eat('catfood')
c.gets_mad()
r.gets_mad()
rex = Dog('Ralph')
rex.eat('steak')


----------


import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='My File Organization')
        book = wx.Notebook(self)
        page = Settings(parent=book)
        page2 = DailyTasks(parent=book)
        book.AddPage(page, 'Settings')
        book.AddPage(page2,'Daily Tasks')
        self.SetSize(wx.Size(1024,768))
        self.Centre()

class Settings(wx.Panel):
    def __init__(self, parent):
        self.parent = parent
        wx.Panel.__init__(self, parent)
        self.panel = wx.Panel(self, pos = (0,0),
        size=(1024,768))
        self.file_text = wx.StaticText(self.panel,
        -1, "Daily Tasks Button 1 Path", (40,20))
        self.file_text_box = wx.TextCtrl(self.panel,
        -1,pos = (170,15), size = (240,20), style = 0, value = 'C:\Documents')
        self.file_button_text = wx.StaticText(self.panel,
        -1, "Button Name", (420,20))
        self.file_button_text_box = wx.TextCtrl(self.panel,
        -1,pos = (500,15), size = (140,20), style = 0, value = 'Zombies')
        self.file_text2 = wx.StaticText(self.panel, -1,
        "Daily Tasks Button 2 Path", (40,50))
        self.file_text_box2 = wx.TextCtrl(self.panel,
        -1,pos = (170,45), size = (240,20), style = 0, value = 'C:\\')
        self.file_button_text2 = wx.StaticText(self.panel,
        -1, "Button Name", (420,50))
        self.file_button_text_box2 = wx.TextCtrl(self.panel,
        -1,pos = (500,45), size = (140,20), style = 0, value = 'Angels')

class DailyTasks(Settings):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.parent = None
        self.panel = wx.Panel(self, pos = (0,0), size=(1024,768))

    def test(self):
        print 'testing'

dt = DailyTasks()  # This is where I have the question and need to create
                   # an instance of the DailyTasks class.

if __name__ == "__main__":
    app = wx.App()
    MyFrame().Show()
    app.MainLoop()       

【问题讨论】:

    标签: python-2.7 inheritance wxpython instantiation


    【解决方案1】:

    wx 应该允许以正常方式继承。您能否详细说明将 dt 分配给 DailyTask() 实例时的实际行为?

    【讨论】:

    • 这就是问题所在。当我尝试用 dt 实例化时,它说它需要 2 个参数,这可能是因为 DailyTasks init 中的父输入。无论我试图放在括号中的任何内容都没有定义或需要 wx.window 要求。我试图删除父级,但这也会导致 GUI 出现问题。
    • 等一下,您是否尝试过发送窗口或窗口子项作为父项。 Self 是第一个隐含参数,但您需要填充第二个...
    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多