web-dev-qa-db-ja.com

PyQT4でメッセージボックスを表示する方法

シンプルなPyQTアプリケーションのボタンをクリックしたときにMessageBoxを表示したいのですが。 2つのテキストボックスを宣言し、両方のテキストボックスのテキストをメッセージボックスに表示するにはどうすればよいですか?

これが私のコードです:

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

そのような単純なコードは一般的な要求なので、基本的なものを一緒にハックすることにしました。

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

行編集(テキストボックス)に何かを書き込んで、ボタンをクリックします。利益! :-)

注:少ないコードで実行できますが、これはPyQtの優れたコーディング方法です。ウィンドウの中央ウィジェットとして機能するウィジェットを作成したり、レイアウトを追加したりします。

23
Eli Bendersky

PyQtをインストールすると、サンプルが付属します。これらの例には非常に便利なコードがたくさん含まれており、コードチャンク全体を取得して使用するだけでなく、それらから学ぶことができます。

たとえば、他のものに沿ってメッセージボックスをポップする "アドレス帳"の例をチェックしてください( "messagebox"のソースを検索してください)。

4
Eli Bendersky

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

ソース:

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