我在几台不同的 PC 上使用 PyQt5 应用程序 - 我注意到在 Windows 中时:
“桌面”->“显示设置”->“缩放和布局”->“更改文本、应用程序和其他项目的大小”
比例从 100% 增加 - 应用正在增加字体,但按钮大小和一般应用大小保持不变。
这几乎没有问题,菜单栏按钮开始隐藏,不同按钮中的文本没有完全显示,另外按钮开始覆盖自身。
是否有可能在更改 Windows 缩放比例时自动缩放整个应用程序或完全禁用该选项(当 Windows 更改缩放应用程序保持不变时)?
代码:
import sys
from PyQt5.QtWidgets import QGridLayout, QLabel, QComboBox, QPushButton, QWidget, QVBoxLayout, \
QApplication, QStyledItemDelegate, QAction, QMenuBar
from PyQt5 import QtGui
from PyQt5.QtCore import Qt
class AllClass(QWidget):
def __init__(self, parent=None):
super(AllClass, self).__init__(parent)
self.setFont(QtGui.QFont("Calibri", 9, QtGui.QFont.Normal, italic=False))
self.tmp_label = QLabel(text='tmp')
self.tmp_label.setMinimumHeight(20)
self.tmp_combo = QComboBox()
self.tmp_combo.setMinimumHeight(20)
self.tmp_combo.setMinimumWidth(130)
self.tmp_combo.setFont(QtGui.QFont("Calibri", 8))
self.tmp_combo.setStyleSheet("QAbstractItemView::item{margin: 2px;}")
self.tmp_combo.setItemDelegate(QStyledItemDelegate())
self.tmp_button = QPushButton()
self.tmp_button.setText('tmp button')
self.tmp_button.setMinimumWidth(80)
self.tmp_button.setMinimumHeight(19)
self.tmp_layout = QGridLayout()
self.tmp_layout.addWidget(self.tmp_label, 1, 1)
self.tmp_layout.addWidget(self.tmp_combo, 2, 1)
self.tmp_layout.addWidget(self.tmp_button, 3, 1)
self.tmp_layout.setVerticalSpacing(0)
self.tmp_layout.setHorizontalSpacing(2)
self.grid = QGridLayout()
self.grid.addLayout(self.tmp_layout, 1, 1)
self.grid.setAlignment(Qt.AlignTop | Qt.AlignLeft)
self.grid.setSpacing(0)
lay = QVBoxLayout(self)
lay.addLayout(self.grid)
lay.setSpacing(0)
lay.setAlignment(Qt.AlignTop)
class TmpApp(QWidget):
short_button = None
s_window = None
def __init__(self, parent=None):
super(TmpApp, self).__init__(parent)
self.menu = self.menu_bar()
self.Rwup = AllClass()
self.setWindowTitle('tmp app')
grid = QGridLayout(self)
grid.addWidget(self.menu, 1, 1)
grid.addWidget(self.Rwup, 2, 1)
grid.setSpacing(0)
grid.setContentsMargins(0, 0, 0, 0)
grid.setAlignment(Qt.AlignTop)
self.setMinimumSize(200, 100)
def menu_bar(self):
self.menu = QMenuBar()
self.short_button = QAction('SHORTCUTS')
self.menu.setFont(QtGui.QFont("Calibri", 8))
self.menu.addAction(self.short_button)
self.menu.setMinimumHeight(20)
self.short_button.triggered.connect(self.short_window)
return self.menu
def short_window(self):
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = TmpApp()
window.show()
app.exec_()
正如我写下的那样,我认为问题可能与使用固定尺寸有关,但使用最小尺寸可以创建与我预期不同的按钮尺寸。