WxPython:类型对象“ Test”没有属性“ openReportButton”

时间:2019-05-30 03:17:40

标签: wxpython

我正在尝试在单击另一个按钮时启用一个按钮,但是它一直给我这个错误(请参见标题)

class Test(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id, "Frame aka Window", style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX, size=(400, 635))

        loadData = wx.Button(panel, label="Load Data", size = (100,40))
        loadData.SetFont(font)
        self.Bind(wx.EVT_BUTTON, self.loadData, loadData)

        openReportButton = wx.Button(panel, label = "Open Report", size = (100,40))
        openReportButton.SetFont(font)
        openReportButton.Disable()
        self.Bind(wx.EVT_BUTTON, self.openReport, openReportButton)

    def loadData(self, event):
        self.openReportButton.Enable()

我在这里想念什么?

谢谢

1 个答案:

答案 0 :(得分:2)

您需要将openReportButton设置为实例属性(而不只是局部变量)

class Test(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id, "Frame aka Window", style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX, size=(400, 635))

        loadData = wx.Button(panel, label="Load Data", size = (100,40))
        loadData.SetFont(font)
        self.Bind(wx.EVT_BUTTON, self.loadData, loadData)
        # added "self."
        self.openReportButton = wx.Button(panel, label = "Open Report", size = (100,40))
        self.openReportButton.SetFont(font)
        self.openReportButton.Disable()
        self.Bind(wx.EVT_BUTTON, self.openReport, openReportButton)

    def loadData(self, event):
        self.openReportButton.Enable()