pyqt5自定义消息对话框,支持返回对话框中文本框的内容,对话框关闭后的状态。python版本:Python 3.6.6,PyQt5版本:PyQt5 5.15.4
对话框代码,tanchuang.py
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLineEdit, QDialogButtonBox, QWidget, \
QTextEdit, QLabel,QHBoxLayout
# 短信确定对话框
class CustomNoteDialog(QDialog):
#输入参数
def __init__(self,content,phoneNumber,edit_width=880,edit_height=400,win_title="信息确认",info_title="信息确定",parent=None):
super(CustomNoteDialog, self).__init__(parent)
self.setWindowTitle(win_title)
# 窗口大小
# self.resize(900,600)
# 创建组件
# 标题标签
self.label_title = QLabel(info_title,self)
self.label_title.setFont(QFont("微软雅黑", 20))
# 电话号码标签
self.label_phone_number = QLabel("电话号码",self)
self.label_phone_number.setFont(QFont("微软雅黑", 12))
# 电话号码单行文本框
self.led_phone_number = QLineEdit(self)
self.led_phone_number.setFont(QFont("微软雅黑", 12))
self.led_phone_number.setMaximumWidth(400)
self.led_phone_number.setText(phoneNumber)
# 短信内容标签
self.label_note_content = QLabel("短信内容",self)
self.label_note_content.setFont(QFont("微软雅黑", 12))
# 短信内容富文本编辑框
self.qTextEdit_note_content = QTextEdit(self)
self.qTextEdit_note_content.setMinimumHeight(edit_height)
self.qTextEdit_note_content.setMinimumWidth(edit_width)
# self.qTextEdit.setFont(QFont("微软雅黑", 12))
self.qTextEdit_note_content.setText(content)
# 确定和取消按钮
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel,self)
self.ok = self.buttonBox.button(QDialogButtonBox.Ok).setText("确定")
self.cancel = self.buttonBox.button(QDialogButtonBox.Cancel).setText("取消")
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
# 创建2个容器,用于撑开水平布局,使标题居中
self.widget1 = QWidget()
self.widget2 = QWidget()
# 布局设置
# 垂直布局
layoutx = QVBoxLayout(self)
# 水平布局
layouty = QHBoxLayout(self)
# 空容器1添加到水平布局
layouty.addWidget(self.widget1)
# 标题标签添加到水平布局
layouty.addWidget(self.label_title)
# 空容器2添加到水平布局
layouty.addWidget(self.widget2)
# 水平布局添加到垂直布局中
layoutx.addLayout(layouty)
# 电话号码标签添加到垂直布局
layoutx.addWidget(self.label_phone_number)
# 电话号码单行文本框添加到垂直布局
layoutx.addWidget(self.led_phone_number)
# 短信内容标签添加到垂直布局
layoutx.addWidget(self.label_note_content)
# 短信内容富文本框加到垂直布局
layoutx.addWidget(self.qTextEdit_note_content)
# 确定取消按钮添加到垂直布局
layoutx.addWidget(self.buttonBox)
'''
调用短信确定对话框,获取窗口关闭后的状态、电话号码、短信内容,使用对话框时,直接调用此方法
对话框大小是根据短信内容文本框的尺寸决定的,比短信内容文本框高一些。
输入参数说明:
win_title:窗口标题(窗口右上角),info_title:内容标题,content:短信内容,phoneNumber:电话号码,edit_width:短信内容文本框宽,edit_height:短信内容文本框高
'''
def Custom_QTextEdit_Dialog(win_title,info_title,content,phoneNumber,edit_width,edit_height):
# 调用短信确定对话框,传入参数
dialog = CustomNoteDialog(win_title=win_title,info_title=info_title,phoneNumber=phoneNumber,content=content,edit_width=edit_width,edit_height=edit_height)
# 获取对话框关闭后的状态
zt = dialog.exec_()
# 点击了确定按钮
if zt:
zt1=True
# 点击了取消按钮或关闭了弹窗
else:
zt1=False
# 获取短信框中的短信内容
content = dialog.qTextEdit_note_content.toPlainText()
# 获取电话号码框中的电话号码
phone_number = dialog.led_phone_number.text()
# 返回对话框状态、短信内容、电话号码
return zt1,content,phone_number
调用自定义对话框,获取返回值,main.py
import sys
from PyQt5.QtWidgets import *
import tanchaung
app=QApplication(sys.argv)
window=QWidget()
dialog = tanchaung.Custom_QTextEdit_Dialog(win_title="弹窗",info_title="信息确认",content="哈哈哈哈哈",phoneNumber="19993523458",edit_width=880,edit_height=400)
print(dialog)
结果
打印出的返回值,元组第一个值为关闭窗口的状态,True代表点击了确定按钮,False代表点击了取消按钮或点击了对话框右上角关闭按钮;第二个值为文本框中的短信内容,第三个值为电话号码文本框中的内容