用python学习GUI(PyQt4)在statusb上显示消息

2024-04-25 08:31:51 发布

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

我在学Python。我对它不熟悉。你知道吗

http://zetcode.com/gui/pyqt4/firstprograms/

现在,我正在试着做一个锻炼用的计算器。我修改了一点示例程序。但我错了。你知道吗

Traceback (most recent call last):
  File "customCalculatorGUI.py", line 50, in buttonClicked
    self.showMessage(sender.text() + ' was pressed')
AttributeError: 'Example' object has no attribute 'showMessage'

#你知道吗

import sys
from PyQt4 import QtGui, QtCore

"""
ZetCode PyQt4 tutorial 

In this example, we create a skeleton
of a calculator using a QtGui.QGridLayout.

author: Jan Bodnar
website: zetcode.com 
last edited: July 2014
"""

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        grid = QtGui.QGridLayout()
        self.setLayout(grid)

        names = ['Cls', 'Bck', '', 'Close',
                 '7', '8', '9', '/',
                '4', '5', '6', '*',
                 '1', '2', '3', '-',
                '0', '.', '=', '+']

        positions = [(i,j) for i in range(5) for j in range(4)]

        for position, name in zip(positions, names):

            if name == '':
                continue
            button = QtGui.QPushButton(name)
            button.clicked.connect(self.buttonClicked)
            grid.addWidget(button, *position)


        self.move(300, 150)
        self.setWindowTitle('Calculator')
        self.show()

    def buttonClicked(self):

        sender = self.sender()
        self.showMessage(sender.text() + ' was pressed')

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我查过了 http://qt-project.org/doc/qt-4.8/qwidget.html

没有showMessage类。 但现在,我不知道如何显示信息。你知道吗

如果我想在单击按钮时在状态栏上显示消息。 我该怎么做?你知道吗


Tags: nameinselfhttpformainexampledef