Python 为什么QFontDialog.getFont()总是返回相同的字体?
每次我使用Python 为什么QFontDialog.getFont()总是返回相同的字体?,python,macos,pyqt5,qfont,Python,Macos,Pyqt5,Qfont,每次我使用QFontDialog.getFont()获取字体时,它都会返回相同的QFont对象 font, ok = QFontDialog.getFont() print(font) if ok: self.lbl.setFont(font) print(self.lbl.font()) 上面是代码段,下面是结果(我做了不同的选择,但是QLabel的字体和我从getFont()获得的字体总是相同的) 只给出对象在内存中的位置,它基本上是一个存储单元编号,但是十六进制的。您可以在同一内
QFontDialog.getFont()
获取字体时,它都会返回相同的QFont
对象
font, ok = QFontDialog.getFont()
print(font)
if ok:
self.lbl.setFont(font)
print(self.lbl.font())
上面是代码段,下面是结果(我做了不同的选择,但是QLabel
的字体和我从getFont()
获得的字体总是相同的)
只给出对象在内存中的位置,它基本上是一个存储单元编号,但是十六进制的。您可以在同一内存空间中存储不同的对象(这里是不同的QFont对象)
通过执行以下操作,可以检查此处的QFont对象是否为您选择的对象:
font, ok = QFontDialog.getFont()
print(font.toString())
更新:你能试试下面的代码并告诉我们你得到了什么吗
更新:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Change Font')
self.button.clicked.connect(self.updateLabel)
self.label = QtWidgets.QLabel()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.updateLabel()
def updateLabel(self):
if self.label.text():
new = QtWidgets.QFontDialog.getFont()[0]
else:
new = QtGui.QFont(self.label.font())
before = self.label.font()
self.label.setFont(new)
after = self.label.font()
text = []
for font in before, after:
info = QtGui.QFontInfo(font)
text.append('BEFORE:' if font is before else 'AFTER:')
text.append(' string: %s' % font.toString())
text.append(' family: %s' % info.family())
text.append(' size: %s' % info.pointSize())
text.append('')
text = '\n'.join(text)
self.label.setText(text)
print(text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 500, 100)
window.show()
sys.exit(app.exec_())
这似乎是由一个只影响macOS的bug引起的:请参阅和。这应该在Qt-5.12中得到修复,因此您需要确保安装了Qt5和PyQt5的最新版本才能解决此问题
下面是一个测试脚本,其中包含我在Linux上使用Qt-5.12.0和PyQt-5.11.3获得的一些输出截图。正如您所看到的,一切都按预期进行。如果您得到不同的行为,您应该声明您正在使用的Qt和PyQt的确切版本,您在哪个平台上进行测试,并显示测试脚本的一些示例输出 测试脚本:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Change Font')
self.button.clicked.connect(self.updateLabel)
self.label = QtWidgets.QLabel()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.updateLabel()
def updateLabel(self):
if self.label.text():
new = QtWidgets.QFontDialog.getFont()[0]
else:
new = QtGui.QFont(self.label.font())
before = self.label.font()
self.label.setFont(new)
after = self.label.font()
text = []
for font in before, after:
info = QtGui.QFontInfo(font)
text.append('BEFORE:' if font is before else 'AFTER:')
text.append(' string: %s' % font.toString())
text.append(' family: %s' % info.family())
text.append(' size: %s' % info.pointSize())
text.append('')
text = '\n'.join(text)
self.label.setText(text)
print(text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 500, 100)
window.show()
sys.exit(app.exec_())
输出:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Change Font')
self.button.clicked.connect(self.updateLabel)
self.label = QtWidgets.QLabel()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.updateLabel()
def updateLabel(self):
if self.label.text():
new = QtWidgets.QFontDialog.getFont()[0]
else:
new = QtGui.QFont(self.label.font())
before = self.label.font()
self.label.setFont(new)
after = self.label.font()
text = []
for font in before, after:
info = QtGui.QFontInfo(font)
text.append('BEFORE:' if font is before else 'AFTER:')
text.append(' string: %s' % font.toString())
text.append(' family: %s' % info.family())
text.append(' size: %s' % info.pointSize())
text.append('')
text = '\n'.join(text)
self.label.setText(text)
print(text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 500, 100)
window.show()
sys.exit(app.exec_())
它在内存中的同一位置为您提供一个
QFont
对象。。打印(self.lbl.font().toString())的结果是什么?QFont
用于从系统请求字体。永远也不能保证你会得到你想要的东西。您应该始终使用QFontInfo
来获取有关系统提供的实际字体的信息。好吧,我使用了方法toString()来获取字体的描述,它会产生相同的结果:。SF NS Text,13,-1,5,50,0,0,0,0,0,0,0,0,0.SF NS Text,13,-1,5,50,0,0,0我使用了方法toString(),在if段之前和之后,我得到了相同的输出。@Yao,你为什么认为它应该不同?为什么这很重要?你是说标签不能正确呈现字体吗?@EKHUROMO是的。setFont()方法似乎无效,并且此标签的外观根本没有改变。我添加了一个使用各种字体的工作示例,你能试试吗?@Yao这一切都完全符合我的预期。请在我的答案中尝试测试脚本。我已经测试了您发布的代码,并用结果更新了此问题。我想我得到相同输出的原因可能是我的操作系统,但我不确定,因为我没有得到有关这方面的相关信息。@Yao请看我更新的答案。似乎有一个只影响macOS的bug。如果将qt和pyqt安装更新为最新版本,则应将其修复。(注:如果您觉得此答案有用,请单击上面的勾号将其标记为已接受)。谢谢您的帮助。我已经检查了QT和PyQt的版本。我正在使用PyQt(5.11.3)和QT(5.9.7)。然而,我的QT与Anaconda捆绑在一起,所以我担心如果我更新我的QT,那里的某些东西将无法正常工作。所以我不能检查这个答案,但现在我知道这是macOS的一个罕见问题。再次感谢您的测试代码和有关此错误的问题,我已将此答案标记为已接受。:)
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Change Font')
self.button.clicked.connect(self.updateLabel)
self.label = QtWidgets.QLabel()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.updateLabel()
def updateLabel(self):
if self.label.text():
new = QtWidgets.QFontDialog.getFont()[0]
else:
new = QtGui.QFont(self.label.font())
before = self.label.font()
self.label.setFont(new)
after = self.label.font()
text = []
for font in before, after:
info = QtGui.QFontInfo(font)
text.append('BEFORE:' if font is before else 'AFTER:')
text.append(' string: %s' % font.toString())
text.append(' family: %s' % info.family())
text.append(' size: %s' % info.pointSize())
text.append('')
text = '\n'.join(text)
self.label.setText(text)
print(text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 500, 100)
window.show()
sys.exit(app.exec_())