如何存储 QLineEdit 的字符串并能够使用 QPushButton 将存储的字符串添加到 QLineEdit?

How to store a string of QLineEdit and to be able to add the stored string to the QLineEdit with a QPushButton?

我正在为我的计算器实现一个 'ANS' 按钮,将最后计算的结果添加到 QLineEdit。我计划有一个 self.stored = '' 变量,然后在每次调用 calculate 信号时更改它。但是不知道怎么回事,可能是存储不正确

我创建了一个代码示例来展示我的计算器的外观。

import sys
from functools import cached_property, partial
from PyQt6.QtCore import QRect, Qt
from PyQt6.QtGui import QAction
from PyQt6.QtWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.buttons = {}
        self.setWindowTitle("Try")

        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        self.lay = QVBoxLayout(central_widget)
        self.lineedit()

        maps = {"Store": (0, 0), "B": (0, 1), "C": (1, 0), "D": (1, 1), "Return": (2, 0, 1, 2)}
        buttons = self.create_page(maps)
        self.lay.addWidget(buttons)

    def lineedit(self):
        self.le = QLineEdit()
        self.le.setFixedHeight(35)
        self.lay.addWidget(self.le)

    def set_lineedit(self, text):
        self.le.setText(text)
        self.le.setFocus()

    def line(self):
        return self.le.text()

    def create_page(self, map_letters):
        page = QWidget()
        grid_layout = QGridLayout(page)
        for name, pos in map_letters.items():
            if name == 'Return':
                self.buttons[name] = QPushButton(name)
                self.buttons[name].setFixedSize(120, 40)
                grid_layout.addWidget(self.buttons[name], *pos)
            else:
                self.buttons[name] = QPushButton(name)
                self.buttons[name].setFixedSize(40, 40)
                grid_layout.addWidget(self.buttons[name], *pos)
        return page

class Controller:

    def __init__(self, MainWindow):

        self.view = MainWindow
        self.stored = ''
        self.connectSignals()

    def store(self):

        currentText = self.view.line()
        self.stored = currentText

    def buildExpression(self, sub_exp):

        expression = self.view.line() + sub_exp
        self.view.set_lineedit(expression)

    def connectSignals(self):

        for btnText, btn in self.view.buttons.items():
            if btnText not in ['Store', 'Return']:
                btn.clicked.connect(partial(self.buildExpression, btnText))

            self.view.buttons['Return'].clicked.connect(partial(self.buildExpression, self.stored))
            self.view.buttons['Store'].clicked.connect(self.store)

app = QApplication(sys.argv)
w = MainWindow()
Controller(w)
w.show()
app.exec()

外观:

所以按钮 BCD 将它们各自的字母添加到 QLineEdit,然后我希望每次单击“存储”按钮时将 QLineEdit 中的当前文本存储到一个变量中,并且当“Return " 单击它应该将存储的变量添加到 QLineEdit。

假设您点击 B,然后点击 C,然后点击 D,那么现在您的 QLineEdit 是 BCD。 现在您单击 'Store',BCD 现在必须存储到一个变量中。 然后,如果您单击 'Return',则存储的变量现在将添加到 QLineEdit。 所以你最终的 QLineEdit 应该是 'BCDBCD'.

但是 'Return' 按钮不起作用,因为它不会将 'BCD' 添加到 QLineEdit。

问题是,当您使用部分时,您表示您将在连接时将“存储”的值传递给它,这是一个空字符串,因此您总是添加一个空字符串意味着不修改任何东西。逻辑是您创建另一个使用“存储”的当前值的函数。

class Controller:
    def __init__(self, view):
        self.view = view
        self.stored = ""
        self.connectSignals()

    def handleStore(self):
        self.stored = self.view.line()

    def handleReturn(self):
        self.buildExpression(self.stored)

    def buildExpression(self, sub_exp):
        expression = self.view.line() + sub_exp
        self.view.set_lineedit(expression)

    def connectSignals(self):
        for btnText, btn in self.view.buttons.items():
            if btnText == "Return":
                btn.clicked.connect(self.handleReturn)
            elif btnText == "Store":
                btn.clicked.connect(self.handleStore)
            else:
                btn.clicked.connect(partial(self.buildExpression, btnText))