程序入口模板:pyside6界面开发笔记——模板框架
回顾:
标签控件
信号与槽
消息框控件
各种类型的消息框
类型 | 方法 |
---|---|
询问 | QMessageBox.question() |
信息 | QMessageBox.information() |
警告 | QMessageBox.waring() |
错误 | QMessageBox.critical() |
关于 | QMessageBox.about() |
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
button=QPushButton('信息框')
button.clicked.connect(self.show_info)
h_layout=QHBoxLayout()
h_layout.addWidget(button)
self.setLayout(h_layout)
def show_info(self):
QMessageBox.information(self,'标题','内容',QMessageBox.Yes)
"""
QMessageBox提供了以下常用按钮:
QMessageBox.Ok
QMessageBox.Yes
QMessageBox.No
QMessageBox.Close
QMessageBox.Open
"""
与消息框交互
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.button=QPushButton('点我')
self.button.clicked.connect(self.change_text)
h_layout=QHBoxLayout()
h_layout.addWidget(self.button)
self.setLayout(h_layout)
def change_text(self):
choice=QMessageBox.question(self,'询问框','要改变文字吗?',
QMessageBox.Yes|QMessageBox.No)
if choice==QMessageBox.Yes:
self.button.setText('文字改变')
编写带有中文按钮的消息框
class QuestionMessageBox(QMessageBox): # 自定义消息框
def __init__(self,parent,title,content):
super(QuestionMessageBox,self).__init__(parent)
self.setWindowTitle(title)
self.setText(content)
self.setIcon(QMessageBox.Question)
"""
QMessageBox.NoIcon:错误
QMessageBox.Question:问号
QMessageBox.Information:信息
QMessageBox.Warning:警告
QMessageBox.Critical:错误
"""
self.addButton('是',QMessageBox.YesRole)
self.addButton('否',QMessageBox.NoRole)
"""
QMessageBox.AcceptRole:ok
QMessageBox.RejectRole:cancel
QMessageBox.YesRole:yes
QMessageBox.NoRole:no
"""
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.button=QPushButton('点我')
self.button.clicked.connect(self.change_text)
h_layout=QHBoxLayout()
h_layout.addWidget(self.button)
self.setLayout(h_layout)
def change_text(self):
msb_box=QuestionMessageBox(self,'标题','是否改变文本?')
msb_box.exec()
if msb_box.clickedButton().text()=='是':
self.button.setText('文字改变')