繁体   English   中英

PySide6:当我使用自定义类重写的函数 sizeHint() 时出现错误“致命的 Python 错误:无法从堆栈溢出中恢复”

[英]PySide6: The errors "Fatal Python error: Cannot recover from stack overflow" occurrs when I use my custom class' rewritten function sizeHint()

我模仿QT的官方文档写了一个计算器的例子。

我尝试将源代码重写为 python 并进行一些更改。 我的代码是打击:

import sys

from PySide6.QtWidgets import (QApplication, QSizePolicy, QWidget, QToolButton, QGridLayout)
from PySide6 import QtCore


class Advanced_Calculator(QWidget):
    def __init__(self):
        super(Advanced_Calculator, self).__init__()

        self.mainwindow = QGridLayout(self)

        # Digit buttons and operator buttons

        self.widget_button = QWidget()

        self.button_digit = []

        for i in range(0, 10):
            self.button_digit.append(Button(str(i)))

        self.button_factor = Button("!")
        self.button_lbracket = Button("(")
        self.button_rbracket = Button(")")
        self.button_backspace = Button("<-")
        self.button_division = Button("/")
        self.button_log = Button("log")
        self.button_multiply = Button("X")
        self.button_sqrt = Button("√")
        self.button_minus = Button("-")
        self.button_power = Button("^")
        self.button_plus = Button("+")
        self.button_abs = Button("|x|")
        self.button_const = Button("Const")
        self.button_dot = Button(".")
        self.button_equal = Button("=")

        # Buttons layout with 0 spacing

        self.layout_button = QGridLayout()
        self.layout_button.setSpacing(0)

        self.layout_button.addWidget(self.button_factor, 0, 0, 1, 1)
        self.layout_button.addWidget(self.button_lbracket, 0, 1, 1, 1)
        self.layout_button.addWidget(self.button_rbracket, 0, 2, 1, 1)
        self.layout_button.addWidget(self.button_backspace, 0, 3, 1, 1)
        self.layout_button.addWidget(self.button_division, 0, 4, 1, 1)
        self.layout_button.addWidget(self.button_log, 1, 0, 1, 1)

        for i in range(1, 10):
            self.layout_button.addWidget(self.button_digit[i], 3 - ((i - 1) // 3), (i - 1) % 3 + 1, 1, 1)

        self.layout_button.addWidget(self.button_multiply, 1, 4, 1, 1)
        self.layout_button.addWidget(self.button_sqrt, 2, 0, 1, 1)
        self.layout_button.addWidget(self.button_minus, 2, 4, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_plus, 3, 4, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_abs, 4, 0, 1, 1)
        self.layout_button.addWidget(self.button_const, 4, 1, 1, 1)
        self.layout_button.addWidget(self.button_digit[0], 4, 2, 1, 1)
        self.layout_button.addWidget(self.button_dot, 4, 3, 1, 1)
        self.layout_button.addWidget(self.button_equal, 4, 4, 1, 1)

        self.widget_button.setLayout(self.layout_button)

        # button layout set to mainwindow

        self.mainwindow.addWidget(self.widget_button)


class Button(QToolButton):
    def __init__(self, text, parent=None):
        super(Button, self).__init__(parent)

        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.setText(text)

    def sizeHint(self) -> QtCore.QSize:
        size = self.sizeHint()
        size.setHeight(size.height + 20)
        size.setWidth(max(size.width(), size.height()))
        # size.rheight() += 20
        # size.rwidth() = max(size.width(), size.height())

        return size


if __name__ == "__main__":
    app = QApplication([])

    Calculator = Advanced_Calculator()
    Calculator.show()

    sys.exit(app.exec())

当我调试这些代码时,会发生“致命的 Python 错误:无法从堆栈溢出中恢复”。 现在我知道错误是由 sizeHint() 函数引起的。 问题出在哪儿?

顺便说一句,该函数是我根据对 sizeHint() 函数的理解重写的。 官方文档使用了我给他们注释的两行代码,它也不起作用。 嗯,我也想不通问题出在哪里。

如果你能帮我解决这两个问题,那就太好了。 还是很感谢你!

考虑代码...

def sizeHint(self) -> QtCore.QSize:
    size = self.sizeHint()
    size.setHeight(size.height + 20)
    size.setWidth(max(size.width(), size.height()))
    # size.rheight() += 20
    # size.rwidth() = max(size.width(), size.height())

    return size

线...

size = self.sizeHint()

导致Button::sizeHint调用自身导致无限递归,从而导致堆栈溢出。 也许您打算改为调用基类实现...

size = super(Button, self).sizeHint()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM