Last active
March 26, 2020 11:04
-
-
Save sailist/d13edc66589f08e53caf6cb00ba07283 to your computer and use it in GitHub Desktop.
QT快速模板
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def show_feature_imgage(self, fname, label): | |
image = QImage(fname) | |
label.setPixmap(QPixmap.fromImage(image)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtCore import * | |
from PyQt5.QtGui import QPixmap, QImage | |
from PyQt5.QtWidgets import * | |
fengzhizhibiao_val = QLineEdit() | |
fengzhizhibiao_val.textChanged.connect(set_val("fengzhizhibiao", fengzhizhibiao_val)) | |
read_data_bt = QPushButton("读取数据") | |
read_data_bt.clicked.connect(read_file_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtWidgets import * | |
from PyQt5.QtCore import * | |
class FirstWindow(QWidget): | |
close_signal = pyqtSignal() | |
def __init__(self, parent=None): | |
# super这个用法是调用父类的构造函数 | |
# parent=None表示默认没有父Widget,如果指定父亲Widget,则调用之 | |
super(FirstWindow, self).__init__(parent) | |
self.resize(100, 100) | |
self.btn = QToolButton(self) | |
self.btn.setText("click") | |
def closeEvent(self, event): | |
self.close_signal.emit() | |
self.close() | |
class SecondWindow(QWidget): | |
def __init__(self, parent=None): | |
super(SecondWindow, self).__init__(parent) | |
self.setLayout(self.functionA()) | |
def functionA(self): | |
right = QVBoxLayout() | |
right_grid = QGridLayout() | |
i = 0 | |
fengzhi_val_label = QLabel("峰 值:") | |
fengzhi_val = QLineEdit() | |
right_grid.addWidget(fengzhi_val_label, i, 0) | |
right_grid.addWidget(fengzhi_val, i, 1) | |
i += 1 | |
junfanggenzhi_val_label = QLabel("均方根值:") | |
junfanggenzhi_val = QLineEdit() | |
right_grid.addWidget(junfanggenzhi_val_label, i, 0) | |
right_grid.addWidget(junfanggenzhi_val, i, 1) | |
i += 1 | |
qiaodu_val_label = QLabel("峭 度:") | |
qiaodu_val = QLineEdit() | |
right_grid.addWidget(qiaodu_val_label, i, 0) | |
right_grid.addWidget(qiaodu_val, i, 1) | |
i += 1 | |
boxingzhibiao_val_label = QLabel("波形指标:") | |
boxingzhibiao_val = QLineEdit() | |
right_grid.addWidget(boxingzhibiao_val_label, i, 0) | |
right_grid.addWidget(boxingzhibiao_val, i, 1) | |
i += 1 | |
fengzhizhibiao_val_label = QLabel("峰值指标:") | |
fengzhizhibiao_val = QLineEdit() | |
right_grid.addWidget(fengzhizhibiao_val_label, i, 0) | |
right_grid.addWidget(fengzhizhibiao_val, i, 1) | |
i += 1 | |
maichongzhibiao_val_label = QLabel("脉冲指标:") | |
maichongzhibiao_val = QLineEdit() | |
right_grid.addWidget(maichongzhibiao_val_label, i, 0) | |
right_grid.addWidget(maichongzhibiao_val, i, 1) | |
i += 1 | |
qiaoduzhibiao_val_label = QLabel("峭度指标:") | |
qiaoduzhibiao_val = QLineEdit() | |
right_grid.addWidget(qiaoduzhibiao_val_label, i, 0) | |
right_grid.addWidget(qiaoduzhibiao_val, i, 1) | |
i += 1 | |
right.addLayout(right_grid) | |
h = QHBoxLayout() | |
h.addLayout(right) | |
return h | |
def handle_click(self): | |
if not self.isVisible(): | |
self.setWindowModality(Qt.ApplicationModal) | |
self.show() | |
def handle_close(self): | |
self.close() | |
if __name__ == "__main__": | |
App = QApplication(sys.argv) | |
ex = FirstWindow() | |
s = SecondWindow() | |
ex.btn.clicked.connect(s.handle_click) | |
# ex.btn.clicked.connect(ex.hide) | |
ex.close_signal.connect(ex.close) | |
ex.show() | |
sys.exit(App.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
left_down = QHBoxLayout() | |
left_down.addWidget(dataflabel) | |
left_down.addWidget(edit) | |
left_down.addWidget(read_data_bt) | |
left.addLayout(left_down) | |
# 在MainWindow中加载界面需要套一层Widget | |
w = QWidget() | |
w.setLayout(h) | |
self.main_ = w | |
self.setCentralWidget(w) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 更多参考 http://qian.sh/paper/pyqt5/%E6%8E%A7%E4%BB%B61.html | |
import sys | |
from PyQt5.QtWidgets import (QMainWindow, QTextEdit, | |
QAction, QFileDialog, QApplication) | |
from PyQt5.QtGui import QIcon | |
class Example(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
self.initUI() | |
def initUI(self): | |
self.textEdit = QTextEdit() | |
self.setCentralWidget(self.textEdit) | |
self.statusBar() | |
openFile = QAction(QIcon('open.png'), 'Open', self) | |
openFile.setShortcut('Ctrl+O') | |
openFile.setStatusTip('Open new File') | |
openFile.triggered.connect(self.showDialog) | |
menubar = self.menuBar() | |
fileMenu = menubar.addMenu('&File') | |
fileMenu.addAction(openFile) | |
self.setGeometry(300, 300, 350, 300) | |
self.setWindowTitle('File dialog') | |
self.show() | |
def showDialog(self): | |
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home') | |
if fname[0]: | |
f = open(fname[0], 'r') | |
with f: | |
data = f.read() | |
self.textEdit.setText(data) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
ex = Example() | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setStyleSheet("background-image: url(black.gif); color: #FFFFFF"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtGui import * | |
from PyQt5.QtWidgets import QApplication, QWidget | |
import sys | |
app = QApplication(sys.argv) | |
screen = QApplication.primaryScreen() | |
widget = QWidget() | |
screenshot = screen.grabWindow(0, 0, 0, 100, 100) | |
screenshot.save('shot', 'jpg') | |
# 如果要获取自身进程的: | |
from PyQt5 import QtWidgets | |
import sys | |
app = QtWidgets.QApplication(sys.argv) | |
w = QtWidgets.QWidget() | |
grab_btn=QtWidgets.QPushButton('Grab Screen') | |
def click_handler(): | |
screen = QtWidgets.QApplication.primaryScreen() | |
screenshot = screen.grabWindow( w.winId() ) | |
screenshot.save('shot.png', 'png') | |
w.close() | |
grab_btn.clicked.connect(click_handler) | |
layout = QtWidgets.QVBoxLayout() | |
layout.addWidget(grab_btn) | |
w.setLayout(layout) | |
w.show() | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication | |
from PyQt5.QtGui import QIcon | |
class Example(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
self.initUI() | |
def initUI(self): | |
exitAct = QAction(QIcon('exit.png'), '&Exit', self) | |
exitAct.setShortcut('Ctrl+Q') | |
exitAct.setStatusTip('Exit application') | |
exitAct.triggered.connect(qApp.quit) | |
self.statusBar() | |
menubar = self.menuBar() | |
fileMenu = menubar.addMenu('&File') | |
fileMenu.addAction(exitAct) | |
self.setGeometry(300, 300, 300, 200) | |
self.setWindowTitle('Simple menu') | |
self.show() | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
ex = Example() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment