【问题标题】:PyQt4: printing Code 128 barcode fontsPyQt4:打印 Code 128 条码字体
【发布时间】:2018-04-21 07:22:37
【问题描述】:

我正在尝试在code128中编写文本,并且打印机也以图片结尾,但这两种方式都不起作用,我查看了官方文档和其他论坛问题,但无法打印代码128

from PyQt4.QtGui import QApplication,QImage,QFont
from PyQt4.QtCore import QSizeF
import sys,time
app = QApplication(sys.argv)
image = QImage(640,100,QImage.Format_Mono)
image.fill(1)
font = QFont('code128',32,QFont.Normal)
painter = QPainter()
painter.begin(image)
painter.setFont(font)
text = "\xccasdfsf\xce"
painter.drawText(10,90,text)
painter.end()
image.save('test.png')

还有我的热敏打印机

from PyQt4.QtGui import QApplication,QImage,QFont,QPrinter
from PyQt4.QtCore import QSizeF
import sys,time
app = QApplication(sys.argv)
printer = QPrinter('POS-58(copy of 1)')
font = QFont('code128',32,QFont.Normal)
painter = QPainter()
painter.begin(printer)
painter.setFont(font)
text = "\xccasdfsf\xce"
painter.drawText(10,90,text)
painter.end()

这两个都不适合我,我找到了解决方案,会发生什么?

实际输出是this

我在带有 python 2.7 的 windows 10 中使用 PyQt4

【问题讨论】:

  • “不起作用”和“在code128中无法打印”是什么意思?请描述实际打印的内容和/或提供显示输出的屏幕截图。您还应该说明您正在测试的平台。
  • PS:我使用 python-3.6.3、qt-4.8.7 和 pyqt-4.12.1 在我的 linux 系统上测试了第一个示例,它对我来说很好用。也就是说,它会生成一个左下角带有条形码的白色矩形图像。
  • QtGui.QFontInto(font).family() 的输出是什么?。看起来 Qt 没有找到正确的字体。你确定你安装正确了吗?
  • font = QFont('code128') info = QFontInfo(font) print(info.family()) 输出是 MS Shell Dlg 2
  • 所以字体没有正确安装。我添加了一个答案,可以让您解决问题。

标签: python qt printing pyqt code128


【解决方案1】:

从您的 cmets 中,很明显 Code 128 字体没有正确安装在您的系统上,因此它正在回退到 MS Shell Dlg 2 作为默认。

要准确查看系统上 Qt 可用的字体,您可以这样做:

>>> fd = QtGui.QFontDatabase()         
>>> len(fd.families())
209
>>> for f in sorted(fd.families()): print(f)
... 
Adobe Courier
Adobe Helvetica
Adobe New Century Schoolbook
Adobe Times
Adobe Utopia
Arabic Newspaper
B&H Lucida
B&H LucidaBright
B&H LucidaTypewriter
Bitstream Charter
Bitstream Terminal
Bitstream Vera Sans
Bitstream Vera Sans Mono
Bitstream Vera Serif
C059
Cantarell
Code 128
Code 128 low
Code 2 of 5 interleaved
Code 3 de 9
Code Datamatrix
Code EAN13
Code PDF417
D050000L
...

如果Code 128字体没有出现在这个列表中,那肯定是没有正确安装。网上有很多这种字体的免费版本(例如Free Barcode Font),所以我建议你尝试安装其中的一些。

如果这些字体仍然没有出现在 Qt 的字体系列列表中,您可以尝试显式添加它们,如下所示:

>>> QtGui.QFontDatabase.addApplicationFont('C:\\MyFonts\\code128.ttf')

要检查新安装的字体是否有效,请使用QFontInfo

>>> font = QtGui.QFont('Code 128')
>>> info = QtGui.QFontInfo(font)
>>> info.family()
'Code 128'

(注意:在这里检查 font.family() 是不够的,因为这只会返回请求的内容,而不是实际找到的内容)。

【讨论】:

  • 很高兴你得到了这个工作 - 最后的确切问题是什么? (PS:你知道你有投票的特权吗?请随时根据我的回答进行测试:)
猜你喜欢
  • 2013-03-26
  • 2014-04-22
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多