0

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:

Basic Calculator

vs Scientific Calculator, the second widget:

Scientific Calculator Edit:

Below is the layout I want to achieve for switching from Scientific Calculator to Basic Calculator (from 2nd widget to 1st widget): What I want the Basic Calculator look to become

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?

musicamante
  • 41,230
  • 6
  • 33
  • 58
JA23Z
  • 25
  • 5
  • 1
    Please do not post several questions in the same post, if you have several problems then you must create several posts, one for each problem. On the other hand you could show an image or diagram of what you want to obtain. – eyllanesc Jun 28 '21 at 20:58
  • I'm not sure about what you're asking. Do you want all buttons to fill all the available space? Maybe you can provide a mockup image of what you'd like to achieve. – musicamante Jun 28 '21 at 22:29
  • @musicamante I edited it to show it visually. – JA23Z Jun 29 '21 at 02:46
  • @JA23Z So you want to resize *the whole window*, not "each widget". – musicamante Jun 29 '21 at 07:31
  • @musicamante Oh yeah I guess that would be more accurate, apologies. – JA23Z Jun 30 '21 at 09:32
  • Note: the question marked as duplicate is for QTabWidget, but the same concept is also valid for QStackedWidget (on which QTabWidget is based). So, you just need to subclass just like as in the provided answer (but subclassing from QStackedWidget instead of QTabWidget), and add that to your UI instead of the basic implementation you're using. – musicamante Jul 01 '21 at 03:35

0 Answers0