简体   繁体   中英

AttributeError: 'Action' object has no attribute 'text1'

Experienced C programmer, total noob at python. Using python wx. In the definition of an object named Action, I declare:

    self.text2 = wx.StaticText(panel, label="Name")

but when I go to access it in the main module on a callback:

    def OnComboSelect(self, e):
        print self.combo.GetValue()
        win = Action(self, "Action")
            win.text2.SetLabel("testing")

win.SetLabel(e, "Action")

I get

AttributeError: 'Action' object has no attribute 'text2'

(I hasten to add that I have gone through all 24 'Questions that may already have your answer' but found nothing relevant.). I have checked and rechecked all spellings. I have also tried adding this function to the definition of Action:

    def SetLabel(self, event, label):
    self.text2.SetLabel("testing")

which gets the same error if I call:

win.text2.SetLabel("testing")

(surprise!). (But there are no complaints about the def defining SetLabel). This is the complete code in case required (it is simple sample code so apart from my little struggle to modify it, it is well sorted.):

#!/usr/bin/python
# -*- coding: utf-8 -*-

# action.py

import wx

class Action(wx.Frame):

    def __init__(self, parent, title):    
        super(Action, self).__init__(parent, title=title, 
            size=(450, 350))


    def InitUI(self):

        panel = wx.Panel(self)

        sizer = wx.GridBagSizer(5, 5)

        text1 = wx.StaticText(panel, label="Ink Cartridge Type")
        sizer.Add(text1, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM, 
            border=15)

        icon = wx.StaticBitmap(panel, bitmap=wx.Bitmap('ink64.png'))
        sizer.Add(icon, pos=(0, 4), flag=wx.TOP|wx.RIGHT|wx.ALIGN_RIGHT, 
            border=5)

        line = wx.StaticLine(panel)
        sizer.Add(line, pos=(1, 0), span=(1, 5), 
            flag=wx.EXPAND|wx.BOTTOM, border=10)

        self.text2 = wx.StaticText(panel, label="Name")
        sizer.Add(text2, pos=(2, 0), flag=wx.LEFT, border=10)

        self.tc1 = wx.TextCtrl(panel)
        sizer.Add(self.tc1, pos=(2, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND)

        text3 = wx.StaticText(panel, label="Package")
        sizer.Add(text3, pos=(3, 0), flag=wx.LEFT|wx.TOP, border=10)

        tc2 = wx.TextCtrl(panel)
        sizer.Add(tc2, pos=(3, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND, 
            border=5)

        button1 = wx.Button(panel, label="Browse...")
        sizer.Add(button1, pos=(3, 4), flag=wx.TOP|wx.RIGHT, border=5)

        text4 = wx.StaticText(panel, label="Extends")
        sizer.Add(text4, pos=(4, 0), flag=wx.TOP|wx.LEFT, border=10)

        combo = wx.ComboBox(panel)
        sizer.Add(combo, pos=(4, 1), span=(1, 3), 
            flag=wx.TOP|wx.EXPAND, border=5)

        button2 = wx.Button(panel, label="Browse...")
        sizer.Add(button2, pos=(4, 4), flag=wx.TOP|wx.RIGHT, border=5)

        sb = wx.StaticBox(panel, label="Optional Attributes")

        boxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
        boxsizer.Add(wx.CheckBox(panel, label="Public"), 
            flag=wx.LEFT|wx.TOP, border=5)
        boxsizer.Add(wx.CheckBox(panel, label="Generate Default Constructor"),
            flag=wx.LEFT, border=5)
        boxsizer.Add(wx.CheckBox(panel, label="Generate Main Method"), 
            flag=wx.LEFT|wx.BOTTOM, border=5)
        sizer.Add(boxsizer, pos=(5, 0), span=(1, 5), 
            flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT , border=10)

        button3 = wx.Button(panel, label='Help')
        sizer.Add(button3, pos=(7, 0), flag=wx.LEFT, border=10)

        button4 = wx.Button(panel, label="Ok")
        sizer.Add(button4, pos=(7, 3))

        # Set event handlers
        button4.Bind(wx.EVT_BUTTON, self.OnButton)

        button5 = wx.Button(panel, label="Cancel")
        sizer.Add(button5, pos=(7, 4), span=(1, 1),  
            flag=wx.BOTTOM|wx.RIGHT, border=5)

        sizer.AddGrowableCol(2)

        panel.SetSizer(sizer)

    def OnButton(self, event):
        self.tc1.GetValue()

    def SetLabel(self, event, label):
        self.text2.SetLabel("testing")

You don't create the text2 attribute until the InitUI method. But you're trying to access it before that method is getting called.

Normally, in wx's InitUI idiom, you call self.InitUI() explicitly from your __init__ method, as in this example . You're not doing that.

So, when you do this:

win = Action(self, "Action")
win.text2.SetLabel("testing")

You've called win.__init__ , but it hasn't called InitUI , and neither has anything else, so the attribute doesn't exist yet.

You have to make text1 self.text1 as well as every other attribute you want this class to have. As is text1 is just a local variable inside InitUI.

Ooops sorry didn't read very carefully.

You can change win.text2.SetLabel("testing") to win.text2 = "testing" the way this code is here python thinks text2 is an object inside of Action with its own method called SetLabel. Thats why it cant find it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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