Pyqt5 渲染在每个平台上都不同,我该怎么做才能使它们渲染相同?

Pyqt5 rendering is different on each platform, what can I do to render them the same?

我在 pyqt5 开发方面遇到了一些问题,因为我正在尝试制作一个应该在每个平台(Windows、MacOS 和 Linux)上使用的软件。 当我在每个平台上执行相同的脚本时,我得到不同的渲染结果。 这似乎主要是由于每个平台上的文本字体和大小不同。例如,底部的按钮不能包含 MacOS 和 Linux 平台的文本。一些物品的尺寸也不同(见底部的按钮) 我想知道他们是否是一个简单的解决方案来呈现 window 在每个平台上几乎相同?

以下是每个平台上的结果:

通过此脚本获得:

from PyQt5.QtWidgets import *
import sys 

class GUI(QMainWindow):
    def __init__(self, parent=None):
        super(GUI, self).__init__()
        self.parent = parent
        self.setFixedWidth(400)
        self.setFixedHeight(300)

        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QVBoxLayout()
        self.setmarginandspacing(self.mainHBOX_param_scene)

        self.Ident_apply = QPushButton('Blablabla')

        self.GB1 = QGroupBox("Identification")
        self.GB1_layout = QHBoxLayout()
        self.Param_0_l = QLabel('Delay')
        self.Param_0_e = QLineEdit('1')
        self.GB1_layout.addWidget(self.Param_0_l)
        self.GB1_layout.addWidget(self.Param_0_e)
        self.GB1.setLayout(self.GB1_layout)


        self.GB2 = QGroupBox("Identification")
        self.GB2_layout = QHBoxLayout()
        grid = QGridLayout()
        self.GB2_layout.addLayout(grid)

        self.RB_Ident = QButtonGroup()
        self.RB_DL  =QRadioButton('DoubleLayer')
        self.RB_CPE= QRadioButton('CPE')
        self.RB_Ident.addButton(self.RB_DL)
        self.RB_Ident.addButton(self.RB_CPE)
        self.RB_DL.setChecked(True)
        self.Fixed_delay  = QCheckBox('Fixed delay?')
        self.Fixed_delay.setChecked(False)

        self.Param_0_l = QLabel('Delay')
        self.Param_0_e = QLineEdit('1')
        self.Param_1_l = QLabel('I(Rs+2*Rcable)')
        self.Param_1_e = QLineEdit('1')
        self.Param_2_l = QLabel('2*I*Zf')
        self.Param_2_e = QLineEdit('1')
        self.Param_3_l = QLabel('Cdl*Zf')
        self.Param_3_e = QLineEdit('1')


        self.Ident_apply = QPushButton('Apply')
        self.Ident_updatepguess = QPushButton('Update Pguess')

        self.Optim_Ident = QButtonGroup()
        self.Optim_curve_fit  =QRadioButton('Curve fit')
        self.Optim_minimize= QRadioButton('minimize')
        self.Optim_basin_hopping= QRadioButton('basin hopping')
        self.Optim_Ident.addButton(self.Optim_curve_fit)
        self.Optim_Ident.addButton(self.Optim_minimize)
        self.Optim_Ident.addButton(self.Optim_basin_hopping)
        self.Optim_curve_fit.setChecked(True)

        grid.addWidget(self.RB_DL, 0, 0 )
        grid.addWidget(self.RB_CPE, 0, 1 )
        grid.addWidget(self.Fixed_delay, 0, 3 )
        grid.addWidget(self.Param_0_l, 1, 0 )
        grid.addWidget(self.Param_1_l, 1, 1 )
        grid.addWidget(self.Param_2_l, 1, 2 )
        grid.addWidget(self.Param_3_l, 1, 3 )
        grid.addWidget(self.Param_0_e, 2, 0 )
        grid.addWidget(self.Param_1_e, 2, 1 )
        grid.addWidget(self.Param_2_e, 2, 2 )
        grid.addWidget(self.Param_3_e, 2, 3 )
        grid.addWidget(self.Optim_curve_fit, 3, 0 )
        grid.addWidget(self.Optim_minimize, 3, 1 )
        grid.addWidget(self.Optim_basin_hopping, 3, 2 )
        grid.addWidget(self.Ident_updatepguess, 4, 0)
        grid.addWidget(self.Ident_apply, 4, 3, )
        self.GB2.setLayout(self.GB2_layout)

        self.mainHBOX_param_scene.addWidget(self.Ident_apply)
        self.mainHBOX_param_scene.addWidget(self.GB1)
        self.mainHBOX_param_scene.addWidget(self.GB2)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

    def setmarginandspacing(self,layout):
        layout.setContentsMargins(5,5,5,5)
        layout.setSpacing(5)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = GUI(app)
    ex.show()
    ex.setWindowTitle('Windows')
    sys.exit(app.exec_( ))

