Python PyQt5 QMessageBox 未打开 [英] Python PyQt5 QMessageBox not opening
本文介绍了Python PyQt5 QMessageBox 未打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我按下 infoButton 时,我试图打开一个消息框.它运行 infoDialogue 方法,因为它打印我在这里"但它不打开信息框.我错过了什么?
I am trying to open a Message Box when I press the infoButton. It runs the infoDialogue method because it prints "I'm here" but it doesn't open the infoBox. What I'm missing?
我在 PyQt5 中使用 python 3.5
I'm using python 3.5 with PyQt5
谢谢!
代码如下:
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QToolTip, QPushButton, QMessageBox)
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QIcon, QFont
class mainWindow(QWidget):
def __init__(self, screenWidth, screenHeight, windowWidth=400, windowHeight=400):
super().__init__()
self.screenWidth = screenWidth
self.screenHeight = screenHeight
self.windowWidth = windowWidth
self.windowHeight = windowHeight
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('ToolTip Widget')
exitButton = QPushButton('Exit', self)
exitButton.setToolTip("<b>Wish to Exit?</b>")
exitButton.resize(exitButton.sizeHint())
exitButton.move(100, 100)
exitButton.clicked.connect(QCoreApplication.instance().quit)
infoButton = QPushButton('Info', self) # Button that calls infoDialogue()
infoButton.setToolTip('<b>ToolTip</b>')
infoButton.resize(infoButton.sizeHint())
infoButton.move(100, 200)
infoButton.clicked.connect(self.infoDialogue)
positionX = (self.screenWidth - self.windowWidth) / 2
positionY = (self.screenHeight - self.windowHeight) / 2
self.setGeometry(positionX, positionY, self.windowWidth, self.windowHeight)
self.setWindowTitle('Window Title')
#self.setWindowIcon(QIcon('./icon.png'))
self.show()
def infoDialogue(self): ## Method to open a message box
infoBox = QMessageBox() ##Message Box that doesn't run
print("Im here")
infoBox.setIcon(QMessageBox.Information)
infoBox.setText("Informações Adicionais")
infoBox.setInformativeText("Informative Text")
infoBox.setWindowTitle("Window Title")
infoBox.setDetailedText("Detailed Text")
infoBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
infoBox.setEscapeButton(QMessageBox.Close)
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Exit', "Are you sure you want to exit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
screenResolution = app.desktop().screenGeometry()
screenWidth = screenResolution.width()
screenHeight = screenResolution.height()
example = mainWindow(screenWidth, screenHeight)
sys.exit(app.exec_())
推荐答案
您可能需要一个
infoBox.exec_()
最后实际执行它
这篇关于Python PyQt5 QMessageBox 未打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文