Pyqt
基本程序模板
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 3 08:55:49 2021
@author: hug
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import threading
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('应用程序标题')
widget = QWidget(self)
self.layout = QHBoxLayout(self)
widget.setLayout(self.layout)
toolbar = QToolBar("My main toolbar")
self.addToolBar(toolbar)
self.setStatusBar(QStatusBar(self))
button_action = QAction("动作1", self)
toolbar.addAction(button_action)
button_action.setStatusTip("动作1提示信息")
button_action.triggered.connect(self.something)
self.col = QColorDialog.getColor()
print(f'你选择了{self.col.name()}颜色')
widgets = [QCheckBox,
QComboBox,
QDateEdit,
QDateTimeEdit,
QDial,
QDoubleSpinBox,
QFontComboBox,
QLCDNumber,
QLabel,
QLineEdit,
QTextEdit,
QProgressBar,
QPushButton,
QRadioButton,
QSlider,
QSpinBox,
QTimeEdit]
for w in widgets:
self.layout.addWidget(w())
self.setCentralWidget(widget)
def something(self):
print('你按下了动作1')
def closeEvent(self, event):
reply = QMessageBox.question(self, '警告', '确认关闭?',
QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.Yes:
self.exit_flag = True
event.accept()
# 清理释放工作
else:
event.ignore()
app = QApplication(sys.argv)
win = MainWindow()
win.showMaximized()
win.show()
sys.exit(app.exec_())
各组件及其功能