I am trying to create a calculator that can switch from a basic calculator to a scientific calculator. I've been advised to use stacked widgets to layout the buttons. But I'm encountering a problem when switching between the widgets.
The size of the main window. The main window defaults to the size of the largest widget, so the buttons of the basic calculator look small in the size that should be filled in by the buttons of the scientific calculator. Below are images depicting it: Basic Calculator, this is the first widget:
vs Scientific Calculator, the second widget:
Below is the layout I want to achieve for switching from Scientific Calculator to Basic Calculator (from 2nd widget to 1st widget):
Below is a basic code example that I am using on my calculator.
import sys
from functools import cached_property, partial
from PyQt6.QtCore import QRect, Qt
from PyQt6.QtGui import QAction
from PyQt6.QtWidgets import (
QApplication,
QGridLayout,
QLineEdit,
QMainWindow,
QMenu,
QMenuBar,
QPushButton,
QStackedWidget,
QVBoxLayout,
QWidget,
)
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()
self.lay.addWidget(self.stacked_widget, alignment=Qt.AlignmentFlag.AlignCenter)
maps = [
{"A": (0, 0), "B": (0, 1), "C": (1, 0), "D": (1, 1)}, # first page
{"A": (0, 0), "B": (0, 1), "C": (1, 0), "D": (1, 1),"E": (0, 2), "F": (0, 3), "G": (1, 2), "H": (1, 3)}, # second page
]
for m in maps:
page = self.create_page(m)
self.stacked_widget.addWidget(page)
@cached_property
def stacked_widget(self):
return QStackedWidget()
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():
self.buttons[name] = QPushButton(name)
self.buttons[name].setFixedSize(20, 20)
grid_layout.addWidget(self.buttons[name], *pos)
return page
class Menu:
def __init__(self, MainWindow):
super().__init__()
self.view = MainWindow
self.menuBar = QMenuBar()
self.menuBar.setGeometry(QRect(0, 0, 277, 22))
self.view.setMenuBar(self.menuBar)
self.open = QMenu(self.menuBar)
self.open.setTitle("Open")
self.menuBar.addAction(self.open.menuAction())
self.this = QAction(self.menuBar)
self.this.setText("This")
self.this.setCheckable(True)
self.open.addAction(self.this)
self.this.triggered.connect(self.show_new_window)
def show_new_window(self, checked):
self.view.stacked_widget.setCurrentIndex(1 if checked else 0)
class Controller:
def __init__(self, MainWindow):
self.view = MainWindow
self.connectSignals()
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():
btn.clicked.connect(partial(self.buildExpression, btnText))
app = QApplication(sys.argv)
w = MainWindow()
Controller(w)
m = Menu(w)
w.show()
app.exec()
How can I set the fixed size of each widget in a stacked widget?