python的前端框架_使用python前端框架pyqt5实现小工具的GUI界面

编写目的pyqt5部分组件简单的讲解

自身巩固知识

为测试平台搭建做技能的储备工作术语定义

术语

说明

PyQt5

是一套Python绑定Digia QT5应用的框架,可用于Python 2和3。

…..

…..PyQt5QtWidgetsQApplication:定义application

QWidget:定义窗口

setObjectName:定义窗口名

setStyleSheet:定义应用窗口的背景图

resize:窗口大小

move:窗口打开的位置

setWindowTitle:窗口标题

setWindowIcon:窗口icon

QLabel:label(setGeometry-定义位置及大小,setText-定义文本,setObjectName-定义名字)

QLineEdit:单行输入框

QTextEdit:text类型输入框

QTextBrowser:日志输出框

QPushButton:按钮

Show:展示应用QtCore:大小处理

QtGui:图片处理

pyinstaller

pyinstaller是一个非常简单的打包python的py文件的库。

Demo: pyinstaller -F -w -i D:\tmp\main.ico D:\python_test.py

详解:

(这些暂时够用)

-F 表示生成单个可执行文件,执行后dist目录中出现了python_test.exe文件,没有任何依赖库,执行它即可。

-w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!

-i 表示可执行文件的图标

实战

写了一个简单的接口签名转换的工具,提供了简单的前端页面,效果如下:

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAyMTU0Mw==,size_16,color_FFFFFF,t_70

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAyMTU0Mw==,size_16,color_FFFFFF,t_70

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAyMTU0Mw==,size_16,color_FFFFFF,t_70

前端代码:

main.py

from PyQt5 import QtCore, QtGui

from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit, QTextEdit, QTextBrowser, QPushButton, \

QApplication, QHBoxLayout, QMenuBar, QAction, QStackedWidget, QWidget, QVBoxLayout, QStackedLayout, QDialog

import sys

import common

import json

from project import Dialog1, Dialog2

class TesterTools(QMainWindow):

def __init__(self, parent=None):

super(TesterTools, self).__init__(parent)

# 窗口

self.setObjectName("MainWindow")

self.setStyleSheet("#MainWindow{border-image:url(./picture/background.jpg);}")

# self.resize(3000, 3000)

# self.move(0, 0)

self.resize(500, 500)

self.move(600, 200)

self.setWindowTitle('TesterTools')

self.setWindowIcon(QtGui.QIcon('./picture/index.png'))

# 创建主界面,layout布局

self.centralwidget = QWidget(self)

self.setCentralWidget(self.centralwidget)

self.Layout = QVBoxLayout(self.centralwidget)

# 菜单

self.home_menu = self.menuBar()

self.file = self.home_menu.addMenu("Menu")

self.create_sign = QAction("Create sign", self)

self.file.addAction(self.create_sign)

self.create_sign.triggered.connect(self.on_pushAction1_clicked)

self.high_concurrency = QAction("High concurrency", self)

# self.save.setShortcut("Ctrl+S") # 设置快捷键

self.file.addAction(self.high_concurrency)

self.high_concurrency.triggered.connect(self.on_pushAction2_clicked)

def on_pushAction1_clicked(self):

self.hide()

#第一个面板

form1 = QDialog()

ui = Dialog1.Ui_Dialog1()

ui.setupUi(form1)

form1.show()

form1.exec_()

self.show()

def on_pushAction2_clicked(self):

self.hide()

#第二个面板

form2 = QDialog()

ui = Dialog2.Ui_Dialog2()

ui.setupUi(form2)

form2.show()

form2.exec_()

self.show()

if __name__ == "__main__":

app = QApplication(sys.argv)

windows = TesterTools()

windows.show()

# CreateSign = CreateSign()

sys.exit(app.exec_())

./project/Dialog1.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Dialog1.ui'

#

# Created by: PyQt5 UI code generator 5.10.1

#

# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.QtWidgets import QLabel, QLineEdit, QTextEdit, QTextBrowser, QPushButton

from project import create_sign

import common

class Ui_Dialog1(object):

def setupUi(self, Dialog1):

Dialog1.setObjectName("Dialog1")

Dialog1.resize(500, 500)

Dialog1.move(600, 200)

# Dialog1.setStyleSheet("#MainWindow{border-image:url(E:\\mason_project\\createSignTools\\picture\\background.jpg);}")

Dialog1.setAutoFillBackground(True)

pal = QtGui.QPalette()

pal.setColor(QtGui.QPalette.Background, QtGui.QColor(245,245,220))

Dialog1.setPalette(pal)

# create_sign.page(Dialog1)

Dialog1.label_secret_key = QLabel(Dialog1)

Dialog1.label_secret_key.setGeometry(QtCore.QRect(50, 50, 70, 20))

Dialog1.label_secret_key.setText('secretKey:')

Dialog1.label_secret_key.setObjectName('label_secret_key')

# textbox

Dialog1.textbox_secret_key = QLineEdit(Dialog1)

Dialog1.textbox_secret_key.resize(250, 20)

