Files
MenuPython_QT/QT5_Project/KD_ZM_6/DialogInform.py

61 lines
2.3 KiB
Python

import sys
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
ui_file_path = sys.path[0] + "/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
button_box.button(QDialogButtonBox.Ok).setText('确定')
#定义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)