K.M*_*ier 3 python pyqt pyqt4 python-3.x pyqt5
我正在PyQt4(并迁移到PyQt5)中编写 GUI 。这是我启动 GUI 的方式:
if __name__== '__main__':
app = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Fusion')) # <- Choose the style
myGUI = MyMainWindow("First GUI")
app.exec_()
Run Code Online (Sandbox Code Playgroud)
PyQt4 中的默认样式:
显然,PyQt4具有以下样式:
'Windows''WindowsXP''WindowsVista''Motif''CDE''Plastique''Cleanlooks'PyQt5 中的默认样式:
PyQt5 有以下样式:
'Windows''WindowsXP''WindowsVista''Fusion'自定义样式?
这些样式都没有对 HiDpi 显示器(4k 等)的适当支持。例如,滚动条太小(参见这篇文章:如何从 PyQt 中的 QTextEdit 调整滚动条的大小?)。我什至没有提到那些视力不清晰的人的问题..
你知道一种为 4k 显示器或视力有问题的人提供良好支持的风格(最好是开源的)吗?
如果是这样,如何下载并安装这种样式?
非常感谢。
我通过另一个问题得到了答案(或者说是一种解决方法): 如何使 QMenu 中的图标更大 (PyQt)?
创建新的最直接的方法QStyle是从现有的派生。PyQtQProxyStyle为此目的提供了该类。下面是我在如何使 QMenu 中的图标更大 (PyQt) 中给出的示例?题。在这个例子中,QStyle创建了一个自定义(派生自“Fusion”风格),自定义风格为QMenu.
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
# Create a custom "QProxyStyle" to enlarge the QMenu icons
#-----------------------------------------------------------
class MyProxyStyle(QProxyStyle):
pass
def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):
if QStyle_PixelMetric == QStyle.PM_SmallIconSize:
return 40
else:
return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
# This is the main window class (with a simple QMenu implemented)
# ------------------------------------------------------------------
class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()
# 1. Set basic geometry and color.
self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200, 200, 200))
self.setPalette(palette)
# 2. Create the central frame.
self.centralFrame = QFrame()
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.setCentralWidget(self.centralFrame)
# 3. Create a menu bar.
myMenuBar = self.menuBar()
fileMenu = myMenuBar.addMenu("&File")
testMenuItem = QAction(QIcon("C:\\my\\path\\myFig.png"), "&Test", self)
testMenuItem.setStatusTip("Test for icon size")
testMenuItem.triggered.connect(lambda: print("Menu item has been clicked!"))
fileMenu.addAction(testMenuItem)
# 4. Show the window.
self.show()
# Start your Qt application based on the new style
#---------------------------------------------------
if __name__== '__main__':
app = QApplication(sys.argv)
myStyle = MyProxyStyle('Fusion') # The proxy style should be based on an existing style,
# like 'Windows', 'Motif', 'Plastique', 'Fusion', ...
app.setStyle(myStyle)
myGUI = TestWindow()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
只需复制粘贴代码片段,然后将其粘贴到 *.py 文件中。当然,您应该将图标的路径替换为本地计算机上的有效路径。只需提供完整路径(“C:...”)即可 100% 确保 Qt 找到图标绘图。
尝试一下,您应该会看到以下窗口: