简体   繁体   中英

How can I output an barcode to QLabel without saving it to a file?

Tell me how to output an image without saving it to a file? Maybe you can save it to RAM somehow? here is the form code:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>849</width>
    <height>677</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QLineEdit" name="editNum"/>
    </item>
    <item>
     <widget class="QLabel" name="label">
      <property name="text">
       <string/>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="btnGenerate">
      <property name="text">
       <string>Сгенерировать</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="btnPrint">
      <property name="text">
       <string>Печать</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

and this is the functionality:

import sys
import os
import barcode
from barcode.writer import ImageWriter

from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QMainWindow, QApplication 
from PyQt5.QtGui import QPixmap
from PyQt5 import uic
from PyQt5.QtCore import Qt


class BarcodeGenerate(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('barcode_design.ui', self)
        self.btnGenerate.clicked.connect(self.generate)
        self.show()
    
    def generate(self):
        self.eanNum = self.editNum.text()
        self.ean13 = barcode.get('ean13', self.eanNum, writer=ImageWriter())
        
        self.pixmap = QPixmap(self.ean13.save('barcode'))
        self.label.setPixmap(self.pixmap)

        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = BarcodeGenerate()
    sys.exit(app.exec())

I don't want to save the image every time and then upload it again. Is there a way to directly output to QLabel?

One possible solution is to use io.BytesIO() as an intermediary and use write() instead save() :

from io import BytesIO
import os
import sys

import barcode
from barcode.writer import ImageWriter

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPixmap
from PyQt5.uic import loadUi


class BarcodeGenerate(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi("barcode_design.ui", self)
        self.btnGenerate.clicked.connect(self.generate)

    def generate(self):
        value = self.editNum.text()
        ean13 = barcode.get("ean13", value, writer=ImageWriter())

        fp = BytesIO()
        ean13.write(fp)
        pixmap = QPixmap()
        pixmap.loadFromData(fp.getvalue())

        self.label.setPixmap(pixmap)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = BarcodeGenerate()
    window.show()
    sys.exit(app.exec())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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