Python 2.7 像普通OOP类一样,在wx Python中创建类的实例
我试图同时学习OOP和wx Python,我发现在使用wx时创建类实例很困难。我不知道我遗漏了什么,但是看到下面的代码,你可以看到我与循环前最后一行的混淆。在正常的继承场景中,我可以创建DailyStasks类的实例,然后使用该实例调用方法。为什么不在wx?是因为我没有在父类中使用对象类,而是必须使用wx.Frame和wx.Panel吗?我希望我的要求有道理。代码的第一部分是我在没有wx的情况下所期望的行为,第二组代码是让我困惑的wx代码。提前感谢你的帮助Python 2.7 像普通OOP类一样,在wx Python中创建类的实例,python-2.7,inheritance,wxpython,instantiation,Python 2.7,Inheritance,Wxpython,Instantiation,我试图同时学习OOP和wx Python,我发现在使用wx时创建类实例很困难。我不知道我遗漏了什么,但是看到下面的代码,你可以看到我与循环前最后一行的混淆。在正常的继承场景中,我可以创建DailyStasks类的实例,然后使用该实例调用方法。为什么不在wx?是因为我没有在父类中使用对象类,而是必须使用wx.Frame和wx.Panel吗?我希望我的要求有道理。代码的第一部分是我在没有wx的情况下所期望的行为,第二组代码是让我困惑的wx代码。提前感谢你的帮助 ---------- class A
----------
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()
wx应该允许以正常方式继承。当您将dt分配给DailyTask()实例时,您能否详细说明实际行为是什么?这就是问题所在。当我尝试用dt实例化时,它说它需要2个参数,可能是因为DailyStasks init中的父输入。我试图放在括号中的内容要么没有定义,要么需要wx.window要求。我已尝试删除父项,但这也会导致GUI出现问题。请稍候,您是否尝试发送窗口或窗口子项作为父项。Self是第一个隐含参数,但您需要填充第二个。。。