简体   繁体   English

如何在PyQt中设置小部件的初始大小大于2/3的屏幕

[英]How to set initial size of widget in PyQt larger than 2/3rds of screen

In the main window I have a central widget which has a natural size, and I would like to initialize it to this size. 在主窗口中,我有一个自然大小的中央小部件,我想将它初始化为这个大小。 However, I do not want it to be fixed to this size; 但是,我不希望它固定在这个尺寸上; the user should be able to shrink or expand it. 用户应该能够缩小或扩展它。

The Qt documentation states that: Qt文档指出:

Note: The size of top-level widgets are constrained to 2/3 of the desktop's height and width. 注意:顶级窗口小部件的大小限制为桌面高度和宽度的2/3。 You can resize() the widget manually if these bounds are inadequate. 如果这些边界不合适,您可以手动调整窗口小部件()。

But I am unable to use the resize method as prescribed. 但我无法按规定使用resize方法。

The following minimal example illustrates the problem: If width and height as given by w and h is less than 2/3 of that of the screen, then the window gets the expected size. 以下最小示例说明了问题:如果wh给出的宽度和高度小于屏幕宽度和高度的2/3,则窗口将获得预期的大小。 If they are greater, the window gets some truncated size. 如果它们更大,窗口会有一些截断的大小。

#!/usr/bin/env python
from PyQt4 import QtCore, QtGui
import sys
w = 1280; h = 720
app = QtGui.QApplication (sys.argv [1:])
frm = QtGui.QFrame ()
frm.sizeHint = lambda: QtCore.QSize (w, h)
win = QtGui.QMainWindow ()
win.setCentralWidget (frm)
win.show ()
sys.exit (app.exec_ ())

This should do it for you I think 我认为应该为你做这件事

from PyQt4 import QtCore, QtGui
import sys
w = 2280; h = 1520
app = QtGui.QApplication (sys.argv [1:])
frm = QtGui.QFrame ()
#frm.sizeHint = lambda: QtCore.QSize (w, h)
win = QtGui.QMainWindow ()
win.setCentralWidget (frm)
win.resize(w, h)
win.show ()
sys.exit (app.exec_ ())

The above answer is just fine, except that QApplication , QFrame and QMainWindow are not part of QtGui in the official PyQt5 . 上面的答案很好,除了QApplicationQFrameQMainWindow 不是官方PyQt5QtGui一部分 They are part of QtWidgets . 它们是QtWidgets的一部分。

from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame

w = 900; h = 600
app = QApplication (sys.argv [1:])
frm = QFrame ()
#frm.sizeHint = lambda: QtCore.QSize (w, h)
win = QMainWindow ()
win.setCentralWidget (frm)
win.resize(w, h)
win.show ()
sys.exit (app.exec_ ())

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

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