Pyqt-qmessagebox

提供:Dev Guides
移動先:案内検索

PyQt-QMessageBox

*QMessageBox* は、いくつかの情報メッセージを表示し、オプションでユーザーに標準ボタンのいずれかをクリックして応答するように要求するために一般的に使用されるモーダルダイアログです。 各標準ボタンには、定義済みのキャプション、ロールがあり、定義済みの16進数を返します。

QMessageBoxクラスに関連付けられている重要なメソッドと列挙は、次の表に記載されています-

Sr.No. Methods & Description
1

setIcon()

メッセージの重大度に対応する事前定義されたアイコンを表示します

質問質問

情報情報

警告警告

クリティカルクリティカル

2

setText()

表示するメインメッセージのテキストを設定します

3

setInformativeText()

追加情報を表示します

4

setDetailText()

ダイアログには詳細ボタンが表示されます。 クリックするとこのテキストが表示されます

5

setTitle()

ダイアログのカスタムタイトルを表示します

6

setStandardButtons()

表示される標準ボタンのリスト。 各ボタンはに関連付けられています

QMessageBox.Ok 0x00000400

QMessageBox.Open 0x00002000

QMessageBox.Save 0x00000800

QMessageBox.Cancel 0x00400000

QMessageBox.Close 0x00200000

QMessageBox。はい0x00004000

QMessageBox.No 0x00010000

QMessageBox.Abort 0x00040000

QMessageBox.Retry 0x00080000

QMessageBox.Ignore 0x00100000

7

setDefaultButton()

ボタンをデフォルトとして設定します。 Enterが押されると、クリックされた信号を発します

8

setEscapeButton()

エスケープキーが押された場合にクリックされたものとしてボタンが処理されるように設定します

次の例では、トップレベルウィンドウのボタンの信号をクリックすると、接続された関数はメッセージボックスダイアログを表示します。

msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("This is a message box")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")

setStandardButton()関数は、目的のボタンを表示します。

msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

buttonClicked()シグナルは、シグナルのソースのキャプションを識別するスロット関数に接続されています。

msg.buttonClicked.connect(msgbtn)

例の完全なコードは次のとおりです-

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

def window():
   app = QApplication(sys.argv)
   w = QWidget()
   b = QPushButton(w)
   b.setText("Show message!")

   b.move(50,50)
   b.clicked.connect(showdialog)
   w.setWindowTitle("PyQt Dialog demo")
   w.show()
   sys.exit(app.exec_())

def showdialog():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Information)

   msg.setText("This is a message box")
   msg.setInformativeText("This is additional information")
   msg.setWindowTitle("MessageBox demo")
   msg.setDetailedText("The details are as follows:")
   msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
   msg.buttonClicked.connect(msgbtn)

   retval = msg.exec_()
   print "value of pressed message box button:", retval

def msgbtn(i):
   print "Button pressed is:",i.text()

if __name__ == '__main__':
   window()

上記のコードは、次の出力を生成します-

QMessageBox Output1 QMessageBox Output2