Dialog1.textbox_secret_key.move(120, 50)

Dialog1.textbox_secret_key.setStyleSheet("background-color: #F0F8FF;")

# params

# label

Dialog1.label_params = QLabel(Dialog1)

Dialog1.label_params.setGeometry(QtCore.QRect(50, 80, 70, 20))

Dialog1.label_params.setText('params:')

Dialog1.label_params.setObjectName('label_params')

# textbox

Dialog1.textbox_params = QTextEdit(Dialog1)

Dialog1.textbox_params.resize(250, 20 * 4)

# Dialog1.textbox_params.resize(250, 20*4)

Dialog1.textbox_params.move(120, 80)

Dialog1.textbox_params.setAlignment(QtCore.Qt.AlignTop)

Dialog1.textbox_params.setStyleSheet("background-color: #F0F8FF;")

# 注释label

Dialog1.label_note = QLabel(Dialog1)

Dialog1.label_note.setGeometry(QtCore.QRect(120, 225, 300, 40))

Dialog1.label_note.setText('demo\n params: id=74f0e1ab&request_time=1593589580000')

Dialog1.label_note.setObjectName('label_note')

# output Text

# label

Dialog1.label_output = QLabel(Dialog1)

Dialog1.label_output.setGeometry(QtCore.QRect(50, 280, 50, 20))

Dialog1.label_output.setText('sign:')

Dialog1.label_output.setObjectName('label_output')

# textbox

Dialog1.text_output = QTextBrowser(Dialog1)

Dialog1.text_output.move(120, 280)

Dialog1.text_output.resize(250, 100)

Dialog1.text_output.setStyleSheet("background-color: #B0C4DE;")

Dialog1.btn = QPushButton('create', Dialog1)

Dialog1.btn.setGeometry(200, 400, 100, 50)

Dialog1.btn.setStyleSheet('''

QPushButton

{text-align : center;

background-color : white;

font: bold;

border-color: gray;

border-width: 2px;

border-radius: 10px;

padding: 6px;

height : 14px;

border-style: outset;

font : 14px;}

QPushButton:pressed

{text-align : center;

background-color : light gray;

font: bold;

border-color: gray;

border-width: 2px;

border-radius: 10px;

padding: 6px;

height : 14px;

border-style: outset;

font : 14px;}

''')

Dialog1.btn.clicked.connect(lambda: self.click_btn(Dialog1))

self.retranslateUi(Dialog1)

QtCore.QMetaObject.connectSlotsByName(Dialog1)

def retranslateUi(self, Dialog1):

_translate = QtCore.QCoreApplication.translate

Dialog1.setWindowTitle(_translate("Dialog1", "Dialog"))

def click_btn(self, Dialog1):

if len(Dialog1.textbox_params.toPlainText()) == 0:

Dialog1.text_output.setText("Input error!")

elif len(Dialog1.textbox_secret_key.text()) == 0:

Dialog1.text_output.setText("Input error!")

elif "=" not in Dialog1.textbox_params.toPlainText():

Dialog1.text_output.setText("Input error!")

else:

url_params = Dialog1.textbox_params.toPlainText().split('&')

body = {}

for kv in url_params:

body[kv.split('=')[0]] = kv.split('=')[1]

sign = common.sign_create(body, Dialog1.textbox_secret_key.text())

Dialog1.text_output.setText(sign)

原文链接:https://blog.csdn.net/weixin_42021543/article/details/107428722

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用Python3+PyQT5+Pyserial实现简单的串口工具方法的示例代码: ``` python import sys import serial from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog from PyQt5.QtCore import QThread, pyqtSignal class SerialThread(QThread): received = pyqtSignal(str) def __init__(self, port, baudrate): super().__init__() self.port = port self.baudrate = baudrate self.serial = None def run(self): try: self.serial = serial.Serial(self.port, self.baudrate) while True: data = self.serial.readline().decode("utf-8") if data: self.received.emit(data) except Exception as e: print(e) def write(self, data): if self.serial: self.serial.write(data.encode("utf-8")) def close(self): if self.serial: self.serial.close() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Serial Tool") self.setGeometry(100, 100, 800, 600) self.text_edit = QTextEdit(self) self.text_edit.setReadOnly(True) self.setCentralWidget(self.text_edit) open_action = QAction("Open", self) open_action.setShortcut("Ctrl+O") open_action.triggered.connect(self.open_file) self.menuBar().addAction(open_action) self.thread = SerialThread("COM1", 115200) self.thread.received.connect(self.append_text) self.thread.start() def append_text(self, text): self.text_edit.moveCursor(QtGui.QTextCursor.End) self.text_edit.insertPlainText(text) def open_file(self): file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files (*.txt)") if file_name: with open(file_name, "r") as f: data = f.read() self.thread.write(data) def closeEvent(self, event): self.thread.close() event.accept() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 这个示例代码实现了一个简单的串口工具,可以实现打开串口、读取串口数据、发送数据等功能。其SerialThread类用于在后台线程读取串口数据,MainWindow类用于创建窗口并处理窗口事件。该代码可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值