0

I'm using this python module called code128 that generates images of barcodes in code 128 barcode format.

The module works fine alone and produces the barcode desired. But now I'm trying to integrate it with tkinter and get tkinter to display it in a window but with no luck.

Tkinter just shows a black rectangle that would otherwise be the whole barcode. I'm not at all familiar with tkinter, but managed to borrow some functional code, but for some reason this barcode thing isn't working.

import tkinter as tk
from PIL import ImageTk, Image
import code128

#This creates the main window of an application
window = tk.Tk()
window.title("barcode test")
window.geometry("800x600")
#window.configure(background='grey')

#create a barcode image
barcode = code128.image('EL123456789US')

#barcode.show()  #for testing purposes.  the barcode is generated

#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(barcode)

#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
label = tk.Label(window, image = img)
label.image = img #keep a reference!

#The Pack geometry manager packs widgets in rows or columns.
label.pack(side = "bottom", fill = "both", expand = "yes")

#Start the GUI
window.mainloop()

I could just save the barcode to disk and load it from disk (it does work as I've already tried it) but I'm trying to avoid that as I need to display many barcodes one after another. Saving and reading from disk would be too costly.

Any help would be greatly appreciated. Thank you.

suboba
  • 1

0 Answers0