Python 如何在 PyQt 中设置小部件的初始大小大于屏幕的 2/3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32017853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set initial size of widget in PyQt larger than 2/3rds of screen
提问by RolKau
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 documentationstates that:
在Qt文档指出:
Note: The size of top-level widgets are constrained to 2/3 of the desktop's height and width. You can resize() the widget manually if these bounds are inadequate.
注意:顶级小部件的大小限制为桌面高度和宽度的 2/3。如果这些边界不足,您可以手动调整小部件的大小()。
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. If they are greater, the window gets some truncated size.
以下最小示例说明了该问题:如果由w
和给出的宽度和高度h
小于屏幕宽度和高度的2/3,则窗口将获得预期大小。如果它们更大,则窗口会被截断。
#!/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_ ())
回答by Achayan
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_ ())
回答by Whitequill Riclo
The above answer is just fine, except that QApplication
, QFrame
and QMainWindow
are notpart of QtGui
in the official PyQt5. They are part of QtWidgets.
以上答案是蛮好的,只是QApplication
,QFrame
和QMainWindow
是不是一部分QtGui
在官方PyQt5。它们是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_ ())