前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PyQt 适配不同屏幕分辨率

PyQt 适配不同屏幕分辨率

作者头像
AnRFDev
修改2021-02-02 10:00:10
4.1K0
修改2021-02-02 10:00:10
举报
文章被收录于专栏:AnRFDev

在宽高为1366*768下设计开发的界面,到了2860*1620屏幕下会显示不正常。

因为像素密度不同,2860*1620屏幕显示出来的控件很小。

适配方法 - 根据当前屏幕调整控件大小和位置

初始化时获取到当前屏幕的宽高像素值。

与原像素值相比求出比例self.ratio_widself.ratio_height

找出所有的QWidget self.findChildren(QWidget),遍历来改变大小和位置。

代码语言:txt
复制
from PyQt4.QtGui import QMainWindow, QApplication, QWidget

class ReMainWindow(QMainWindow):

    def __init__(self, parent=None):
        # ...........
        self.app = QApplication.instance()  # Calculate the ratio. Design screen is [1366, 768]
        screen_resolution = self.app.desktop().screenGeometry()
        self.hw_ratio = 768 / 1366  # height / width
        self.ratio_wid = screen_resolution.width() / 1366
        if self.ratio_wid < 1:
            self.ratio_wid = 1
        self.ratio_height = screen_resolution.height() / 768
        if self.ratio_height < 1:
            self.ratio_height = 1

    def _init_ui_size(self):
        """ Travel all the widgets and resize according to the ratio """
        self._resize_with_ratio(self)
        for q_widget in self.findChildren(QWidget):
            # print q_widget.objectName()
            self._resize_with_ratio(q_widget)
            self._move_with_ratio(q_widget)

            # Don't deal with the text browser
            # for q_widget in self.findChildren(QAbstractScrollArea):
            #     print q_widget.objectName()
            #     self._resize_with_ratio(q_widget)
            #     self._move_with_ratio(q_widget)

    def _resize_with_ratio(self, input_ui):
        input_ui.resize(input_ui.width() * self.ratio_wid, input_ui.height() * self.ratio_height)

    def _move_with_ratio(self, input_ui):
        input_ui.move(input_ui.x() * self.ratio_wid, input_ui.y() * self.ratio_height)

实践发现,不需要对QTextBrowser所属的QAbstractScrollArea处理。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 适配方法 - 根据当前屏幕调整控件大小和位置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档