Search code examples
wxpython

WxPython: type object 'Test' has no attribute 'openReportButton'


I'm trying to enable a button on click of another button but it keeps giving me this error (see title)

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()

What am I missing here?

Thank you,


Solution

  • You need to make openReportButton an instance attribute (instead of just a local variable)

    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()