gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:26:23 28 4
gpt4 key购买 nike

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

以下是每个平台上的结果: enter image description here

使用此脚本获得:

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_( ))

这应该给出如下输出:

Output

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

关于python - Pyqt5 渲染在每个平台上都不同,我该怎么做才能使它们渲染相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54217686/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com