Skip to main content
 首页 » 编程设计

python之如何纠正python错误: "icon object is not callable"

2025年05月04日15soundcode

我有一本字典,其中包含某些元素和关联的键。我想创建一个 GUI 来显示项目。我在 for 循环中使用了 QMessageBox PyQt 小部件。但是当我运行代码时出现以下错误:

追溯(最近调用最后):文件“C:\Python34_64bit\dictt.py”,第 50 行,在 main() 文件“C:\Python34_64bit\dictt.py”,第 45 行,在 main GUI=MYGUI() 文件“C:\Python34_64bit\dictt.py”,第 31 行,在 init 中否)类型错误:“图标”对象不可调用

请帮助我如何通过修改我的代码来解决这个问题。下面是我的代码:

import sys 
from PyQt4 import QtGui,QtCore 
 
class MYGUI(QtGui.QWidget): 
 
    def __init__(self): 
        super(MYGUI,self).__init__() 
 
        self.setWindowTitle("GUI") 
 
        #widgets: 
 
        self.labl=QtGui.QLabel(self)     
        self.labl.setFont(QtGui.QFont('Calibri', 34)) 
 
 
        #Layout: 
 
        Layout =QtGui.QVBoxLayout() 
        Layout.addWidget(self.labl) 
        Layout.addStretch() 
        self.setLayout(Layout) 
 
        #Actions:                 
 
 
        Queries={'Q1':'question 1','Q2':'question2'} 
 
        for k,val in Queries.items(): 
 
            self.Choice=QtGui.QMessageBox.Question(self,k,val,QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) 
 
            if choice==QtGui.QMessageBox.Yes: 
                self.labl.setText('yes') 
 
            else: 
                self.labl.setText('No') 
 
        self.show() 
 
 
def main(): 
 
    app=QtGui.QApplication(sys.argv) 
    GUI=MYGUI() 
 
    sys.exit(app.exec_()) 
 
 
main() 

请您参考如下方法:

你的问题只是小写/大写。

QMessageBox.Question是图标

QMessageBox.question(parent, title, text, button0, button1)是函数

参见:https://srinikom.github.io/pyside-docs/PySide/QtGui/QMessageBox.html