功能介绍
使用配置文件可以自定义按钮个数、按钮颜色、按钮按下后发出的http请求以及对http回复的校验,用于模拟电气柜上的按钮,便于调试
界面
默认打开界面

收到请求结果

查看请求和回复详情

代码
import sys
import json
import requests
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QTextEdit, QLabel,
QScrollArea, QGridLayout, QMessageBox, QSizePolicy,
QFrame, QToolButton, QSplitter, QProgressBar)
from PyQt5.QtCore import Qt, QSize, QPropertyAnimation, QEasingCurve, QThread, pyqtSignal
from PyQt5.QtGui import QFont, QColor, QPalette, QIcon, QCursor
class RequestThread(QThread):
"""处理HTTP请求的线程类"""
finished = pyqtSignal(object, object) # 信号:请求完成,参数为(response, config)
error = pyqtSignal(str) # 信号:请求出错,参数为错误信息
def __init__(self, config):
super().__init__()
self.config = config
def run(self):
"""执行HTTP请求"""
try:
method = self.config.get('method', 'GET').upper()
url = self.config.get('url', '')
headers = self.config.get('headers', {
})
body = self.config.get('body', None)
if method == 'GET':
response = requests.get(url, headers=headers, timeout=10)
elif method == 'POST':
response = requests.post(url, json=body, headers=headers, timeout=10)
elif method == 'PUT':
response = requests.put(url, json=body, headers=headers, timeout=10)
elif method == 'DELETE':
response = requests.delete(url, headers=headers, timeout=10)
else:
raise ValueError(f"不支持的HTTP方法: {
method}")
self.finished.emit(response, self.config)
except Exception as e:
self.error.emit(str(e))
class CircularButtonWidget(QWidget):
"""圆形按钮控件,包含按钮和描述标签"""
clicked = pyqtSignal(dict) # 信号:按钮点击,参数为请求配置
def __init__(self, config, parent=None):
super().__init__(parent)
self.config = config
# 创建布局
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignCenter)
layout.setSpacing(5)
# 创建圆形按钮
button_text = config.get("name", "请求")
if len(button_text) > 8:
button_text = button_text[:5] + "..."
button_color = config.get("color", "#4CAF50")
# self.button = QPushButton(button_text)
self.button = QPushButton()
self.button.setFixedSize(160, 160)
self.button.setStyleSheet(f"""
QPushButton {
{
border-radius: 80px;
background-color: {
button_color};
color: white;
font-weight: bold;
border: 2px solid {
self.darken_color(button_color)};
font-size: 12px;
}}
QPushButton:hover {
{
background-color: {
self.lighten_color(button_color)};
}}
QPushButton:pressed {
{
background-color: {
self.darken_color(button_color)};
}}
QPushButton:disabled {
{
background-color: #cccccc;
border: 2px solid #999999;
}}
""")
self.button.clicked.connect(self.on_button_clicked)
# 创建描述标签
self.description = QLabel(config.get("name", "请求"))
self.description.setAlignment(Qt.AlignCenter)
self.description.setWordWrap(True)
self.description.setMaximumWidth(600)
self.description.setStyleSheet("font-size:35px;")
# 添加到布局
layout.addWidget(self.button)
layout.addWidget(self.description)
def darken_color(self, color):
"""使颜色变深"""
try:
c = QColor(color)
c = c.darker(120)
return c.name()
except:
return "#2E7D32"
def lighten_color(self, color):
"""使颜色变亮"""
try:
c = QColor(color)

最低0.47元/天 解锁文章
7万+

被折叠的 条评论
为什么被折叠?



