0

我正在尝试创建一个可以从基本计算器切换到科学计算器的计算器。有人建议我使用堆叠的小部件来布局按钮。但是在小部件之间切换时遇到问题。

主窗口的大小。主窗口默认为最大小部件的大小,因此基本计算器的按钮看起来很小,应该由科学计算器的按钮填充。下面是描述它的图像: 基本计算器,这是第一个小部件:

基本计算器

vs 科学计算器,第二个小部件:

科学计算器 编辑:

下面是我想要实现的从科学计算器切换到基本计算器(从第二个小部件到第一个小部件)的布局: 我想要基本计算器的样子

下面是我在计算器上使用的基本代码示例。

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()

如何在堆叠的小部件中设置每个小部件的固定大小?

4

0 回答 0