使用PyQt5和Qt Designer创建Python GUI应用程序(五)
如何创建PyQt5消息框:
QMessageBox
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class Linuxidc(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5消息框 - Linux公社 www.linuxidc.com'
self.left = 50
self.top = 50
self.width = 600
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
buttonReply = QMessageBox.question(self, 'PyQt5消息框', "你喜欢PyQt5吗?Linux公社 www.linuxidc.com", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if buttonReply == QMessageBox.Yes:
print('单击Yes')
else:
print('单击No')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Linuxidc()
sys.exit(app.exec_())
buttonReply = QMessageBox.question(self, 'PyQt5消息框', "你喜欢PyQt5吗?Linux公社 www.linuxidc.com", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Cancel)
print(int(buttonReply))
if buttonReply == QMessageBox.Yes:
print('点击了YES')
if buttonReply == QMessageBox.No:
print('点击了No')
if buttonReply == QMessageBox.Cancel:
print('取消')
QMessageBox.Open QMessageBox.Save QMessageBox.SaveAll
QMessageBox.Discard QMessageBox.Close QMessageBox.Apply
QMessageBox.Reset QMessageBox.Yes QMessageBox.YesToAll
QMessageBox.No QMessageBox.NoToAll QMessageBox.NoButton
QMessageBox.RestoreDefaults QMessageBox.Abort QMessageBox.Retry
QMessageBox.Ignore
创建弹出窗口:
def window():
app = QApplication(sys.argv)
win = QWidget()
button1 = QPushButton(win)
button1.setText("点击这里显示对话框 www.linuxidc.com")
button1.move(200,50)
button1.clicked.connect(showDialog)
win.setWindowTitle("显示对话框www.linuxidc.com")
win.show()
sys.exit(app.exec_())
from PyQt5.QtWidgets import QPushButton
def showDialog():
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Information)
msgBox.setText("消息框弹出窗口www.linuxidc.com")
msgBox.setWindowTitle("消息框示例www.linuxidc.com")
msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msgBox.buttonClicked.connect(msgButtonClick)
returnValue = msgBox.exec()
if returnValue == QMessageBox.Ok:
print('点击了 OK')
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
def window():
app = QApplication(sys.argv)
win = QWidget()
button1 = QPushButton(win)
button1.setText("点击这里显示对话框 www.linuxidc.com")
button1.move(200,50)
button1.clicked.connect(showDialog)
win.setWindowTitle("显示对话框www.linuxidc.com")
win.show()
sys.exit(app.exec_())
def showDialog():
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Information)
msgBox.setText("消息框弹出窗口www.linuxidc.com")
msgBox.setWindowTitle("消息框示例www.linuxidc.com")
msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msgBox.buttonClicked.connect(msgButtonClick)
returnValue = msgBox.exec()
if returnValue == QMessageBox.Ok:
print('点击了 OK')
def msgButtonClick(i):
print("点击的按钮是:",i.text())
if __name__ == '__main__':
window()
GUI应用程序 见 https://www.linuxidc.com/search.aspx?where=nkey&keyword=65658
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址:https://www.linuxidc.com/Linux/2020-05/163168.htm
微信赞赏支付宝赞赏