【问题标题】:Dynamic buttons - Open file, read and insert text into corresponding column动态按钮 - 打开文件,读取文本并将其插入相应的列
【发布时间】:2017-12-12 08:02:18
【问题描述】:

我的代码根据用户从标记为 self.numberLine 的 QlineEdit 框中的输入动态创建一定数量的按钮。每个按钮将打开文件对话框以允许用户指定它搜索文件的文件,然后将文本插入相应的 QlineEdit。我遇到的问题是我无法识别正在按下哪个按钮,所以我无法填写 QLineEdit 字段的相应列。

我让它有点工作,但是当我点击第二个按钮时,它只是添加到第一列,而不是第二列

清除按钮什么都不做

示例: 如果用户在 numberLine 小部件中输入“3”并单击“确定”,它将创建 3 列小部件,顶部小部件将是为用户打开文件对话框的按钮。下面的其他小部件是 QlineEdit 条目

附带说明,当用户更改编号时,它会创建适当数量的列,但当我选择要搜索的文件时它不会插入任何内容。

任何线索我错过了什么?

图片

    spacer1 = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
    spacer2 = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)

    self.hbutton.addItem(spacer1)
    self.hbutton.addItem(spacer2)
    self.hboxlayout = QtWidgets.QHBoxLayout()
    self.hboxlayout0 = QtWidgets.QHBoxLayout()
    self.hboxlayout1 = QtWidgets.QHBoxLayout()
    self.hboxlayout2 = QtWidgets.QHBoxLayout()
    self.hboxlayout3 = QtWidgets.QHBoxLayout()
    self.hboxlayout4 = QtWidgets.QHBoxLayout()
    self.hboxlayout5 = QtWidgets.QHBoxLayout()

    self.gridLayout_12.addLayout(self.hbutton, 1, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout, 0, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout0, 1, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout1, 2, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout2, 3, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout3, 4, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout4, 6, 1, 1, 1)
    self.gridLayout_12.addLayout(self.hboxlayout5, 7, 1, 1, 1)
    self.pushButton_9.clicked.connect(self.addLineEdit)

    self.numberLine = QtWidgets.QLineEdit()
    self.numberLine.setMaximumSize(QtCore.QSize(60, 16777215))
    self.numberLine.setObjectName("self.numberLine")
    self.horizontalLayout_7.addWidget(self.numberLine)
    self.Array = []
    self.PDiffEntryList = []

def clearLayout(self):
    # del self.Array[:]
    while  self.hboxlayout0.count():
        child =  self.hboxlayout.takeAt(0)
        child1 = self.hboxlayout0.takeAt(0)
        child2 = self.hboxlayout1.takeAt(0)
        child3 = self.hboxlayout2.takeAt(0)
        child4 = self.hboxlayout3.takeAt(0)
        child5 = self.hboxlayout4.takeAt(0)
        child6 = self.hboxlayout5.takeAt(0)
        if child.widget():
            child.widget().deleteLater()
        if child1.widget():
            child1.widget().deleteLater()
        if child2.widget():
            child2.widget().deleteLater()
        if child3.widget():
            child3.widget().deleteLater()
        if child4.widget():
            child4.widget().deleteLater()
        if child5.widget():
            child5.widget().deleteLater()
        if child6.widget():
            child6.widget().deleteLater()

def addLineEdit(self):
    try:
        self.clearLayout()
        self.FileButton = {}
        for i in range(int(self.numberLine.text())):
            self.FileButton[i] = QtWidgets.QPushButton(self.centralwidget)
            self.FileButton[i].setText('File')
            self.FileButton[i].setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout.addWidget(self.FileButton[i])
            self.FileButton[i].clicked.connect(lambda i=i: self.openfile(i))

            self.le = QtWidgets.QLineEdit()
            self.le.setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout0.addWidget(self.le)

            self.le1 = QtWidgets.QLineEdit()
            self.le1.setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout1.addWidget(self.le1)

            self.le2 = QtWidgets.QLineEdit()
            self.le2.setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout2.addWidget(self.le2)

            self.le3 = QtWidgets.QLineEdit()
            self.le3.setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout3.addWidget(self.le3)

            self.le4 = QtWidgets.QLineEdit()
            self.le4.setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout4.addWidget(self.le4)

            self.le5 = QtWidgets.QLineEdit()
            self.le5.setMaximumSize(QtCore.QSize(50, 16777215))
            self.hboxlayout5.addWidget(self.le5)
            self.PDiffEntryList.append(self.le)
    except:
        print('')
