PyQT5/PyQt linux/ubuntu 标题栏无法显示中文
解决方案
原因:系统缺少中文字库!
准备: 下载一个中文字库,例如 思源黑体 syht.ttf(非原名,手动重命名),放到桌面 ~/Desktop/syht.ttf
解决方法【无管理员权限】:
使用用户权限,在用户目录下补充中文字体,并刷新字体
cd ~/.local/share
mkdir fonts
cd fonts
mkdir myfonts
cp ~/Desktop/syht.ttf ./syht.ttf
mkfontscale
mkfontdir
fc-cache -fv
解决方法【有管理员权限】:
使用管理员权限,在系统目录下补充中文字体,并刷新字体
cd /usr/share/fonts
sudo mkdir myfonts
cd myfonts
sudo cp ~/Desktop/syht.ttf ./syht.ttf
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv
实测效果
测试代码同代码1,无需手动指定自定义字库【有美化需求除外】
未测试内容,但可获得的推论
如果用户字体库【包括用户目录字体库、系统目录字体库】如果不包含(未安装)某个中文字体,代码如同测试代码2手动指定了该字体,那么标题栏的字体仍然无法使用该字体,除标题栏外可以使用该字体。
如果对标题栏美化有执念,那么建议让用户安装相应的字体,或者编写一个安装脚本,自动给用户装上字体。
测试过程【危险操作,建议先备份】
准备
准备一个纯英文字库,例如 ~/Desktop/arial.ttf
准备一个中文字库,例如 ~/Desktop/syht.ttf
删除ubuntu系统字库
删除系统字体库
cd /usr/share/fonts
sudo rm -rf cmap
sudo rm -rf cMap
sudo rm -rf opentype
sudo rm -rf truetype
sudo rm -rf type1
sudo rm -rf X11
补充英文字库【不补充英文字库,系统会崩溃!】
sudo mkdir arial
cp ~/Desktop/arial.ttf ./arial
刷新字库
cd arial
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv
根据测试需求补充中文字库
补充中文字库方法见前文的两类解决方案。
测试代码1【系统无中文字库,未手动指定中文字库,全乱码】
代码
# -*- coding:UTF-8 -*-
from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtGui import QFontDatabase, QFont
import sys
import qtawesome
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi()
def setupUi(self):
widget = QtWidgets.QWidget(self)
lay = QtWidgets.QVBoxLayout(widget)
self.label = QtWidgets.QLabel('测试中ABCO0123LMNijkIJK'*2)
lay.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit('测试中ABCO0123LMNijkIJK'*2)
lay.addWidget(self.lineEdit)
self.setWindowTitle('测试中ABCO0123LMNijkIJK')
self.setWindowIcon(qtawesome.icon('fa.rocket'))
self.setCentralWidget(widget)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
mw = MyWindow()
mw.setWindowTitle(mw.tr('测试不行吗'))
mw.show()
sys.exit(app.exec_())
现象
测试代码2【系统无中文字库,手动指定中文字库,标题乱码】
代码
# -*- coding:UTF-8 -*-
from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtGui import QFontDatabase, QFont
import sys
import qtawesome
# import test_rc
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi()
def setupUi(self):
widget = QtWidgets.QWidget(self)
lay = QtWidgets.QVBoxLayout(widget)
self.label = QtWidgets.QLabel('测试中ABCO0123LMNijkIJK'*2)
lay.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit('测试中ABCO0123LMNijkIJK'*2)
lay.addWidget(self.lineEdit)
self.setWindowTitle('测试中ABCO0123LMNijkIJK')
self.setWindowIcon(qtawesome.icon('fa.rocket'))
self.setCentralWidget(widget)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
fontDb = QFontDatabase()
fontID = fontDb.addApplicationFont("siyuanheiti.ttf") # 此处的路径为qrc文件中的字体路径
# fontID = fontDb.addApplicationFont(":resources/siyuanheiti.ttf") # 此处的路径为qrc文件中的字体路径
fontFamilies = fontDb.applicationFontFamilies(fontID)
print(fontFamilies) # ['Source Han Sans CN Bold']
f = QFont('Source Han Sans CN Bold', 11, QtGui.QFont.Normal, False)
print(f.family())
app.setFont(f)
# app.setStyleSheet('font: 14pt "Source Han Sans CN Bold";')
mw = MyWindow()
mw.setWindowTitle(mw.tr('测试不行吗'))
mw.show()
sys.exit(app.exec_())