【问题标题】:PYQT Accessing and changing dynamically added controlsPYQT 访问和更改动态添加的控件
【发布时间】:2017-08-31 15:40:59
【问题描述】:

我是 GUI 和 PYQT 的初学者。我要做的是动态设置 QComboBox 和 QLineEdit 的网格。从 QComboBox 您可以选择一个选项,然后从该选项中,它将用一些数字填充相应的 QLineEdit。我遇到的问题是在第一个 QComboBox 和第一个 QLineEdit 框之间创建链接。我可以为每一行创建一个函数,但我想知道一个更好的方法。我将发布一些示例代码。感谢您提供的任何帮助或建议。

import sys
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class window(QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 700, 600)

        self.home()

    def home(self):
        Test1Choices = ['Test1:','Choice1', 'Choice2', 'Choice3', 'Choice4','Choice5', 'Choice6', 'Choice7', 'Choice8', 'Choice9']
        Test2Choices= ['Test2:','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
        for i in range(0,10):
            Choice1ComboBox = QComboBox(self)
            Choice1ComboBox.addItems(Test1Choices)
            Choice1ComboBox.resize(150,25)
            Choice1ComboBox.move(30,(150+(i*35)))
            Choice1ComboBox.setCurrentIndex(2)

            Choice2ComboBox = QComboBox(self)
            Choice2ComboBox.setObjectName("Choice2ComboBox"+str(i))
            Choice2ComboBox.addItems(Test2Choices)
            Choice2ComboBox.resize(75,25)
            Choice2ComboBox.move(200,(150+(i*35)))
            Choice2ComboBox.setCurrentIndex(2)
            Choice2ComboBox.activated[str].connect(self.doSomething)

            numTextBox = QLineEdit(self)
            numTextBox.setObjectName("numBox"+str(i))
            numTextBox.move(325,(150+(i*35)))
            numTextBox.resize(35,25)

            result1TextBox = QLineEdit(self)
            result1TextBox.setObjectName("result1Box"+str(i))
            result1TextBox.move(400,(150+(i*35)))
            result1TextBox.resize(100,25)
            result1TextBox.setEnabled(0)

            result2TextBox = QLineEdit(self)
            result2TextBox.setObjectName("result2Box"+str(i))
            result2TextBox.move(525,(150+(i*35)))
            result2TextBox.resize(100,25)
            result2TextBox.setEnabled(0)

        self.show()

    def doSomething(self):
        numbers=['result1','result2','result3','result4','result5','result6','result7','result8','result9','result10','result11','result12','result13','result14','result15']

def run():
    app = QApplication(sys.argv)
    Gui = window()
    sys.exit(app.exec_())


run()

总结一下,我想引入所选QComboBox 的索引。然后使用该索引号来引用“数字”数组中的答案。然后在同一行中的QLineEdit 中打印该结果

【问题讨论】:

  • 我看到你有2列组合框,一列QLineEdit和另外2列QLineEdit禁用,它们之间有什么关系。您的描述与他们无关。
  • 这是为了计算一组电线的总面积。第一个组合框是针对每条电线的绝缘类型,而第二个组合框是电线尺寸。因此,如果您有绝缘类型的 THWN 并且尺寸为 12awg,那么 0.0133 应该出现在同一行的第一个禁用的 QLineEdit 中。我试图更改代码以使其更通用。
  • 禁用的其他 QLineEdits 你不会使用它们吗?您只想使用 2 QComboBox 和 QLineEdit。
  • 对不起,我意识到这不是很清楚,我试图编辑它并且我的计算机关闭以安装更新。但第一个组合框用于电线的绝缘类型。第二个组合框用于电线尺寸。第一个 QLineEdit 是供用户输入他们拥有的线数。第一个禁用的 QLineEdit 应该打印出所选单条线的大小,最后一个 QLineEdit 应该是一条线的面积 * 数量
  • 我猜输入是启用了 2 个 QComboBox 和 QLineEdit,输出是禁用了最后 2 个 QLineEdit,对吗?

标签: python python-3.x pyqt pyqt5


【解决方案1】:

我们使用sender()获取发出信号的对象,然后使用setObjectName()查找该对象的名称,然后搜索索引,然后使用findChildren()获取其他对象,例如输出将是所选文本的并集。

为 Choice1ComboBox 添加名称:

Choice1ComboBox.setObjectName("Choice1ComboBox"+str(i))

doSomething 函数:

def doSomething(self, _):
    sender = self.sender()

    l = sender.objectName().split("Choice1ComboBox")
    if len(l) > 1:
        number = l[1]
    else:
        number = sender.objectName().split("Choice2ComboBox")[1]

    combo1 = self.findChildren(QComboBox, "Choice1ComboBox"+number)[0]
    combo2 = self.findChildren(QComboBox, "Choice2ComboBox"+number)[0]
    obj = self.findChildren(QLineEdit, "numBox"+number)[0]
    obj.setText(combo1.currentText() + " " +  combo2.currentText())

完整代码:

import sys
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class window(QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 700, 600)

        self.home()

    def home(self):
        Test1Choices = ['Test1:','Choice1', 'Choice2', 'Choice3', 'Choice4','Choice5', 'Choice6', 'Choice7', 'Choice8', 'Choice9']
        Test2Choices= ['Test2:','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
        for i in range(0,10):
            Choice1ComboBox = QComboBox(self)
            Choice1ComboBox.setObjectName("Choice1ComboBox"+str(i))
            Choice1ComboBox.addItems(Test1Choices)
            Choice1ComboBox.resize(150,25)
            Choice1ComboBox.move(30,(150+(i*35)))
            Choice1ComboBox.setCurrentIndex(2)
            Choice1ComboBox.activated[str].connect(self.doSomething)

            Choice2ComboBox = QComboBox(self)
            Choice2ComboBox.setObjectName("Choice2ComboBox"+str(i))
            Choice2ComboBox.addItems(Test2Choices)
            Choice2ComboBox.resize(75,25)
            Choice2ComboBox.move(200,(150+(i*35)))
            Choice2ComboBox.setCurrentIndex(2)
            Choice2ComboBox.activated[str].connect(self.doSomething)

            numTextBox = QLineEdit(self)
            numTextBox.setObjectName("numBox"+str(i))
            numTextBox.move(325,(150+(i*35)))
            numTextBox.resize(35,25)

            result1TextBox = QLineEdit(self)
            result1TextBox.setObjectName("result1Box"+str(i))
            result1TextBox.move(400,(150+(i*35)))
            result1TextBox.resize(100,25)
            result1TextBox.setEnabled(0)

            result2TextBox = QLineEdit(self)
            result2TextBox.setObjectName("result2Box"+str(i))
            result2TextBox.move(525,(150+(i*35)))
            result2TextBox.resize(100,25)
            result2TextBox.setEnabled(0)

        self.show()

    def doSomething(self, _):
        sender = self.sender()

        l = sender.objectName().split("Choice1ComboBox")
        if len(l) > 1:
            number = l[1]
        else:
            number = sender.objectName().split("Choice2ComboBox")[1]

        combo1 = self.findChildren(QComboBox, "Choice1ComboBox"+number)[0]
        combo2 = self.findChildren(QComboBox, "Choice2ComboBox"+number)[0]
        obj = self.findChildren(QLineEdit, "numBox"+number)[0]
        obj.setText(combo1.currentText() +  " " + combo2.currentText())




def run():
    app = QApplication(sys.argv)
    Gui = window()
    sys.exit(app.exec_())


run()

【讨论】:

  • 就是这样!太感谢了!!我花费的时间比我想承认的要多。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 2011-02-18
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多