编写生成直方图代码的Qt脚本

This commit is contained in:
weiye.wang 2024-03-10 22:37:17 +08:00
parent 7b32e1ba4f
commit e595ea3276
3 changed files with 435 additions and 0 deletions

View File

@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file '生成直方图代码.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QLabel, QLineEdit,
QPlainTextEdit, QPushButton, QSizePolicy, QTextBrowser,
QVBoxLayout, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(400, 381)
self.pushButton_generatexlist = QPushButton(Form)
self.pushButton_generatexlist.setObjectName(u"pushButton_generatexlist")
self.pushButton_generatexlist.setGeometry(QRect(10, 130, 121, 24))
self.textBrowser = QTextBrowser(Form)
self.textBrowser.setObjectName(u"textBrowser")
self.textBrowser.setEnabled(False)
self.textBrowser.setGeometry(QRect(10, 160, 121, 211))
self.label_6 = QLabel(Form)
self.label_6.setObjectName(u"label_6")
self.label_6.setGeometry(QRect(140, 10, 121, 16))
self.plainTextEdit_yinfo = QPlainTextEdit(Form)
self.plainTextEdit_yinfo.setObjectName(u"plainTextEdit_yinfo")
self.plainTextEdit_yinfo.setGeometry(QRect(140, 30, 171, 181))
self.pushButton_exec = QPushButton(Form)
self.pushButton_exec.setObjectName(u"pushButton_exec")
self.pushButton_exec.setGeometry(QRect(320, 30, 71, 181))
font = QFont()
font.setBold(True)
self.pushButton_exec.setFont(font)
self.label_7 = QLabel(Form)
self.label_7.setObjectName(u"label_7")
self.label_7.setGeometry(QRect(140, 220, 71, 16))
self.plainTextEdit_latexcode = QPlainTextEdit(Form)
self.plainTextEdit_latexcode.setObjectName(u"plainTextEdit_latexcode")
self.plainTextEdit_latexcode.setGeometry(QRect(140, 240, 251, 131))
self.widget = QWidget(Form)
self.widget.setObjectName(u"widget")
self.widget.setGeometry(QRect(10, 10, 121, 108))
self.verticalLayout = QVBoxLayout(self.widget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.label = QLabel(self.widget)
self.label.setObjectName(u"label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit_xmin = QLineEdit(self.widget)
self.lineEdit_xmin.setObjectName(u"lineEdit_xmin")
self.horizontalLayout.addWidget(self.lineEdit_xmin)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QHBoxLayout()
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.label_2 = QLabel(self.widget)
self.label_2.setObjectName(u"label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_xmax = QLineEdit(self.widget)
self.lineEdit_xmax.setObjectName(u"lineEdit_xmax")
self.horizontalLayout_2.addWidget(self.lineEdit_xmax)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_5 = QHBoxLayout()
self.horizontalLayout_5.setObjectName(u"horizontalLayout_5")
self.label_5 = QLabel(self.widget)
self.label_5.setObjectName(u"label_5")
self.horizontalLayout_5.addWidget(self.label_5)
self.lineEdit_xwidth = QLineEdit(self.widget)
self.lineEdit_xwidth.setObjectName(u"lineEdit_xwidth")
self.horizontalLayout_5.addWidget(self.lineEdit_xwidth)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.horizontalLayout_4 = QHBoxLayout()
self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
self.label_4 = QLabel(self.widget)
self.label_4.setObjectName(u"label_4")
self.horizontalLayout_4.addWidget(self.label_4)
self.lineEdit_xlabel = QLineEdit(self.widget)
self.lineEdit_xlabel.setObjectName(u"lineEdit_xlabel")
self.horizontalLayout_4.addWidget(self.lineEdit_xlabel)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u751f\u6210\u76f4\u65b9\u56fe\u4ee3\u7801", None))
self.pushButton_generatexlist.setText(QCoreApplication.translate("Form", u"\u751f\u6210\u6a2a\u5750\u6807\u5e8f\u5217", None))
self.textBrowser.setHtml(QCoreApplication.translate("Form", u"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"hr { height: 1px; border-width: 0; }\n"
"li.unchecked::marker { content: \"\\2610\"; }\n"
"li.checked::marker { content: \"\\2612\"; }\n"
"</style></head><body style=\" font-family:'Microsoft YaHei UI'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">\u4e0d\u6539\u53d8\u6a2a\u5750\u6807\u7684\u503c, \u5728\u9017\u53f7\u540e\u8f93\u5165\u7eb5\u5750\u6807\u7684\u503c</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">\u5982\u679c\u6807\u7b7e\u548c\u7eb5\u5750\u6807\u7684\u503c\u4e0d\u540c, \u5728\u540e\u9762\u7684\u65b9\u62ec\u53f7\u5185\u8f93\u5165\u6807\u7b7e</"
"p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">\u5982:</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">(10,0.02)[]</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">(20,0.04)[a]</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">(30,0.04][]</p>\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>", None))
self.label_6.setText(QCoreApplication.translate("Form", u"\u8f93\u5165\u7eb5\u5750\u6807\u4fe1\u606f", None))
self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u751f\u6210\u4ee3\u7801", None))
self.label_7.setText(QCoreApplication.translate("Form", u"\u76f4\u65b9\u56fe\u4ee3\u7801", None))
self.label.setText(QCoreApplication.translate("Form", u"x\u6700\u5c0f\u503c", None))
self.label_2.setText(QCoreApplication.translate("Form", u"x\u6700\u5927\u503c", None))
self.label_5.setText(QCoreApplication.translate("Form", u"x\u5bbd\u5ea6", None))
self.label_4.setText(QCoreApplication.translate("Form", u"x\u8f74label", None))
# retranslateUi

View File

@ -0,0 +1,92 @@
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_生成直方图代码 import Ui_Form
import re
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
self.pushButton_generatexlist.clicked.connect(self.generateXList)
self.pushButton_exec.clicked.connect(self.exec)
def generateXList(self):
self.xmin = int(self.lineEdit_xmin.text())
self.xmax = int(self.lineEdit_xmax.text())
self.xwidth = int(self.lineEdit_xwidth.text())
self.xlist = list(range(self.xmin,self.xmax,self.xwidth))
output = ""
for x in self.xlist:
output += f"({x},)[]\n"
self.plainTextEdit_yinfo.setPlainText(output)
def exec(self):
xlabel = self.lineEdit_xlabel.text()
coordinateinfo = self.plainTextEdit_yinfo.toPlainText()
if self.xmin < self.xwidth: #计算x轴的起点坐标
self.xstart = 0
else:
self.xstart = self.xmin - self.xwidth
self.xmax = self.xlist[-1] + self.xwidth
self.xend = self.xmax + self.xwidth #计算x轴的终点坐标
xscale = 5/(self.xend-self.xstart) #计算x放缩比例
# print(xscale)
self.coordinatelist = re.findall(r"\(([\d\.]+),([\d\.]+)\)\[([\S]*)\]",coordinateinfo) #计算坐标信息
for cindex in range(len(self.coordinatelist)):
x,y,label = self.coordinatelist[cindex]
if label.strip() == "":
self.coordinatelist[cindex] = (x,y,y)
self.coordinatestr_raw = []
for coord in self.coordinatelist:
x,y,label = coord
self.coordinatestr_raw.append(f"{x}/{y}")
self.coordinatestr = ",".join(self.coordinatestr_raw)
# print(self.coordinatelist)
self.dasheddict = {} #计算以纵坐标为key的虚线信息, 内容为(x,label)
for coord in self.coordinatelist:
x,y,label = coord
if not y in self.dasheddict:
self.dasheddict[y] = (x,label)
else:
if x > self.dasheddict[y][0] and label == y:
self.dasheddict[y] = (x,self.dasheddict[y][1])
elif x > self.dasheddict[y][0] and label != y:
self.dasheddict[y] = (x,label)
self.dashedstr_raw = []
for y in self.dasheddict:
x,label = self.dasheddict[y]
self.dashedstr_raw.append(f"{x}/{y}/{label}")
self.dashedstr = ",".join(self.dashedstr_raw)
# print(self.dasheddict)
ymax = max([float(i) for i in self.dasheddict.keys()])
yscale = 3/ymax #3是y的长度, 计算y放缩比例
self.yend = ymax + 1/float(yscale)
latexcode = f"""\\begin{{center}}
\\begin{{tikzpicture}}[>=latex, xscale = {xscale:.2f}, yscale = {yscale:.2f}]
\\draw [->] ({self.xstart},0) -- ({self.xend},0) node [below right] {{{xlabel}}};
\\draw [->] ({self.xstart},0) -- ({self.xstart},{self.yend:.2f}) node [left] {{$\\dfrac{{\\text{{频率}}}}{{\\text{{组距}}}}$}};
\\draw ({self.xstart},0) node [below] {{${self.xstart}$}};
\\foreach \\i/\\j in {{{self.coordinatestr}}}
{{\\draw (\\i,0) node [below] {{$\\i$}} --++ (0,\\j) --++ ({self.xwidth},0) --++ (0,-\\j);}};
\\draw ({self.xmax},0) node [below] {{${self.xmax}$}};
\\foreach \i/\j/\k in {{{self.dashedstr}}}
{{\\draw [dashed] (\\i,\\j) -- ({self.xstart},\\j) node [left] {{$\\k$}};}};
\\end{{tikzpicture}}
\\end{{center}}"""
latexcode = re.sub(r"\n\s+","\n",latexcode)
self.plainTextEdit_latexcode.setPlainText(latexcode)
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()

View File

@ -0,0 +1,193 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>381</height>
</rect>
</property>
<property name="windowTitle">
<string>生成直方图代码</string>
</property>
<widget class="QPushButton" name="pushButton_generatexlist">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>121</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>生成横坐标序列</string>
</property>
</widget>
<widget class="QTextBrowser" name="textBrowser">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>10</x>
<y>160</y>
<width>121</width>
<height>211</height>
</rect>
</property>
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Microsoft YaHei UI'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;不改变横坐标的值, 在逗号后输入纵坐标的值&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;如果标签和纵坐标的值不同, 在后面的方括号内输入标签&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;如:&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;(10,0.02)[]&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;(20,0.04)[a]&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;(30,0.04][]&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>140</x>
<y>10</y>
<width>121</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>输入纵坐标信息</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit_yinfo">
<property name="geometry">
<rect>
<x>140</x>
<y>30</y>
<width>171</width>
<height>181</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton_exec">
<property name="geometry">
<rect>
<x>320</x>
<y>30</y>
<width>71</width>
<height>181</height>
</rect>
</property>
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>生成代码</string>
</property>
</widget>
<widget class="QLabel" name="label_7">
<property name="geometry">
<rect>
<x>140</x>
<y>220</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>直方图代码</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit_latexcode">
<property name="geometry">
<rect>
<x>140</x>
<y>240</y>
<width>251</width>
<height>131</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>121</width>
<height>108</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>x最小值</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_xmin"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>x最大值</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_xmax"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>x宽度</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_xwidth"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>x轴label</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_xlabel"/>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>