g的问题

2024-04-24 19:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

在过去的一个小时里,我一直在绞尽脑汁想找出我正在开发的支持IRC的程序中的一个bug,经过一番调试后,我发现getattr由于某种原因不能正常工作。我有以下测试代码:

def privmsg(self, user, channel, msg):
    #Callback for when the user receives a PRVMSG. 
    prvmsgText = textFormatter(msg,  
    self.factory.mainWindowInstance.ui.testWidget.ui.channelBrowser,
                                QColor(255, 0, 0, 127), 'testFont', 12)
    prvmsgText.formattedTextappend() 

一切都很完美。在

替换以下代码,代码将中断(不将文本输出到PyQT TextBrowser实例)

^{pr2}$

这两种编写textFormatter函数第二个参数的方法本质上不是等价的吗?为什么会发生这种情况,以及我如何处理这样的bug?谢谢。在

编辑:以下是textFormatter类,以防有帮助:

from timeStamp import timeStamp

class textFormatter(object):
    '''
    Formats text for output to the tab widget text browser.
    '''
    def __init__(self,text,textBrowserInstance,textColor,textFont,textSize):
        self.text = text
        self.textBrowserInstance = textBrowserInstance
        self.textColor = textColor
        self.textFont = textFont
        self.textSize = textSize

    def formattedTextAppend(self):
        timestamp = timeStamp()
        self.textBrowserInstance.setTextColor(self.textColor)
        self.textBrowserInstance.setFontPointSize(self.textSize)
        self.textBrowserInstance.append(unicode(timestamp.stamp()) + unicode(self.text))

Tags: thetextselffordefmsgbugtimestamp
1条回答
网友
1楼 · 发布于 2024-04-24 19:42:53

不,getattr将获取一个对象的属性。它无法遍历字符串中给定的层次结构。正确的方法是:

getattr(self.factory.mainWindowInstance.ui.testWidget.ui, 'channelBrowser'),
                                QColor(255, 0, 0, 127), 'testFont', 12)

或者

^{pr2}$

相关问题 更多 >