def searchfile(self,dir):
    try:
        with open(dir) as f:
            content = f.readlines()

            # # Main Info - Ps, Pd, Ts
            MainList = content[44].split()
            RPM = str(round(float(MainList[0]), 2))
            Ps = str(round(float(MainList[1]), 2))
            Ts = str(round(float(MainList[2]), 2))
            Pd = str(round(float(MainList[3]), 2))
            Ratio = str(round(Pd / Ps, 2))
            DiffP = str(round(Pd - Ps, 2))
            self.input(i)
    except:
        print('Only out files')
def input(self,i):
    if i == 0:
        print('0')
    elif i == 1:
        print('1')
def openfile(self,i):
    filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Choose file')
    dir = filename[0]
    directory = os.path.split(dir)[0]
    self.searchfile(dir,i)

【问题讨论】:

    标签: python pyqt python-3.5 pyqt5


    【解决方案1】:

    你正在做很多奇怪的事情。例如,不确定您为什么将“i”声明为全局变量或您选择的变量名称。不过,为了解决您的主要问题,您正在迭代一个 for 循环,在其中一遍又一遍地创建一个具有相同名称的对象。

     try:
            self.clearLayout()
            self.file_buttons = {}
            for i in range(int(self.numberLine.text())):
                self.file_buttons[i] = QtWidgets.QPushButton(self.centralwidget)
    

    现在您可以参考 self.file_buttons[i] 来了解您想要对按钮进行的​​任何更改,但实际上每次都将其存储为单独的对象。这同样适用于您创建的所有 Line Edit 变量。

    一般来说,您似乎一遍又一遍地创建相同的对象,而只是写出不同的名称(object1、object2 等)。这是使用数组或字典迭代并创建所有这些的好机会。这将使跟踪正在发生的事情变得更加容易,并且更加灵活。

    【讨论】:

    • 全局只是我添加的东西忘记取出,至于在数组中创建相同的对象,您指的是QLineEdits吗?如果用户将 3 作为数字输入,并且我想访问第二个按钮,我会使用 If self.file_buttons[2].clicked: 来获取第二个按钮吗?
    • 我按照您提供的代码和它做同样的事情。 if self.FileButton[1]: print('1') elif self.FileButton[2]: print('2') 当我点击第一个按钮时,它会按原样打印 '1',但是当按下第二个按钮时,它会打印 ' 1'
    • 您能否使用新代码更新您的问题或提交一个新代码?否则很难确切知道发生了什么
    • 对不起,我以为我做到了,我更新了它。我遇到的主要问题是 def input(self, i) 函数,这是我想知道单击了哪个按钮并根据按钮执行某些操作的地方,现在我将其设置为 i == #,我尝试了 if self.FileButton[0].clicked,它做同样的事情
    • 我看到你调用 self.input 的唯一地方是在函数 searchfile 中,你从来没有真正定义过“i”是什么,所以我不确定你正在发生什么/你在做什么预计。此外,对于所有“self.le”变量,您仍然会遇到与 FileButton 相同的问题。我的建议是阅读一些 PyQt 教程和 python 编程教程,以了解编写良好的代码是什么样的,然后重新评估您的特定问题并将简化版本作为新问题发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2021-04-16
    • 1970-01-01
    • 2015-10-21
    • 2014-08-27
    • 1970-01-01
    相关资源
    最近更新 更多