简体   繁体   English

AttributeError:“操作”对象没有属性“ text1”

[英]AttributeError: 'Action' object has no attribute 'text1'

Experienced C programmer, total noob at python. 经验丰富的C程序员,对Python完全陌生。 Using python wx. 使用python wx。 In the definition of an object named Action, I declare: 在名为Action的对象的定义中,我声明:

    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") win.SetLabel(e,“动作”)

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.). (我还要补充一点,我已经回答了所有24个“可能已经有了答案的问题”,但没有发现任何问题。) I have checked and rechecked all spellings. 我检查并重新检查了所有拼写。 I have also tried adding this function to the definition of Action: 我也尝试过将此功能添加到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). (但是对于定义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. 直到使用InitUI方法,您才创建text2属性。 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 . 通常,在wx的InitUI习惯用法中,您可以通过__init__方法显式调用self.InitUI() ,如本例所示 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. 您已调用win.__init__ ,但尚未调用InitUI ,也没有任何其他内容,因此该属性尚不存在。

You have to make text1 self.text1 as well as every other attribute you want this class to have. 您必须使text1 self.text1以及您希望此类具有的所有其他属性。 As is text1 is just a local variable inside InitUI. 正如text1一样,它只是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. 您可以将win.text2.SetLabel(“ testing”)更改为win.text2 =“ testing”,此代码在此处的方式python认为text2是Action内部的一个对象,其自身的方法称为SetLabel。 Thats why it cant find it. 那就是为什么它找不到它。

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

相关问题 AttributeError: 'Event' object 没有属性 'Text1' - AttributeError: 'Event' object has no attribute 'Text1' Beautifulsoup AttributeError:“列表”对象没有属性“文本” - Beautifulsoup AttributeError: 'list' object has no attribute 'text' AttributeError: 'Response' 对象没有属性 'text' - AttributeError: 'Response' object has no attribute 'text' AttributeError:'NoneType'对象没有属性'text'? - AttributeError: 'NoneType' object has no attribute 'text'? Pytorch 文本 AttributeError: 'BucketIterator' 对象没有属性 - Pytorch Text AttributeError: ‘BucketIterator’ object has no attribute AttributeError: 'NoneType' 对象没有属性 'text' BeautifulSoup - AttributeError: 'NoneType' object has no attribute 'text' BeautifulSoup AttributeError:“str”对象没有属性“文本” - AttributeError: "str' object has no attribute 'text AttributeError: 'NoneType' object 没有属性 'text' - BeautifulShop - AttributeError: 'NoneType' object has no attribute 'text' - BeautifulShop AttributeError:'NoneType'对象没有属性'text'-元组 - AttributeError: 'NoneType' object has no attribute 'text' - tuples AttributeError:'NoneType'对象没有属性'text'kivy - AttributeError: 'NoneType' object has no attribute 'text' kivy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM