问题描述
我尝试了我能找到的最简单的 PyQt5 QWebView 示例,并尝试在 HTML 中显示 Monospace 字体。 QWebView 挺强大的,除了Monospace 字体,都是我想做的。
示例代码如下: `
#! /usr/bin/python3
#coding: latin-1
"""
https://github.com/baoboa/pyqt5/blob/master/examples/webkit/fancybrowser/fancybrowser.py
"""
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWebKitWidgets import QWebPage,QWebView
#from PyQt5.QtWebEngine import QWebEnginesettings
class MyWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setwindowTitle('Hello')
self.resize(768,576)
self.view = QWebView(self)
fpath = os.path.abspath("test.html")
self.view.load(QUrl.fromLocalFile(fpath))
self.view.loadFinished.connect(self.loaded)
self.layout = qgridLayout()
self.layout.setColumnStretch(1,1)
self.layout.addWidget(self.view,0)
self.setLayout(self.layout)
def loaded(self):
print("Loaded")
app = QApplication(sys.argv)
win = MyWidget()
win.show()
sys.exit(app.exec_())
```
这是我尝试过的 test.html,它可以在 Firefox 中使用,例如:
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="utf-8">
<title>title</title>
</head>
<body>
<p style="color:red">This is a paragraph.</p>
<p style="font-family:'Courier New'">This is another paragraph.</p>
</body>
</html>
有人知道我需要做什么才能使用任何 Monospace 字体吗?
我在 Linux、OpenSuse 15.3 上运行 PyQt5。
请找到显示行为的屏幕截图(在 Firefox 中可以,在 Chrome 中可以,在 PyQt5 WebView 中不可以): 第二段字体符合预期。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)