[英]PyQt5: How to Resize mainwindow to fit a Stackedwidget or Stackedlayout
[英]PyQt5 MainWindow resize() call not working
我有一個 PyQt5 GUI 應用程序主窗口,它根據屏幕大小設置幾何。 當我調用toogleLogWindow()
函數時, toogleLogWindow()
的可見性屬性hLayoutWidget_error
發生變化,但不會發生窗口大小調整。 當我通過單擊右上角的恢復按鈕手動恢復主窗口時,調整大小功能起作用。 誰能幫助我理解這種行為? 默認情況下不檢查actionToggleLogWindow
狀態。
import sys, os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUI()
def setupUI(self):
# Set screen size parameters
for i in range(QApplication.desktop().screenCount()):
self.window_size = QApplication.desktop().availableGeometry(i).size()
self.resize(self.window_size)
self.move(QPoint(0, 0))
self._button = QtWidgets.QPushButton(self)
self._button.setText('Test Me')
self._editText = QtWidgets.QComboBox(self)
self._editText.setEditable(True)
self._editText.addItem("")
self._editText.setGeometry(QtCore.QRect(240, 40, 113, 21))
# Connect signal to slot
self._button.clicked.connect(self.toogleLogWindow)
def toogleLogWindow(self):
if self._editText.currentText() == "0":
h = self.window_size.height()
w = int(self.window_size.width()/2)
self.resize(w,h)
elif self._editText.currentText() == "1":
h = self.window_size.height()
w = int(self.window_size.width())
self.resize(w,h)
else:
pass
def get_main_app(argv=[]):
app = QApplication(argv)
win = MainWindow()
win.show()
return app, win
def main():
app, _win = get_main_app(sys.argv)
return app.exec_()
if __name__ == '__main__':
sys.exit(main())
應當指出的是:
似乎如果在顯示之前設置窗口的最大大小然后顯示它就相當於最大化窗口。
當窗口最大化時,除非您將其返回到以前的狀態,否則您無法更改其大小,例如,如果您手動更改窗口的大小,直到它處於正常狀態,那么您只需更改大小即可。
因此,對於這種情況,有幾種替代方案:
不要設置屏幕的全尺寸:
self.window_size = QApplication.desktop().availableGeometry(i).size() - QSize(10, 10)
顯示后設置大小:
class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setupUI() def setupUI(self): # Set screen size parameters for i in range(QApplication.desktop().screenCount()): self.window_size = QApplication.desktop().availableGeometry(i).size() self._button = QPushButton(self) self._button.setText("Test Me") self._editText = QComboBox(self) self._editText.setEditable(True) self._editText.addItem("") self._editText.setGeometry(QRect(240, 40, 113, 21)) # Connect signal to slot self._button.clicked.connect(self.toogleLogWindow) def init_geometry(self): self.resize(self.window_size) self.move(QPoint(0, 0)) def toogleLogWindow(self): if self._editText.currentText() == "0": h = self.window_size.height() w = int(self.window_size.width() / 2) self.resize(w, h) elif self._editText.currentText() == "1": h = self.window_size.height() w = int(self.window_size.width()) self.resize(w, h) else: pass def get_main_app(argv=[]): app = QApplication(argv) win = MainWindow() win.show() win.init_geometry() return app, win
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.