Issue
I'm in the process of implementing an 'ANS' button for my calculator that adds the last calculated result to the QLineEdit. I planned to have a self.stored = ''
variable and then have it changed every time the calculate
signal is called. But I don't know what's wrong, it may be that it is not storing correctly.
I created a code example of what my Calculator looks like.
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()
What it looks like:
So buttons BCD add their respective letters to the QLineEdit, then I want the current text in the QLineEdit to be stored to a variable every time the button "Store" is clicked, and when the "Return" is clicked it should add the stored variable to the QLineEdit.
So say you click B then C then D, so now your QLineEdit is BCD. Now you click 'Store', the BCD must be now stored to a variable. Then if you click 'Return', the stored variable is now added to the QLineEdit. So your final QLineEdit should be 'BCDBCD'.
But the 'Return' button doesn't work, as it does not add 'BCD' to the QLineEdit.
Solution
The problem is that when you use the partial you are indicating that you are going to pass it the value of "stored" at the time of connection and that is an empty string so you are always adding an empty string that implies not modifying anything. The logic is that you create another function that uses the current value of "stored".
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))
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.