我试图为我的程序制作漂亮的GUI,我也知道我也需要很酷的字体。因此,我安装了所有粗细的'Ubuntu'字体。
但是当我尝试这样做时:
class MenuButton(QPushButton):
def __init__(self, caption):
super().__init__()
self.setFixedHeight(60)
self.setFixedWidth(100)
self.setFlat(True)
font = QFont()
font.setFamily('Ubuntu')
font.setWeight(QFont.Bold)
font.setPixelSize(20)
self.setFont(font)
self.setStyleSheet("color: rgb(85,170,255);")
self.setText(caption)
我得到的字体肯定不是我所选择的:
但是,当我在应用程序开头召唤QFontDialog
时,我可以看到Ubuntu字体,它的名称为Ubuntu!
为什么它不起作用,我应该怎么做才能使其起作用?
感谢帮助...
仅此而已:
import sys
import _thread
from PyQt5.QtCore import QSize, Qt, QRect, QObject
from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QLabel, QTabWidget, QGridLayout, QVBoxLayout, \
QHBoxLayout, QSizePolicy, QSpacerItem, QStyle, QStyleFactory, QPushButton, QFrame, QFontDialog, QStackedWidget
from PyQt5.QtGui import QImage, QIcon, QPixmap, QPalette, QBrush, QColor, QFontDatabase, QFont
### Custom Classes
class MenuButton(QPushButton):
def __init__(self, caption):
super().__init__()
self.setFixedHeight(40)
self.setFixedWidth(100)
self.setFlat(True)
font = QFont()
font.setFamily('Ubuntu')
font.setWeight(QFont.Bold)
font.setPixelSize(19)
self.setFont(QFont('Ubuntu', 19, QFont.Medium))
self.setStyleSheet("color: rgb(85,170,255);")
self.setText(caption)
def select(self):
self.setStyleSheet("color: rgb(255,255,255);")
def deselect(self):
self.setStyleSheet("color: rgb(85,170,255);")
class StackPage(QWidget):
def __init__(self):
super().__init__()
self.setStyleSheet('background-color: rgb(28,33,39)')
self.layout = QVBoxLayout()
self.setLayout(self.layout)
class MainScreen(QWidget):
def __init__(self):
super().__init__()
#self.windowWidth = 500
#self.windowHeight = 300
self.icon = 'images\\system\\HashshashinLogo.png'
self.title = 'DevOrder'
self.fontFamily = 'Segoe UI' # Corbel | Sagoe UI
self.fontColor = 'black'
self.fontSize = '12'
self.initUI()
def initUI(self):
self.setStyleSheet('font-family: %s; color: %s; background-color: rgb(28,33,39);' % (self.fontFamily, self.fontColor))
self.setWindowTitle(self.title)
self.setWindowIcon(QIcon(self.icon))
#self.setFixedSize(self.windowWidth, self.windowHeight)
### Top Menu
def menubtnProjectsClicked():
menubtnProjects.select()
menubtnClients.deselect()
menubtnUpdates.deselect()
menubtnSettings.deselect()
self.leftStackMenu.setCurrentIndex(0)
def menubtnClientsClicked():
menubtnProjects.deselect()
menubtnClients.select()
menubtnUpdates.deselect()
menubtnSettings.deselect()
self.leftStackMenu.setCurrentIndex(1)
def menubtnUpdatesClicked():
menubtnProjects.deselect()
menubtnClients.deselect()
menubtnUpdates.select()
menubtnSettings.deselect()
self.leftStackMenu.setCurrentIndex(2)
def menubtnSettingsClicked():
menubtnProjects.deselect()
menubtnClients.deselect()
menubtnUpdates.deselect()
menubtnSettings.select()
self.leftStackMenu.setCurrentIndex(3)
menubtnProjects = MenuButton('Projects')
menubtnProjects.clicked.connect(menubtnProjectsClicked)
menubtnClients = MenuButton('Clients')
menubtnClients.clicked.connect(menubtnClientsClicked)
menubtnUpdates = MenuButton('Updates')
menubtnUpdates.clicked.connect(menubtnUpdatesClicked)
menubtnSettings = MenuButton('Settings')
menubtnSettings.clicked.connect(menubtnSettingsClicked)
### Left Menu
self.leftStackMenu = QStackedWidget()
self.leftStackMenu.setFixedWidth(500)
leftProjectsPage = StackPage()
leftProjectsPage.setStyleSheet('background-color: white;')
leftClientsPage = StackPage()
leftClientsPage.setStyleSheet('background-color: red;')
leftUpdatesPage = StackPage()
leftUpdatesPage.setStyleSheet('background-color: blue;')
leftSettingsPage = StackPage()
leftSettingsPage.setStyleSheet('background-color: yellow;')
self.leftStackMenu.addWidget(leftProjectsPage)
self.leftStackMenu.addWidget(leftClientsPage)
self.leftStackMenu.addWidget(leftUpdatesPage)
self.leftStackMenu.addWidget(leftSettingsPage)
### Placings
menuLayout = QHBoxLayout()
menuLayout.setContentsMargins(50, 10, 10, 10)
menuLayout.addWidget(menubtnProjects)
menuLayout.addWidget(menubtnClients)
menuLayout.addWidget(menubtnUpdates)
menuLayout.addWidget(menubtnSettings)
menuLayout.addSpacerItem(QSpacerItem(20, 40, QSizePolicy.Expanding, QSizePolicy.Minimum))
stacksLayout = QHBoxLayout()
stacksLayout.addWidget(self.leftStackMenu)
menuWidget = QWidget()
menuWidget.setStyleSheet('background-color: rgb(22,26,31);')
menuWidget.setLayout(menuLayout)
mainLayout = QVBoxLayout()
mainLayout.addWidget(menuWidget)
mainLayout.addLayout(stacksLayout)
mainLayout.addSpacerItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
mainLayout.setContentsMargins(0, 0, 0, 0)
self.setLayout(mainLayout)
#self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
#print('Currently used style:', app.style().metaObject().className())
#print('Available styles:', QStyleFactory.keys())
app.setStyle(QStyleFactory().create('windowsvista'))
QFontDialog().getFont()
loadingScreen = LoadingScren()
sys.exit(app.exec_())
答案 0 :(得分:0)
要解决您的问题,我们需要稍微更改您的代码。
首先,您在self.setFont(QtGui.QFont("Ubuntu", 20, QtGui.QFont.Bold))
下添加此行代码class MenuButton()
。
记住要显示主窗口,您可以通过添加以下代码来完成
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyle(QtWidgets.QStyleFactory().create('windowsvista'))
win = MainScreen()
win.show()
sys.exit(app.exec_())
启动后具有正确字体的窗口
代码
import sys
import _thread
from PyQt5 import QtCore, QtGui, QtWidgets
### Custom Classes
class MenuButton(QtWidgets.QPushButton):
def __init__(self, caption):
super(MenuButton, self).__init__()
self.setFlat(True)
self.setFont(QtGui.QFont("Ubuntu", 20, QtGui.QFont.Bold))
self.setStyleSheet("color: rgb(85,170,255);")
self.setText(caption)
def select(self):
self.setStyleSheet("color: rgb(255,255,255);")
def deselect(self):
self.setStyleSheet("color: rgb(85,170,255);")
class StackPage(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setStyleSheet('background-color: rgb(28,33,39)')
self.layout = QtWidgets.QVBoxLayout()
self.setLayout(self.layout)
class MainScreen(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MainScreen, self).__init__(parent)
#self.windowWidth = 500
#self.windowHeight = 300
self.icon = 'images\\system\\HashshashinLogo.png'
self.title = 'DevOrder'
self.fontFamily = 'Segoe UI' # Corbel | Sagoe UI
self.fontColor = 'black'
self.fontSize = '12'
self.initUI()
def initUI(self):
self.setStyleSheet('font-family: %s; color: %s; background-color: rgb(28,33,39);' % (self.fontFamily, self.fontColor))
self.setWindowTitle(self.title)
self.setWindowIcon(QtGui.QIcon(self.icon))
#self.setFixedSize(self.windowWidth, self.windowHeight)
### Top Menu
def menubtnProjectsClicked():
menubtnProjects.select()
menubtnClients.deselect()
menubtnUpdates.deselect()
menubtnSettings.deselect()
self.leftStackMenu.setCurrentIndex(0)
def menubtnClientsClicked():
menubtnProjects.deselect()
menubtnClients.select()
menubtnUpdates.deselect()
menubtnSettings.deselect()
self.leftStackMenu.setCurrentIndex(1)
def menubtnUpdatesClicked():
menubtnProjects.deselect()
menubtnClients.deselect()
menubtnUpdates.select()
menubtnSettings.deselect()
self.leftStackMenu.setCurrentIndex(2)
def menubtnSettingsClicked():
menubtnProjects.deselect()
menubtnClients.deselect()
menubtnUpdates.deselect()
menubtnSettings.select()
self.leftStackMenu.setCurrentIndex(3)
menubtnProjects = MenuButton('Projects')
menubtnProjects.clicked.connect(menubtnProjectsClicked)
menubtnClients = MenuButton('Clients')
menubtnClients.clicked.connect(menubtnClientsClicked)
menubtnUpdates = MenuButton('Updates')
menubtnUpdates.clicked.connect(menubtnUpdatesClicked)
menubtnSettings = MenuButton('Settings')
menubtnSettings.clicked.connect(menubtnSettingsClicked)
### Left Menu
self.leftStackMenu = QtWidgets.QStackedWidget()
self.leftStackMenu.setFixedWidth(500)
leftProjectsPage = StackPage()
leftProjectsPage.setStyleSheet('background-color: white;')
leftClientsPage = StackPage()
leftClientsPage.setStyleSheet('background-color: red;')
leftUpdatesPage = StackPage()
leftUpdatesPage.setStyleSheet('background-color: blue;')
leftSettingsPage = StackPage()
leftSettingsPage.setStyleSheet('background-color: yellow;')
self.leftStackMenu.addWidget(leftProjectsPage)
self.leftStackMenu.addWidget(leftClientsPage)
self.leftStackMenu.addWidget(leftUpdatesPage)
self.leftStackMenu.addWidget(leftSettingsPage)
### Placings
menuLayout = QtWidgets.QHBoxLayout()
menuLayout.setContentsMargins(50, 10, 10, 10)
menuLayout.addWidget(menubtnProjects)
menuLayout.addWidget(menubtnClients)
menuLayout.addWidget(menubtnUpdates)
menuLayout.addWidget(menubtnSettings)
menuLayout.addSpacerItem(QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum))
stacksLayout = QtWidgets.QHBoxLayout()
stacksLayout.addWidget(self.leftStackMenu)
menuWidget = QtWidgets.QWidget()
menuWidget.setStyleSheet('background-color: rgb(22,26,31);')
menuWidget.setLayout(menuLayout)
mainLayout = QtWidgets.QVBoxLayout()
mainLayout.addWidget(menuWidget)
mainLayout.addLayout(stacksLayout)
mainLayout.addSpacerItem(QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding))
mainLayout.setContentsMargins(0, 0, 0, 0)
self.setLayout(mainLayout)
#self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyle(QtWidgets.QStyleFactory().create('windowsvista'))
win = MainScreen()
win.show()
sys.exit(app.exec_())