【发布时间】:2021-12-06 12:06:42
【问题描述】:
我模仿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() 函数的理解重写的。官方文档使用了我将它们制作成 cmets 的两行代码,它也不起作用。嗯,我也想不通问题出在哪里。
【问题讨论】:
-
请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。