我想知道在PyQt环境中分配值和对多个不同变量进行计算的最佳方式,其中变量已分配到QtDesigner UI文件中。PyQt - 通过代码管理变量名称和值
变量是根据不同的分组命名的,因为每个组可以有多个变量,所以它们有一个反映其顺序的次要命名。
例如,我有;
{group_name}_range_{number}_start
{group_name}_range_{number}_end
我想知道如何分配,并以更Python化的方式用的这些变量的工作,将该代码复制多次,打开它错误。
这是我的Qt ui
文件(单行节省空间);
<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>390</width><height>261</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><widget class="QLineEdit" name="a_range_1_start"><property name="geometry"><rect><x>30</x><y>30</y><width>113</width><height>41</height></rect></property></widget><widget class="QLineEdit" name="a_range_1_end"><property name="geometry"><rect><x>150</x><y>30</y><width>113</width><height>41</height></rect></property></widget><widget class="QLineEdit" name="a_range_2_start"><property name="geometry"><rect><x>30</x><y>80</y><width>113</width><height>41</height></rect></property></widget><widget class="QLineEdit" name="a_range_2_end"><property name="geometry"><rect><x>150</x><y>80</y><width>113</width><height>41</height></rect></property></widget><widget class="QLineEdit" name="b_range_1_start"><property name="geometry"><rect><x>30</x><y>190</y><width>113</width><height>41</height></rect></property></widget><widget class="QLineEdit" name="b_range_1_end"><property name="geometry"><rect><x>150</x><y>190</y><width>113</width><height>41</height></rect></property></widget><widget class="QLabel" name="a_range_1_sum"><property name="geometry"><rect><x>280</x><y>40</y><width>71</width><height>21</height></rect></property><property name="font"><font><family>Arial</family><pointsize>12</pointsize></font></property><property name="text"><string>TextLabel</string></property></widget><widget class="QLabel" name="a_range_2_sum"><property name="geometry"><rect><x>280</x><y>90</y><width>71</width><height>21</height></rect></property><property name="font"><font><family>Arial</family><pointsize>12</pointsize></font></property><property name="text"><string>TextLabel</string></property></widget><widget class="QLabel" name="b_range_1_sum"><property name="geometry"><rect><x>280</x><y>200</y><width>71</width><height>21</height></rect></property><property name="font"><font><family>Arial</family><pointsize>12</pointsize></font></property><property name="text"><string>TextLabel</string></property></widget></widget></widget><resources/><connections/></ui>
我有下列变量a_range_1_start
,a_range_1_end
,a_range_2_start
,a_range_2_end
,b_range_1_start
,b_range_1_end
落入予上述的格式。 python代码如下复制该问题;
import sys
import pandas as pd
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
qtCreatorFile = r'test.ui'
Ui_MainWindow, QtBaseClass = loadUiType(qtCreatorFile)
class MainApp(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, *args):
super().__init__()
self.initUI()
def initUI(self, parent=None, *args):
QMainWindow.__init__(self, None)
Ui_MainWindow.__init__(self)
self.df1 = pd.DataFrame({'mynumbers': range(10)},
index=['a','b','c','d','e','f','g','h','i','j'])
self.df2 = pd.DataFrame({'values': range(10, 40)})
self.setupUi(self)
self.show()
self.a_range_1_start.textChanged.connect(self.a_range_1_start_function)
self.a_range_1_end.textChanged.connect(self.a_range_1_end_function)
def a_range_1_start_function(self):
# Print replaces other text functions here for example...
print('start - ' + self.a_range_1_start.text())
self.calc_a_range_1()
def a_range_1_end_function(self):
print('end - ' + self.a_range_1_end.text())
self.calc_a_range_1()
def calc_a_range_1(self):
myvalue = self.df1.loc[self.a_range_1_start.text():self.a_range_1_end.text(),:].mynumbers.sum()
if myvalue > 0:
self.a_range_1_sum.setText('{:,.2f}'.format(myvalue))
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainApp()
w.show()
sys.exit(app.exec_())
这只是一个快速重现问题,功能和过程并不完全是我使用的。
最后,我想有在a_range_2_start
,在UI a_range_2_end
变量被再次附接到df1
(类似于如何a_range1_start
和a_range_1_end
上df1
执行Calcs(计算)),但显示器附接到a_range_2_sum
QLabel。
此外,我希望b_range_1_start
和b_range_1_end
代替df2
和相应的QLabel。
希望这足以说明问题。非常感谢!
非常感谢,非常感谢。我正在学习这些方法和模式,我非常感谢您的帮助和参考。干杯! –