简体   繁体   English

Python调用父类属性

[英]Python call parent class attribute

I make some experiences in Python especially in wxpython from EventGhost but I have some generally problem with classes. 我在Python中有一些经验,特别是在EventGhost的wxpython中,但是我对类有一些普遍的问题。 I have watched around and tried a lot but have no success. 我环顾四周,尝试了很多,但没有成功。

My problem is that I want to close my Gui from a button inside my "MyDialog()" class: 我的问题是我想从“ MyDialog()”类中的按钮关闭Gui:

class ShowInputDialog(eg.ActionBase):
    name = "Show Input Dialog"  
    description = "Show an input dialog that allows you to create an EventGhost event that you can then use to trigger AutoRemote messages or notifications"
    def __call__(self):
        class MyDialog():
            def __init__(self):

                ########################Main Dialog###########################
                no_sys_menu = wx.CLIP_CHILDREN | wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR | wx.NO_BORDER | wx.FRAME_SHAPED
                self.Dialog = wx.Frame(None, wx.ID_ANY, "Hello World", style=no_sys_menu, size=(400,600))

                ########################Header###########################
                Header = wx.Panel(self.Dialog, wx.ID_ANY, size=(400,600)) 
                HeaderSizer = wx.GridSizer(rows=1, cols=2, hgap=5, vgap=5)

                HeaderSizer.Add(wx.StaticText(Header, label="Hello World"), flag=wx.ALIGN_CENTER_VERTICAL)

                button = wx.Button(Header, label='close')
                button.Bind(wx.EVT_BUTTON, self.close)
                HeaderSizer.Add(button, 0, wx.ALIGN_RIGHT, 0)

                Header.SetSizer(HeaderSizer) 

                upDownSizer = wx.BoxSizer(wx.VERTICAL)
                upDownSizer.Add(Header, 0, flag=wx.EXPAND)            
                self.Dialog.SetSizer(upDownSizer) 

                self.Dialog.Fit()
                self.Dialog.Show()


            def close(self, event):
                self.Close()
                print "see you soon"

        wx.CallAfter(MyDialog)  

if I call "close" from my button I get 如果我通过按钮调用“关闭”,我会得到

AttributeError: MyDialog instance has no attribute 'Close'

but how to call "Close"? 但是如何称呼“关闭”? I Have read about to super the init of "MyDialog" but have no success doing that and also don't know if this would clear my problem. 我已经读过有关“ MyDialog”的初始化的文章,但是这样做没有成功,也不知道这样做是否可以解决我的问题。

Thanks and be not so hard to a noob 谢谢,对菜鸟不要那么难

self is your own class, it is not a wx Class ... if you want it to have the attributes of a wx.Dialog you need to inherit from wx.Dialog self是您自己的类,它不是wx类...如果您希望它具有wx.Dialog的属性,则需要从wx.Dialog继承

the easiest solution is probably to just call close on self.Dialog which appears to be your actual instance of a dialog 最简单的解决方案可能是在self.Dialog上调用close,这似乎是您实际的对话框实例

def close(self, event):
     self.Dialog.Close()
     print "see you soon"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM