Multi-signal to the same slot
pressed和released信号
def initUI(self):
......
# 连接信号和槽
self.button.pressed.connect(self.change_label)
self.button.released.connect(self.change_label)
......
OUTPUT :
Pressed but not released
Released
A Signal to A Signal
def initUI(self):
......
# 连接信号和槽
self.button.pressed.connect(self.button.released)
self.button.released.connect(self.change_label)
......
这里的效果图同上,但是当点击不放的时候,它是通过发射released信号调用change_label函数的,和上边调用该函数的过程是有差别的(上边的是通过点击信号直接调用)
One signal to Multi-slots
......
self.button.clicked.connect(self.change_label)
self.button.clicked.connect(self.change_label)
......
OUTPUT :
点击一次的结果,直接加了两次15
A Signal to slots with parameters
......
self.button.clicked.connect(lambda: self.change_label("9999"))
# 这里需要匿名函数把函数转为函数对象,否则会出现以下错误:
# TypeError: argument 1 has unexpected type 'NoneType'
......
def change_label(self, text):
# 改变文本控件的内容
self.label.setText(text)
OUTPUT :
点击后就是9999.
+++
QMessageBox & Interaction
这个类提供一个对话框(视窗),增强软件的交互能力,减少一些失误的出现
对话框种类有多种,例如:提示、警告、错误、询问、关于等,这些也只是显示的图标不同。
# 一些常用的方法:
QMessageBox.information(parent, title, text, buttons, defaultButton)
QMessageBox.setTitle() # 设置消息标题
QMessageBox.setText() # 设置消息内容
QMessageBox.setIcon() # 设置弹出对话框图片
"""
这里的information都可以换成其他方法,但是about方法没有第三第四参数
parent 是指定这个视窗属于那个父窗口控件
title 是对话框的标题
text 是对话框的文本内容
buttons 可以写入多个按钮参数 利用符号 | 隔开
按钮有许多种:
QMessage.Ok | QMessage.Yes | QMessage.No
| QMessage.Cancel | QMessage.Abort | QMessage.Retry | QMessage.Ignore
defaultButton 默认按键设置
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QMessageBox
......
def initUI(self):
self.resize(500, 200)
self.label = QLabel("主视窗", self)
self.button = QPushButton("点击弹出对话框", self)
# 实例化一个QVBoxLayout对象(在后边第七节内容会记录到)
self.v_lay = QVBoxLayout()
self.v_lay.addWidget(self.label)
self.v_lay.addWidget(self.button)
# 连接信号和槽
self.button.clicked.connect(self.change_label)
# 调用窗口的setLayout方法将总布局设置为窗口的整体布局
self.setLayout(self.v_lay)
self.show()
def change_label(self):
QMessageBox.information(self, "Title", '消息框正文', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
QMessageBox.question(self, "Title", "提问框正文", QMessageBox.No | QMessageBox.Yes, QMessageBox.Yes)
QMessageBox.warning(self, "Title", "警告框正文", QMessageBox.Cancel | QMessageBox.No, QMessageBox.Cancel)
QMessageBox.critical(self, "Title", "严重错误框正文", QMessageBox.Abort | QMessageBox.Ignore, QMessageBox.Ignore)
QMessageBox.about(self, "Title", '正文正文正文正文正文正文')
......
OUTPUT:
"""
实现交互,就要让计算机知道点击了什么,那就要使用connect()
里边要传入点击后执行的函数,函数功能得分类,分类就依据点击的Button是哪个?
所以函数参数就要接收按键func(self, button)
这里就需要把connect(lambda: func(self.button))的func函数封装成函数类型
而func里边要有按钮的分类,利用 if button == self.button: 进行处理
再执行想要的程序。
以下代码图片来自博主:Giyn (https://blog.csdn.net/weixin_45961774)
"""
# -*- coding: utf-8 -*-
"""
Created on Sat May 9 14:33:28 2020
@author: Giyn
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QMessageBox
class Simple_Window(QWidget):
def __init__(self):
super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
self.setWindowTitle("QMessageBox")
self.resize(400, 200)
self.button1 = QPushButton("Question", self) # self是指定的父类Simple_Window,表示QLabel属于Simple_Window窗口
self.button2 = QPushButton("Information", self)
# 连接信号和槽
self.button1.clicked.connect(lambda: self.show_msg_box(self.button1)) # 槽函数带有参数,使用lambda表达式封装函数
self.button2.clicked.connect(lambda: self.show_msg_box(self.button2))
self.v_layout = QVBoxLayout() # 实例化一个QVBoxLayout对象
self.v_layout.addWidget(self.button1)
self.v_layout.addWidget(self.button2)
self.setLayout(self.v_layout) # 调用窗口的setLayout方法将总布局设置为窗口的整体布局
def show_msg_box(self, button):
if button == self.button1:
choice = QMessageBox.question(self, "Question", "Do you want to save it?", QMessageBox.Yes | QMessageBox.No)
if choice == QMessageBox.Yes:
print("Saved!")
self.close()
elif choice == QMessageBox.No:
self.close()
elif button == self.button2:
QMessageBox.information(self, "Information", "I am an information.", QMessageBox.Ok)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Simple_Window()
window.show()
sys.exit(app.exec())