简体   繁体   English

如何在PyQT4上显示消息框?

[英]How to display a message box on PyQT4?

I'd like a MessageBox to display when I click a button on my simple PyQT application. 当我点击简单的PyQT应用程序上的按钮时,我想要显示一个MessageBox。 How can I declare two textboxes and have a MessageBox display with the text from both textboxes? 如何声明两个文本框并使用两个文本框中的文本显示MessageBox?

Here's my code far: 这是我的代码:

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_())

Since such simple code is a common request, I decided to hack something basic together, here you go: 由于这样简单的代码是一个常见的请求,我决定将基本的东西放在一起,在这里你去:

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_()

Write something into the line edits (text boxes), click the button. 在行编辑(文本框)中写入内容,单击按钮。 Profit! 利润! :-) :-)

Note: it can be done with less code, but this is a good PyQt coding practice - create a widget to serve as a central widget of a window, populate it with a layout, etc. 注意:它可以用更少的代码完成,但这是一个很好的PyQt编码实践 - 创建一个小部件作为窗口的中央小部件,用布局填充它等。

PyQt comes with examples when you install it. PyQt在安装时附带了示例。 These examples contain a lot of very useful code and you can learn from them, as well as take whole code chunks and use them. 这些示例包含许多非常有用的代码,您可以从中学习,以及获取整个代码块并使用它们。

Check out, for example, the "Address book" example which pops message boxes along other things (search its sources for "messagebox"). 例如,查看“地址簿”示例,该示例在其他内容中弹出消息框(搜索其“messagebox”的来源)。

在此输入图像描述

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 http://pyqt.sourceforge.net/Docs/PyQt4/qmessagebox.html
  2. http://www.programcreek.com/python/example/62361/PyQt4.QtGui.QMessageBox http://www.programcreek.com/python/example/62361/PyQt4.QtGui.QMessageBox

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM