简体   繁体   English

PyQt4:打印Code 128条码字体

[英]PyQt4: printing Code 128 barcode fonts

I'm trying to write a text in code128 and a printer also ends in a picture, but it does not work in either of the two ways, I looked at the official documentation and other forum questions but it does not print in code128 我正在尝试用code128编写文本,并且打印机也以图片结尾,但是它不能以两种方式工作,我查看了官方文档和其他论坛问题,但无法在code128中打印

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')

and also in my thermal printer 还有我的热敏打印机

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()

Neither of these two works for me and I find a solution for this, what could be happening? 这两本书都不适合我,我也找到了解决方案,这可能是怎么回事?

The actual output is this 实际的输出是这个 在此处输入图片说明

I am using PyQt4 in windows 10 with python 2.7 我在Windows 10和python 2.7中使用PyQt4

From your comments, it is clear that the Code 128 font has not been installed properly on your system, so it is falling back to MS Shell Dlg 2 as a default. 从您的注释中可以明显看出,您的系统上尚未正确安装Code 128字体,因此默认情况下,它会退回到MS Shell Dlg 2

To see exactly which fonts are available to Qt on your system, you can do this: 要确切了解系统上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
...

If the Code 128 font doesn't appear in this list, it has definitely not been installed properly. 如果Code 128字体未出现在此列表中,则表示它肯定没有正确安装。 There are plenty of free versions of this font available online (eg Free Barcode Font ), so I suggest you try installing some of those instead. 该字体有很多免费版本可在线使用(例如Free Barcode Font ),因此我建议您尝试安装其中的一些版本。

If these fonts still don't show up in Qt's list of font families, you can try adding them explicitly, like this: 如果这些字体仍未出现在Qt的字体系列列表中,则可以尝试显式添加它们,如下所示:

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

To check that the newly installed font is valid, use QFontInfo : 要检查新安装的字体是否有效,请使用QFontInfo

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

(NB: checking font.family() is not sufficient here, because that will only return what was requested , rather than what was actually found). (注意:在这里检查font.family()是不够的,因为这只会返回所请求的内容 ,而不是实际找到的内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM