Eric6与pyqt5学习笔记 10【弹出窗口大全】

时间:2022-01-23 08:43:56


from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, pyqtSignal, QTimer, QThread # 后三个都是用于第二个窗口



class MyWindow(QWidget):

def __init__(self):

super().__init__()

self.setWindowTitle(‘PyQt5 弹出窗口(框)大全‘)
self.resize(400, 300)

# 全局布局(注意参数 self)
wl = QVBoxLayout(self)

# 局部布局
h1 = QHBoxLayout() # 输入框
h2 = QHBoxLayout() # 消息窗口
h3 = QHBoxLayout() # 文件(夹)打开,保存
h4 = QHBoxLayout() # 颜色、字体、自定义


btn1_1 = QPushButton(‘输入:整数‘)
btn1_2 = QPushButton(‘输入:小数‘)
btn1_3 = QPushButton(‘输入:文本‘)
btn1_4 = QPushButton(‘输入:多文‘)
btn1_5 = QPushButton(‘输入:选项‘)

btn2_1 = QPushButton(‘消息:信息‘)
btn2_2 = QPushButton(‘消息:问答‘)
btn2_3 = QPushButton(‘消息:警告‘)
btn2_4 = QPushButton(‘消息:危险‘)
btn2_5 = QPushButton(‘消息:关于‘)

btn3_1 = QPushButton(‘文件:文件夹‘)
btn3_2 = QPushButton(‘文件:单文件‘)
btn3_3 = QPushButton(‘文件:多文件‘)
btn3_4 = QPushButton(‘文件:保存‘)
btn3_5 = QPushButton(‘文件:另存为‘)

btn4_1 = QPushButton(‘颜色‘)
btn4_2 = QPushButton(‘字体‘)
btn4_3 = QPushButton(‘自定义‘)

for btn in (btn1_1, btn1_2, btn1_3, btn1_4, btn1_5):
h1.addWidget(btn)

for btn in (btn2_1, btn2_2, btn2_3, btn2_4, btn2_5):
h2.addWidget(btn)

for btn in (btn3_1, btn3_2, btn3_3, btn3_4, btn3_5):
h3.addWidget(btn)

for btn in (btn4_1, btn4_2, btn4_3):
h4.addWidget(btn)

btn1_1.clicked.connect(self.do_btn1_1) #(‘输入:整数‘)
btn1_2.clicked.connect(self.do_btn1_2) #(‘输入:小数‘)
btn1_3.clicked.connect(self.do_btn1_3) #(‘输入:文本‘)
btn1_4.clicked.connect(self.do_btn1_4) #(‘输入:多文‘)
btn1_5.clicked.connect(self.do_btn1_5) #(‘输入:选项‘)

btn2_1.clicked.connect(self.do_btn2_1) #(‘消息:信息‘)
btn2_2.clicked.connect(self.do_btn2_2) #(‘消息:问答‘)
btn2_3.clicked.connect(self.do_btn2_3) #(‘消息:警告‘)
btn2_4.clicked.connect(self.do_btn2_4) #(‘消息:危险‘)
btn2_5.clicked.connect(self.do_btn2_5) #(‘消息:关于‘)

btn3_1.clicked.connect(self.do_btn3_1) #(‘文件:文件夹‘)
btn3_2.clicked.connect(self.do_btn3_2) #(‘文件:单文件‘)
btn3_3.clicked.connect(self.do_btn3_3) #(‘文件:多文件‘)
btn3_4.clicked.connect(self.do_btn3_4) #(‘文件:保存‘)
btn3_5.clicked.connect(self.do_btn3_5) #(‘文件:另存为‘)

btn4_1.clicked.connect(self.do_btn4_1) #(‘颜色‘)
btn4_2.clicked.connect(self.do_btn4_2) #(‘字体‘)
btn4_3.clicked.connect(self.do_btn4_3) #(‘自定义‘)

# 加到全局布局
wl.addLayout(h1)
wl.addLayout(h2)
wl.addLayout(h3)
wl.addLayout(h4)

self.window2 = MyWindow2() # 自定义窗口
self.window2.have_closed_signal.connect(self.echo) # 接收自定义窗口关闭时发送过来的信号,交给 echo 函数显示


def echo(self, value):
‘‘‘显示对话框返回值‘‘‘
QMessageBox.information(self, "返回值", "得到:{}\n\ntype: {}".format(value, type(value)), QMessageBox.Yes | QMessageBox.No)
#pass

# =====================================================================
@Echo
def do_btn1_1(self, event): #(‘输入:整数‘)
#后面四个数字的作用依次是 初始值 最小值 最大值 步幅
value, ok = QInputDialog.getInt(self, "输入框标题", "这是提示信息\n\n请输入整数:", 37, -10000, 10000, 2)
#self.echo(value)

def do_btn1_2(self, event): #(‘输入:小数‘)
#后面四个数字的作用依次是 初始值 最小值 最大值 小数点后位数
value, ok = QInputDialog.getDouble(self, "输入框标题", "这是提示信息\n\n请输入整数:", 37.56, -10000, 10000, 2)
self.echo(value)

def do_btn1_3(self, event): #(‘输入:文本‘)
#第三个参数表示显示类型,可选,有正常(QLineEdit.Normal)、密碼( QLineEdit. Password)、不显示( QLineEdit. NoEcho)三种情况
value, ok = QInputDialog.getText(self, "输入框标题", "这是提示信息\n\n请输入文本:", QLineEdit.Normal, "这是默认值")
self.echo(value)

def do_btn1_4(self, event): #(‘输入:多文‘)
value, ok = QInputDialog.getMultiLineText(self, "输入框标题", "这是提示信息\n\n请输入地址:", "默认的\n我的地址是\n中国广东广州番禺")
self.echo(value)

def do_btn1_5(self, event): #(‘输入:选项‘)
#1为默认选中选项目,True/False 列表框是否可编辑。
items = ["Spring", "Summer", "Fall", "Winter"]
value, ok = QInputDialog.getItem(self, "输入框标题", "这是提示信息\n\n请选择季节:", items, 1, True)
self.echo(value)

# =====================================================================
def do_btn2_1(self, event): #(‘消息:信息‘)
reply = QMessageBox.information(self,
"消息框标题",
"这是一条消息。",
QMessageBox.Yes | QMessageBox.No)
self.echo(reply)

def do_btn2_2(self, event): #(‘消息:问答‘)
reply = QMessageBox.question(self,
"消息框标题",
"这是一条问答吗?",
QMessageBox.Yes | QMessageBox.No)
self.echo(reply)

def do_btn2_3(self, event): #(‘消息:警告‘)
reply = QMessageBox.warning(self,
"消息框标题",
"这是一条警告!",
QMessageBox.Yes | QMessageBox.No)
self.echo(reply)

def do_btn2_4(self, event): #(‘消息:危险‘)
reply = QMessageBox.ctitical(self,
"消息框标题",
"危险!程序即将强制退出!!!\n\n这个按钮再也点不开。",
QMessageBox.Yes | QMessageBox.No)
self.echo(reply)

def do_btn2_5(self, event): #(‘消息:关于‘)
reply = QMessageBox.about(self,
"消息框标题",
"这是关于软件的说明。。。",
QMessageBox.Yes | QMessageBox.No)
self.echo(reply)

# =====================================================================
def do_btn3_1(self, event): #(‘文件:文件夹‘)
dir = QFileDialog.getExistingDirectory(self,
"选取文件夹",
"C:/") # 起始路径
self.echo(dir)

def do_btn3_2(self, event): #(‘文件:单文件‘)
file_, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
"C:/",
"All Files (*);;Text Files (*.txt)") #设置文件扩展名过滤,注意用双分号间隔
self.echo(file_)

def do_btn3_3(self, event): #(‘文件:多文件‘)
files, ok = QFileDialog.getOpenFileNames(self,
"多文件选择",
"C:/",
"All Files (*);;Text Files (*.txt)")
self.echo(files)

def do_btn3_4(self, event): #(‘文件:保存‘)
file_, ok = QFileDialog.getSaveFileName(self,
"文件保存",
"C:/",
"All Files (*);;Text Files (*.txt)")
self.echo(file_)

def do_btn3_5(self, event): #(‘文件:另存为‘)
file_, ok = QFileDialog.getSaveFileName(self,
"文件另存为",
"C:/",
"All Files (*);;Text Files (*.txt)")
self.echo(file_)

# =====================================================================
def do_btn4_1(self, event): #(‘颜色‘)
color = QColorDialog.getColor(Qt.blue, self, "Select Color")
self.echo(color)

def do_btn4_2(self, event): #(‘字体‘)
font, ok = QFontDialog.getFont()
self.echo(font)

def do_btn4_3(self, event): #(‘自定义‘)

self.window2.show()


class MyWindow2(QWidget):
‘‘‘自定义窗口‘‘‘
# 知识点:
# 1.为了得到返回值用到了自定义的信号/槽
# 2.为了显示动态数字,使用了计时器
# 3.为了不让界面不失去响应,使用了线程

have_closed_signal = pyqtSignal(int) # 〇自定义信号(int类型)

def __init__(self):
super().__init__()
self.sec = 0
self.setWindowTitle(‘自定义窗口‘)
self.resize(200,150)

self.lcd = QLCDNumber(18, self)
btn1 = QPushButton(self, text="测试")
btn2 = QPushButton(self, text="关闭")

layout = QVBoxLayout(self)
layout.addWidget(self.lcd)
layout.addWidget(btn1)
layout.addWidget(btn2)

self.timer = QTimer()
self.workThread = WorkThread()

self.timer.timeout.connect(self.update) #每次计时结束,触发update
btn1.clicked.connect(self.startTimer)
btn2.clicked.connect(self.before_close) # ① 去到关闭前的处理

def before_close(self, event):
self.have_closed_signal.emit(self.lcd.value()) # 发送信号,带参数 888
self.close() # 然后窗口关闭

def update(self):
self.sec += 1
self.lcd.display(self.sec) #LED显示数字+1

def startTimer(self):
self.timer.start(1000) #计时器每秒计数
self.workThread.start() #计时开始
self.workThread.trigger.connect(self.stopTimer) #当获得循环完毕的信号时,停止计数

def stopTimer(self):
self.timer.stop()
self.sec = 0
#print("运行结束用时", self.lcd.value())

class WorkThread(QThread):
trigger = pyqtSignal()

def __int__(self):
super().__init__()

def run(self):
for i in range(203300030):
pass
self.trigger.emit() #循环完毕后发出信号

if __name__=="__main__":
import sys

app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())