変数がQtDesignerのuiファイルに既に割り当てられているPyQt環境で、さまざまな変数に値を割り当てて計算する最良の方法を理解したいと思います。PyQt - コードで変数名と値を管理する
変数はさまざまなグループに基づいて名前が付けられます。また、各グループには複数の変数を含めることができるため、順序が反映された2次的な名前が付けられます。
たとえば、私は持っています。
{group_name}_range_{number}_start
{group_name}_range_{number}_end
私はこのコードを複数回コピーするとエラーにそれを開くことが、よりPython的な方法で割り当てて、これらの変数の数で動作する方法を知っていただきたいと思います。
これは私のQt ui
ファイルです(スペースを節約するために1行です)。
<?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_())
これは、機能とプロセスがまさに私が使用しているものではない問題の迅速なレクリエーションです。最後に
、Iは(a_range1_start
とa_range_1_end
がdf1
にCALCSを実行する方法に類似)UIのa_range_2_end
変数が再びdf1
に取り付けられるように、a_range_2_start
で持っていると思いますが、ディスプレイがa_range_2_sum
QLabelに取り付けられています。
また、私はb_range_1_start
とb_range_1_end
をdf2
とそれぞれのQLabelに付けたいと思っています。
これで問題を十分に説明できます。どうもありがとう!
すごくありがとう、ありがとう。私はこれらの方法とパターンを勉強しています。私はあなたの助けと参考にも非常に感謝しています。乾杯! –