PyQt5学习:Qtdesigner设计转换而来的界面.py文件与 2k等高分辨率屏幕不匹配或自适应问题,导致部分控件显示不完全解决办法

1.软件环境

Python3.7

PyQt5    5.13.0

2.问题和预览截图

如下图1,在把Qtdesigner设计的界面.ui文件转换为.py文件再调用时按钮的文字显示不完全,而且界面有比在Qtdesigner预览时小了很多。(注:我测试的是Mainwindow窗口类型,Widget的Form窗口类型解决办法也一样)

PyQt5学习:Qtdesigner设计转换而来的界面.py文件与 2k等高分辨率屏幕不匹配或自适应问题,导致部分控件显示不完全解决办法_第1张图片

                             (图1)

测试代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui3.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(320, 240)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 50, 56, 17))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(180, 50, 56, 17))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 18))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton"))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainwindow = QMainWindow()
    ui3 = Ui_MainWindow()
    ui3.setupUi(mainwindow)
    mainwindow.show()
    sys.exit(app.exec_())

下面的图2是在Qtdesigner预览的图,和图1比,说明了确实存在与 2k等高分辨率屏幕不匹配或自适应问题,导致部分控件显示不完全

PyQt5学习:Qtdesigner设计转换而来的界面.py文件与 2k等高分辨率屏幕不匹配或自适应问题,导致部分控件显示不完全解决办法_第2张图片

                                   (图2)

3.解决方法

在app = QApplication(sys.argv) 语句前加一句
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)就行。

注:(自行测试时要注意头部是否import了QtCore)(QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)一定要在app = QApplication(sys.argv)前面,否则不成功)

解决后代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui3.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(320, 240)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 50, 56, 17))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(180, 50, 56, 17))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 18))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton"))

if __name__ == '__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    mainwindow = QMainWindow()
    ui3 = Ui_MainWindow()
    ui3.setupUi(mainwindow)
    mainwindow.show()
    sys.exit(app.exec_())

运行后截图如下图3,按钮控件已经正常显示了。

PyQt5学习:Qtdesigner设计转换而来的界面.py文件与 2k等高分辨率屏幕不匹配或自适应问题,导致部分控件显示不完全解决办法_第3张图片

                                                   (图3)

 

你可能感兴趣的:(python随笔,PyQt5随笔)