窗口标题未按每个监视器DPI缩放PyQt5

2024-06-28 11:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我运行的是Windows10,使用的是PyQt5(python3.6,来自anaconda的Qt5.6)。我有两个显示器,一个是100%的高清显示器,另一个是150%的4K显示器。下面是一个简单的GUI示例。除标题栏外,所有内容都按比例缩放。不知道我该怎么解决。请参见下面这两种分辨率的图像。在

import sys

from PyQt5 import QtGui, QtCore, QtWidgets
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.setWindowTitle("Hello World")
fileMenu = QtWidgets.QMenu("File", window)
longMenu = QtWidgets.QMenu("Long Menu Title", window)
window.menuBar().addMenu(fileMenu)
window.menuBar().addMenu(longMenu)
window.show()

sys.exit(app.exec_())

enter image description here


回答如下:

设置窗口.font()属性根据下面的答案更改菜单字体,但不影响窗口标题栏字体。以下代码将在下图中生成结果:

^{pr2}$

enter image description here


Tags: importappsyswindow显示器pyqt5qapplication标题栏
1条回答
网友
1楼 · 发布于 2024-06-28 11:38:33

应该使用font.setPointSize(x)完成,其中x=12,如下面的代码所示。另请参见内联注释。在

import sys

from PyQt5 import QtGui, QtCore, QtWidgets
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.setWindowTitle("Hello World")

fileMenu = QtWidgets.QMenu("File", window)
longMenu = QtWidgets.QMenu("Long Menu Title", window)
window.menuBar().addMenu(fileMenu)
window.menuBar().addMenu(longMenu)

font = window.font()
font.setPointSize(12)
window.setFont(font)     # set font here
window.setMinimumSize(QtCore.QSize(400, 200)) # gives flexible window size with minimum size.
#window.resize(400, 200)  # fixed size: in case you're using fontsize 26 or so ;-)

window.show()

sys.exit(app.exec_())

相关问题 更多 >