如何重新初始化pythonpyqt5gui

2024-10-01 11:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由两个QWidgets组成的简单程序。一个是输入窗口,另一个是输出窗口。输入窗口有一个QlineEdit,一旦我点击Apply using a signal and slot,它的值就会传递到输出窗口QlineEdit。我目前的计划是什么样的:

enter image description here

到目前为止一切正常。我想要实现的是,根据我在输入中给出的数字,输出窗口中应该出现类似数量的“Entry options”(新的QlineEdits)。我想要达到的目标:

enter image description here

我希望我的目标很明确。下面你会发现我的工作代码如图1所示。我刚开始学习编程,所以在这个阶段,小的代码示例比通常枯燥的手册更能帮助我。在

from PyQt5.QtWidgets import QLabel, QHBoxLayout, QWidget, QLineEdit, QPushButton, QGridLayout, QApplication
from PyQt5.QtCore import pyqtSignal, pyqtSlot
import sys


class Input(QWidget):

    dataChanged = pyqtSignal(str)

    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):

        self.input_label = QLabel('Entries:', self)
        self.input_cell = QLineEdit()
        self.apply_button = QPushButton('Apply')
        self.apply_button.setEnabled(False)

        self.hboxLayout = QHBoxLayout(self)

        self.hboxLayout.addWidget(self.input_label)
        self.hboxLayout.addWidget(self.input_cell)
        self.hboxLayout.addWidget(self.apply_button)

        self.setLayout(self.hboxLayout)

        self.input_cell.textChanged.connect(self.apply_change)

        self.setWindowTitle("Input window")
        self.show()

    def apply_change(self, value):
        self.apply_button.setEnabled(True)
        self.send_this = value
        self.apply_button.clicked.connect(self.send_value)

    def send_value(self, value):
        if value is False:
            self.dataChanged.emit(self.send_this)
            self.apply_button.setEnabled(False)


class Output(QWidget):
    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('Output window')

        self.content()

        self.show()

    def content(self):
        layout = QGridLayout()
        output_label = QLabel('Nr. of entries:', self)
        self.output_cell = QLineEdit()

        entry_label_1 = QLabel('Entry1:', self)
        entry_cell_1 = QLineEdit()
        layout.addWidget(entry_label_1, 2, 0)
        layout.addWidget(entry_cell_1, 2, 2)
        entry_label_2 = QLabel('Entry2:', self)
        entry_cell_2 = QLineEdit()
        layout.addWidget(entry_label_2, 4, 0)
        layout.addWidget(entry_cell_2, 4, 2)

        layout.addWidget(output_label, 0, 0)
        layout.addWidget(self.output_cell, 0, 2)

        layout.setSpacing(10)
        self.setLayout(layout)

    def make_connection(self, input_object):
        input_object.dataChanged.connect(self.get_input_value)

    @pyqtSlot(str)
    def get_input_value(self, val):
        self.output_cell.setText(val)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    inp = Input()
    out = Output()
    # Making the connection
    out.make_connection(inp)
    sys.exit(app.exec_())

Tags: selfinputinitvaluedefcellbuttonlabel
2条回答

好消息是你的问题有一半很容易解决。为此替换代码并执行它以查看结果。在

def content(self):
    layout = QGridLayout()
    output_label = QLabel('Nr. of entries:', self)
    self.output_cell = QLineEdit()

    for x in range(3):
        entry_label_1 = QLabel('Entry'+str(x)+':', self)
        entry_cell_1 = QLineEdit()
        layout.addWidget(entry_label_1, x+1, 0)
        layout.addWidget(entry_cell_1, x+1, 2)

    layout.addWidget(output_label, 0, 0)
    layout.addWidget(self.output_cell, 0, 2)
    layout.setSpacing(10)
    self.setLayout(layout)

在我的例子中,我使用range(3)作为循环的例子,告诉程序创建3个文本框。如果你改变范围(10)。。。好吧,猜猜看。在

现在棘手的部分是,您只需要捕获输入数据并将range(3)替换为int(self.output_cell.text()),就可以完成了。然而,有很多方法可以做到这一点。你选择使用信号,而我在我的程序中主要使用全局变量,所以我不完全知道它将如何为你工作。当我找到一个简单的方法时,我会更新这个答案。在

@saelyth的答案很有趣,但使用内存不正确,因为每次更改数字时都会创建布局,我的解决方案不会创建更多的布局,而是重用它们。在

为此,我们必须进行一些更改以创建更稳定的代码:

  1. QIntValidator设置为QLineEdit,以便用户只能放置数字:

self.input_cell.setValidator(QIntValidator(0, 1000, self))
  1. 将QGridLayout更改为QFormLayout,因为它被优化为在另一个小部件旁边有一个标签。在

^{pr2}$
  1. 我们必须添加带有addRow()的项,并删除带有removeRow()的项:

@pyqtSlot(str)
def get_input_value(self, val):

    self.output_cell.setText(val)
    val_int = int(val)

    if val_int > (self.layout().rowCount() - 1):
        [self.hboxLayout.addRow('Entry '+str(i), QLineEdit(self)) for i in range(self.layout().rowCount(), val_int+1)]

    elif val_int < (self.layout().rowCount() - 1):
        for i in range(self.layout().rowCount(), val_int, -1):
            child = self.hboxLayout.removeRow(i)

完整代码:

class Input(QWidget):

    dataChanged = pyqtSignal(str)

    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):

        self.input_label = QLabel('Entries:', self)
        self.input_cell = QLineEdit(self)
        self.input_cell.setValidator(QIntValidator(0, 1000, self))
        self.apply_button = QPushButton('Apply')
        self.apply_button.setEnabled(False)

        self.hboxLayout = QHBoxLayout(self)

        self.hboxLayout.addWidget(self.input_label)
        self.hboxLayout.addWidget(self.input_cell)
        self.hboxLayout.addWidget(self.apply_button)

        self.setLayout(self.hboxLayout)

        self.input_cell.textChanged.connect(self.apply_change)
        self.apply_button.clicked.connect(self.send_value)

        self.setWindowTitle("Input window")
        self.show()

    def apply_change(self, value):
        self.apply_button.setEnabled(True)

    def send_value(self, value):
        if value is False:
            self.dataChanged.emit(self.input_cell.text())
            self.apply_button.setEnabled(False)


class Output(QWidget):
    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('Output window')
        self.content()
        self.show()

    def content(self):
        layout = QFormLayout(self)
        self.output_cell = QLineEdit(self)
        layout.addRow('Nr. of entries:', self.output_cell)
        layout.setSpacing(10)

    def make_connection(self, input_object):
        input_object.dataChanged.connect(self.get_input_value)

    @pyqtSlot(str)
    def get_input_value(self, val):

        self.output_cell.setText(val)
        val_int = int(val)

        if val_int > (self.layout().rowCount() - 1):
            [self.layout().addRow('Entry '+str(i), QLineEdit(self)) for i in range(self.layout().rowCount(), val_int+1)]

        elif val_int < (self.layout().rowCount() - 1):
            for i in range(self.layout().rowCount(), val_int, -1):
                child = self.layout().removeRow(i)

相关问题 更多 >