使用python和qt框架构建GUI应用程序时。我想重复使用某些UI元素而不是一遍又一遍地定义它们。在我的示例中,我想创建一个下拉菜单,用y(数字)和结果(数字)定义行数
应用示例
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton,
QLineEdit, QGridLayout, QLabel)
from math import sqrt
class program(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Program')
flow = QLabel('x1')
pressuredrop_1 = QLabel('y1')
pressuredrop_2 = QLabel('y2 ')
kv = QLabel('result 1')
kv_2 = QLabel('result 2')
grid = QGridLayout()
grid.setSpacing(10)
self.setLayout(grid)
self.setGeometry(300,300,300,150)
self.btn = QPushButton('Calculate', self)
self.flow = QLineEdit(self)
self.pd = QLineEdit(self)
self.result = QLineEdit(self)
self.pd_2 = QLineEdit(self)
self.kvresult_2 = QLineEdit(self)
grid.addWidget(flow, 1,0)
grid.addWidget(self.flow, 1, 1)
grid.addWidget(pressuredrop_1, 2,0)
grid.addWidget(self.pd, 2, 1)
grid.addWidget(pressuredrop_2, 3,0)
grid.addWidget(self.pd_2, 3, 1)
grid.addWidget(kv, 2,2)
grid.addWidget(self.result, 2,3)
grid.addWidget(kv_2, 3,2)
grid.addWidget(self.kvresult_2, 3,3)
grid.addWidget(self.btn, 4,1)
self.btn.clicked.connect(self.calculate)
self.btn.clicked.connect(self.calculate_2)
self.show()