import sys import os from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QLineEdit, QWidget, QPushButton, QMessageBox, QComboBox, QDialogButtonBox, QShortcut from PyQt5 import uic from PyQt5.QtGui import QKeySequence from PyQt5.QtCore import Qt, QEvent, QObject, QTimer sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) ui_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../Shared_UI')) ui_file_path = os.path.join(ui_path, "DialogInform.ui") class DialogInform(QDialog): def __init__(self, parent=None): super(DialogInform, self).__init__(parent) # Load UI file uic.loadUi(ui_file_path, self) label :QLabel = self.label label.setText("") label.setWordWrap(True) # 允许文本自动换行 self.message_timer = QTimer() self.message_timer.timeout.connect(self.close_dialog_timeout) self.setWindowTitle("消息提示") self.setWindowFlag(Qt.FramelessWindowHint) button_box : QDialogButtonBox = self.buttonBox ok_button = button_box.button(QDialogButtonBox.Ok) if ok_button: ok_button.setText('确定') else: button_box.addButton(QDialogButtonBox.Ok) #定义4个快捷键, Key_Enter, Key_Escape经常被各类程序模块使用, 用Key_End, 与Key_Home来代替 QShortcut(QKeySequence(Qt.Key_PageDown), self, activated=self.key_enter_process) QShortcut(QKeySequence(Qt.Key_PageUp), self, activated=self.key_escape_process) QShortcut(QKeySequence(Qt.Key_End), self, activated=self.key_enter_process) QShortcut(QKeySequence(Qt.Key_Home), self, activated=self.key_escape_process) def information(self, title : str, message : str, timeout_ms : int = 2000) : self.setWindowTitle(title) label :QLabel = self.label label.setText(message) self.message_timer.start(timeout_ms) self.exec() def key_enter_process(self): button_box : QDialogButtonBox = self.buttonBox select_button = button_box.button(QDialogButtonBox.Ok) if select_button: select_button.click() def key_escape_process(self): button_box : QDialogButtonBox = self.buttonBox select_button = button_box.button(QDialogButtonBox.Cancel) if select_button: select_button.click() #消息超时 def close_dialog_timeout(self): self.key_enter_process() if __name__ == '__main__': app = QApplication(sys.argv) dialog = DialogInform() dialog.exec() sys.exit(0)