PySide6: The errors "Fatal Python error: Cannot recover from stack overflow" occurrs when I use my custom class' rewritten function sizeHint()
PySide6: The errors "Fatal Python error: Cannot recover from stack overflow" occurrs when I use my custom class' rewritten function sizeHint()
我模仿QT的official documentation写了一个计算器的例子。
我尝试将源代码重写为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
调用自身导致无限递归,从而导致堆栈溢出。也许您打算改为调用基础 class 实现...
size = super(Button, self).sizeHint()
我模仿QT的official documentation写了一个计算器的例子。
我尝试将源代码重写为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
调用自身导致无限递归,从而导致堆栈溢出。也许您打算改为调用基础 class 实现...
size = super(Button, self).sizeHint()