我目前有一个大小为775x380的窗口和一个按钮。当我点击按钮时,我想将窗口的高度增加到775x780,但由于某种原因无效。这是我按下按钮时调用的功能(停靠栏,从一开始就有" +"文本):
def resize(self):
if self.dock.text() == '+':
print('increase size')
self.resize(775,780)
self.dock.setText("-")
else:
print('decrease size')
self.resize(775,380)
self.dock.setText("+")
打印命令更多用于调试,它们工作正常,但由于某种原因,自我调整(775,780)'不管用。窗口保持相同的大小,按钮会改变它的文字来自' +'到' - '反之亦然。
编辑说明:resize()是类MainWindow(对象)中的一个函数 编辑2:这是我使用的整个代码(至少代码的相关部分)
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
#MAIN WINDOW
MainWindow.setObjectName("MainWindow")
MainWindow.resize(775, 340)
MainWindow.setWindowTitle("MainWindow")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#APPLY BUTTON
self.apply = QtWidgets.QPushButton(self.centralwidget)
self.apply.setGeometry(QtCore.QRect(440, 10, 75, 25))
self.apply.setText("Apply")
self.apply.setObjectName("apply")
#REREF BUTTON
self.reref = QtWidgets.QPushButton(self.centralwidget)
self.reref.setGeometry(QtCore.QRect(280, 10, 75, 25))
self.reref.setText("Re Ref")
self.reref.setObjectName("reref")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1324, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
#RESIZE BUTTON
self.dock = QtWidgets.QPushButton(self.centralwidget)
self.dock.setGeometry(QtCore.QRect(-1, 280, 777, 30))
self.dock.setText("+")
self.dock.setObjectName("dock")
#
self.apply.clicked.connect(self._big)
self.reref.clicked.connect(self._small)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def _big(self):
self.resize(775,780)
def _small(self):
self.resize(775,340)
def start(self):
import sys
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(app.deleteLater)
VolatilityManager = QtWidgets.QMainWindow()
ui = MainWindow()
ui.setupUi(VolatilityManager)
VolatilityManager.show()
sys.exit(app.exec_())
if __name__ == "__main__":
mw = MainWindow()
mw.start()
答案 0 :(得分:0)
将resize()方法命名为其他方法,以便它不会覆盖QMainWindow方法。
import sys
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
layout = QtWidgets.QVBoxLayout()
widget = QtWidgets.QWidget(self)
self.setCentralWidget(widget)
widget.setLayout(layout)
self.expand_btn = QtWidgets.QPushButton('Expand', self)
self.contract_btn = QtWidgets.QPushButton('Contract', self)
layout.addWidget(self.expand_btn)
layout.addWidget(self.contract_btn)
self.expand_btn.clicked.connect(self._expand)
self.contract_btn.clicked.connect(self._contract)
def _expand(self):
self.resize(775,780)
def _contract(self):
self.resize(775,380)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()