如何根据PyQt5中的Contents获取Frame的Minimum Size/Fixed Size?
How to obtain the Minimum Size/Fixed Size of the frame based on the Contents in PyQt5?
我想根据框架的内容将框架大小设置为 Minimum/Fixed。例如,在我的代码中,我通过手动(第 47 行)设置框架大小值,而不是这个,如何通过代码本身 calculate/obtain 框架的大小?
import sys, os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
def stylesheet_country_63():
return """
QLabel{
font-family: Trebuchet MS; font-style: normal; font-size:10pt; font-weight:400;
color:Black; padding:6px;
qproperty-alignment: 'AlignVCenter | AlignRight';
min-width:170px;
}
"""
class AssistScriptClassCountry63(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Country")
self.widgets_country_63()
self.layout_country_63()
def widgets_country_63(self):
self.lbl_country63_name = QLabel("Country Name :")
self.lbl_country63_isdcode = QLabel("Isd Code :")
self.lbl_country63_currency = QLabel("Currency :")
self.lbl_country63_curascii = QLabel("Currency Symbol :\n (Ascii Value) ")
self.lbl_country63_curicon = QLabel(" Currency Symbol :")
self.lbl_country63_name.setStyleSheet(stylesheet_country_63())
self.lbl_country63_isdcode.setStyleSheet(stylesheet_country_63())
self.lbl_country63_currency.setStyleSheet(stylesheet_country_63())
self.lbl_country63_curascii.setStyleSheet(stylesheet_country_63())
self.lbl_country63_curicon.setStyleSheet(stylesheet_country_63())
self.tbx_country63_name = QLineEdit()
self.tbx_country63_isdcode = QLineEdit()
self.tbx_country63_currency = QLineEdit()
self.tbx_country63_curascii = QLineEdit()
self.tbx_country63_curicon = QLineEdit()
def layout_country_63(self):
self.layout_country63_outerframe = QHBoxLayout()
self.frame_country63_innerframe = QFrame()
self.frame_country63_innerframe.setStyleSheet("background-color:lightgreen")
self.layout_country63_innerframe = QFormLayout(self.frame_country63_innerframe)
self.layout_country63_innerframe.addRow(self.lbl_country63_name, self.tbx_country63_name)
self.layout_country63_innerframe.addRow(self.lbl_country63_isdcode, self.tbx_country63_isdcode)
self.layout_country63_innerframe.addRow(self.lbl_country63_currency, self.tbx_country63_currency)
self.layout_country63_innerframe.addRow(self.lbl_country63_curascii, self.tbx_country63_curascii)
self.layout_country63_innerframe.addRow(self.lbl_country63_curicon, self.tbx_country63_curicon)
self.frame_country63_innerframe.setFixedSize(300,180)
self.layout_country63_outerframe.addWidget(self.frame_country63_innerframe)
self.setLayout(self.layout_country63_outerframe)
if __name__ == "__main__":
app = QApplication(sys.argv)
countrywin = AssistScriptClassCountry63()
# qApp.setStyleSheet(stylesheet_country_63())
countrywin.show()
sys.exit(app.exec_())
使用小部件的sizeHint()
:
This property holds the recommended size for the widget [...]
The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.
(强调我的)
布局的首选大小是该布局管理的所有子窗口小部件(和布局)的首选大小。您将获得基于各种尺寸限制(minimum/maximum/fixed 尺寸)、每个子项的提示和策略的计算尺寸。
hint = self.frame_country63_innerframe.sizeHint()
self.frame_country63_innerframe.setMinimumSize(hint)
注意:您可以设置最小尺寸 或 固定尺寸,同时尝试两者都是没有意义的:设置固定尺寸实际上会导致设置 both minimum and maximum size 为相同的值。
我想根据框架的内容将框架大小设置为 Minimum/Fixed。例如,在我的代码中,我通过手动(第 47 行)设置框架大小值,而不是这个,如何通过代码本身 calculate/obtain 框架的大小?
import sys, os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
def stylesheet_country_63():
return """
QLabel{
font-family: Trebuchet MS; font-style: normal; font-size:10pt; font-weight:400;
color:Black; padding:6px;
qproperty-alignment: 'AlignVCenter | AlignRight';
min-width:170px;
}
"""
class AssistScriptClassCountry63(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Country")
self.widgets_country_63()
self.layout_country_63()
def widgets_country_63(self):
self.lbl_country63_name = QLabel("Country Name :")
self.lbl_country63_isdcode = QLabel("Isd Code :")
self.lbl_country63_currency = QLabel("Currency :")
self.lbl_country63_curascii = QLabel("Currency Symbol :\n (Ascii Value) ")
self.lbl_country63_curicon = QLabel(" Currency Symbol :")
self.lbl_country63_name.setStyleSheet(stylesheet_country_63())
self.lbl_country63_isdcode.setStyleSheet(stylesheet_country_63())
self.lbl_country63_currency.setStyleSheet(stylesheet_country_63())
self.lbl_country63_curascii.setStyleSheet(stylesheet_country_63())
self.lbl_country63_curicon.setStyleSheet(stylesheet_country_63())
self.tbx_country63_name = QLineEdit()
self.tbx_country63_isdcode = QLineEdit()
self.tbx_country63_currency = QLineEdit()
self.tbx_country63_curascii = QLineEdit()
self.tbx_country63_curicon = QLineEdit()
def layout_country_63(self):
self.layout_country63_outerframe = QHBoxLayout()
self.frame_country63_innerframe = QFrame()
self.frame_country63_innerframe.setStyleSheet("background-color:lightgreen")
self.layout_country63_innerframe = QFormLayout(self.frame_country63_innerframe)
self.layout_country63_innerframe.addRow(self.lbl_country63_name, self.tbx_country63_name)
self.layout_country63_innerframe.addRow(self.lbl_country63_isdcode, self.tbx_country63_isdcode)
self.layout_country63_innerframe.addRow(self.lbl_country63_currency, self.tbx_country63_currency)
self.layout_country63_innerframe.addRow(self.lbl_country63_curascii, self.tbx_country63_curascii)
self.layout_country63_innerframe.addRow(self.lbl_country63_curicon, self.tbx_country63_curicon)
self.frame_country63_innerframe.setFixedSize(300,180)
self.layout_country63_outerframe.addWidget(self.frame_country63_innerframe)
self.setLayout(self.layout_country63_outerframe)
if __name__ == "__main__":
app = QApplication(sys.argv)
countrywin = AssistScriptClassCountry63()
# qApp.setStyleSheet(stylesheet_country_63())
countrywin.show()
sys.exit(app.exec_())
使用小部件的sizeHint()
:
This property holds the recommended size for the widget [...]
The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.
(强调我的)
布局的首选大小是该布局管理的所有子窗口小部件(和布局)的首选大小。您将获得基于各种尺寸限制(minimum/maximum/fixed 尺寸)、每个子项的提示和策略的计算尺寸。
hint = self.frame_country63_innerframe.sizeHint()
self.frame_country63_innerframe.setMinimumSize(hint)
注意:您可以设置最小尺寸 或 固定尺寸,同时尝试两者都是没有意义的:设置固定尺寸实际上会导致设置 both minimum and maximum size 为相同的值。