如何在PyQT4中显示消息框?

15

我想在单纯的PyQT应用程序中单击按钮时显示一个MessageBox。如何声明两个文本框,并使用两个文本框中的文本来显示MessageBox?

以下是我的代码:

import sys
from PyQt4 import QtGui, QtCore

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        #The setGeometry method is used to position the control.
        #Order: X, Y position - Width, Height of control.
        self.setGeometry(300, 300, 500, 350)
        self.setWindowTitle("Sergio's QT Application.")
        self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png'))

        self.setToolTip('<i>Welcome</i> to the <b>first</b> app ever!')
        QtGui.QToolTip.setFont(QtGui.QFont('Helvetica', 12))

        txtFirstName = QtGui.?
        txtLastName = QtGui.?

        btnQuit = QtGui.QPushButton('Exit Application', self)
        btnQuit.setGeometry(340, 300, 150, 35)

        self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
                    QtGui.qApp, QtCore.SLOT('quit()'))

app = QtGui.QApplication(sys.argv)
mainForm = myWindow()
mainForm.show()
sys.exit(app.exec_())
3个回答

23

既然这样简单的代码是一个常见的要求,我决定想办法编写一些基础代码,给你:

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.create_main_frame()       

    def create_main_frame(self):        
        page = QWidget()        

        self.button = QPushButton('joy', page)
        self.edit1 = QLineEdit()
        self.edit2 = QLineEdit()

        vbox1 = QVBoxLayout()
        vbox1.addWidget(self.edit1)
        vbox1.addWidget(self.edit2)
        vbox1.addWidget(self.button)
        page.setLayout(vbox1)
        self.setCentralWidget(page)

        self.connect(self.button, SIGNAL("clicked()"), self.clicked)

    def clicked(self):
        QMessageBox.about(self, "My message box", "Text1 = %s, Text2 = %s" % (
            self.edit1.text(), self.edit2.text()))



if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

在文本框中输入内容,点击按钮即可。Profit! :-)

注意:可以用更少的代码完成,但这是一个良好的PyQt编程实践 - 创建一个小部件作为窗口的中心小部件,使用布局填充它等。


4

安装PyQt时,会自带示例代码。这些示例包含了许多非常有用的代码,您可以从中学习,也可以使用整个代码块。

例如,请查看“通讯录”示例,其中弹出消息框等功能(在其源代码中搜索“messagebox”)。


我在Windows上使用二进制安装程序安装了PyQT。请问我在哪里可以找到这些示例?谢谢。 - delete
尝试在开始菜单中找到PyQT安装的链接,它应该在那里。 - Eli Bendersky
@Serg:一个直接的链接是(适用于Python 2.6,如果你的版本不同,请进行调整):C:\Python26\Lib\site-packages\PyQt4\examples - Eli Bendersky
谢谢各位,我找到了例子。不幸的是,我似乎找不到与消息框有关的任何东西。你们能发布一些针对这个问题的小代码吗?感谢你们的时间。 - delete
@Serg:请查看我在此问题的另一个答案(https://dev59.com/6m855IYBdhLWcg3wxHUq#4155232) - Eli Bendersky
我会收藏ZetCode,他们提供了一些很棒的示例。http://zetcode.com/gui/pyqt4/dialogs/ - NuclearPeon

3

enter image description here

from PyQt4 import QtGui, QtCore

class Window( QtGui.QWidget ):

    def __init__( self ):
        QtGui.QWidget.__init__( self )

        msgBox = QtGui.QMessageBox( self )
        msgBox.setIcon( QtGui.QMessageBox.Information )
        msgBox.setText( "Do not stare into laser with remaining eye" )

        msgBox.setInformativeText( "Do you really want to disable safety enforcement?" )
        msgBox.addButton( QtGui.QMessageBox.Yes )
        msgBox.addButton( QtGui.QMessageBox.No )

        msgBox.setDefaultButton( QtGui.QMessageBox.No ) 
        ret = msgBox.exec_()

        if ret == QtGui.QMessageBox.Yes:
            print( "Yes" )
            return
        else:
            print( "No" )
            return

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication( sys.argv )
    window = Window()
    # window.show()
    sys.exit( app.exec_() )

Source:

  1. http://pyqt.sourceforge.net/Docs/PyQt4/qmessagebox.html
  2. http://www.programcreek.com/python/example/62361/PyQt4.QtGui.QMessageBox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接