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)
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)
def echo(self, value):
‘‘‘显示对话框返回值‘‘‘
QMessageBox.information(self, "返回值", "得到:{}\n\ntype: {}".format(value, type(value)), QMessageBox.Yes | QMessageBox.No)
@Echo
def do_btn1_1(self, event):
value, ok = QInputDialog.getInt(self, "输入框标题", "这是提示信息\n\n请输入整数:", 37, -10000, 10000, 2)
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):
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):
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):
‘‘‘自定义窗口‘‘‘
have_closed_signal = pyqtSignal(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)
btn1.clicked.connect(self.startTimer)
btn2.clicked.connect(self.before_close)
def before_close(self, event):
self.have_closed_signal.emit(self.lcd.value())
self.close()
def update(self):
self.sec += 1
self.lcd.display(self.sec)
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
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_())