如果你想让它在每个平台上都呈现相同的效果,我建议你在开始时设置样式。给自己一个融合风格的代码,你的代码可能是这样的:

from PyQt5.QtWidgets import *
app = QApplication([])
app.setStyle('Fusion')
#Your code ...

下面列出了您可以选择的不同样式:

'Windows', 'GTK+', 'Fusion'

Windows 选项将使您的代码在 windows 上看起来像 运行,在 GTK+ 平台上看起来像 GTK+,一个 Fusion 它自己独特的一.

Windows 外观的代码(使用您的代码)如下所示:

from PyQt5.QtWidgets import *
import sys 

class GUI(QMainWindow):
    def __init__(self, parent=None):
        super(GUI, self).__init__()
        self.parent = parent
        self.setFixedWidth(400)
        self.setFixedHeight(300)

        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QVBoxLayout()
        self.setmarginandspacing(self.mainHBOX_param_scene)

        self.Ident_apply = QPushButton('Blablabla')

        self.GB1 = QGroupBox("Identification")
        self.GB1_layout = QHBoxLayout()
        self.Param_0_l = QLabel('Delay')
        self.Param_0_e = QLineEdit('1')
        self.GB1_layout.addWidget(self.Param_0_l)
        self.GB1_layout.addWidget(self.Param_0_e)
        self.GB1.setLayout(self.GB1_layout)


        self.GB2 = QGroupBox("Identification")
        self.GB2_layout = QHBoxLayout()
        grid = QGridLayout()
        self.GB2_layout.addLayout(grid)

        self.RB_Ident = QButtonGroup()
        self.RB_DL  =QRadioButton('DoubleLayer')
        self.RB_CPE= QRadioButton('CPE')
        self.RB_Ident.addButton(self.RB_DL)
        self.RB_Ident.addButton(self.RB_CPE)
        self.RB_DL.setChecked(True)
        self.Fixed_delay  = QCheckBox('Fixed delay?')
        self.Fixed_delay.setChecked(False)

        self.Param_0_l = QLabel('Delay')
        self.Param_0_e = QLineEdit('1')
        self.Param_1_l = QLabel('I(Rs+2*Rcable)')
        self.Param_1_e = QLineEdit('1')
        self.Param_2_l = QLabel('2*I*Zf')
        self.Param_2_e = QLineEdit('1')
        self.Param_3_l = QLabel('Cdl*Zf')
        self.Param_3_e = QLineEdit('1')


        self.Ident_apply = QPushButton('Apply')
        self.Ident_updatepguess = QPushButton('Update Pguess')

        self.Optim_Ident = QButtonGroup()
        self.Optim_curve_fit  =QRadioButton('Curve fit')
        self.Optim_minimize= QRadioButton('minimize')
        self.Optim_basin_hopping= QRadioButton('basin hopping')
        self.Optim_Ident.addButton(self.Optim_curve_fit)
        self.Optim_Ident.addButton(self.Optim_minimize)
        self.Optim_Ident.addButton(self.Optim_basin_hopping)
        self.Optim_curve_fit.setChecked(True)

        grid.addWidget(self.RB_DL, 0, 0 )
        grid.addWidget(self.RB_CPE, 0, 1 )
        grid.addWidget(self.Fixed_delay, 0, 3 )
        grid.addWidget(self.Param_0_l, 1, 0 )
        grid.addWidget(self.Param_1_l, 1, 1 )
        grid.addWidget(self.Param_2_l, 1, 2 )
        grid.addWidget(self.Param_3_l, 1, 3 )
        grid.addWidget(self.Param_0_e, 2, 0 )
        grid.addWidget(self.Param_1_e, 2, 1 )
        grid.addWidget(self.Param_2_e, 2, 2 )
        grid.addWidget(self.Param_3_e, 2, 3 )
        grid.addWidget(self.Optim_curve_fit, 3, 0 )
        grid.addWidget(self.Optim_minimize, 3, 1 )
        grid.addWidget(self.Optim_basin_hopping, 3, 2 )
        grid.addWidget(self.Ident_updatepguess, 4, 0)
        grid.addWidget(self.Ident_apply, 4, 3, )
        self.GB2.setLayout(self.GB2_layout)

        self.mainHBOX_param_scene.addWidget(self.Ident_apply)
        self.mainHBOX_param_scene.addWidget(self.GB1)
        self.mainHBOX_param_scene.addWidget(self.GB2)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

    def setmarginandspacing(self,layout):
        layout.setContentsMargins(5,5,5,5)
        layout.setSpacing(5)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle('Windows')
    ex = GUI(app)
    ex.show()
    ex.setWindowTitle('Windows style')
    sys.exit(app.exec_( ))

这应该给出这样的输出:

另一种方法是获取 .qss 样式表,或制作您自己的样式表。 Here 是关于制作 .qss 样式表的简短教程。