简体   繁体   中英

I can't figure out why my python script works but when try to make it an executable it does not work

I'm having an issue with running a script as an executable. I believe it has to do with MEIXXXXX but am not sure how to remedy it. The script below runs perfectly when run in python 3.10.5 but when I create an executable the file won't save or create.

import os
from tkinter import Button, Entry, Label, Tk

import barcode
from barcode.writer import ImageWriter

root = Tk()
root.minsize(400, 200)

# Add Title from Filename

title = os.path.basename(__file__)[0:-3]
root.title(title.title())

# Get the path of the running file

fp = os.path.dirname(__file__)
os.chdir(fp)


def calculated(event=None):
    # Define content of the barcode as a string
    # Get entry as an input for the barcode 11 char or 12 if you know the checksum digit.
    number = e1.get()
    # calulate the checksum and store the full barcode in the variable 'cs'
    cs = barcode.UPCA(number).get_fullcode()
    # print(cs)
    # Get the required barcode format
    barcode_format = barcode.get_barcode_class("upc")

    # Generate barcode and render as image
    my_barcode = barcode_format(number, writer=ImageWriter())

    # Save barcode as PNG

    my_barcode.save(cs)


lbl1 = Label(root, text="Enter UPC-A code : ")
lbl1.grid(column=0, row=0)
e1 = Entry(root)
e1.grid(column=1, row=0, ipadx=5, padx=5, pady=5)
btn1 = Button(root, text="Calculate", command=calculated)
btn1.grid(column=2, row=0)
root.bind('<Return>', calculated)
root.mainloop()

These are the steps I took to make it work.

  1. mkdir newdir and cd newdir
  2. py -m venv venv and venv\Scripts\activate
  3. py -m pip install --upgrade pip pyinstaller pillow python-barcode
  4. touch main.py

main.py

import os
from tkinter import Button, Entry, Label, Tk
import barcode
from barcode.writer import ImageWriter

root = Tk()
root.minsize(400, 200)

# Add Title from Filename

title = os.path.basename(__file__)[0:-3]
root.title(title.title())


def calculated(event=None):
    # Define content of the barcode as a string
    # Get entry as an input for the barcode 11 char or 12 if you know the checksum digit.
    number = e1.get()
    # calulate the checksum and store the full barcode in the variable 'cs'
    cs = barcode.UPCA(number).get_fullcode()
    # print(cs)
    # Get the required barcode format
    barcode_format = barcode.get_barcode_class("upc")

    # Generate barcode and render as image
    my_barcode = barcode_format(number, writer=ImageWriter())

    # Save barcode as PNG
    path = os.path.join(os.getcwd(),cs)  # <--- added this
    my_barcode.save(path)


lbl1 = Label(root, text="Enter UPC-A code : ")
lbl1.grid(column=0, row=0)
e1 = Entry(root)
e1.grid(column=1, row=0, ipadx=5, padx=5, pady=5)
btn1 = Button(root, text="Calculate", command=calculated)
btn1.grid(column=2, row=0)
root.bind('<Return>', calculated)
root.mainloop()

I made some very minor alterations to make sure the images were written to the users current directory instead of writing them to the temporary runtime folder.

  1. pyinstaller -F main.py
  2. inside main.spec add a tuple containing the path to the barcode fonts folder and the target path for app runtime.

main.spec

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('./venv/Lib/site-packages/barcode/fonts', './barcode/fonts')], # add this
    ...
  1. pyinstaller main.spec
  2. dist\main.exe

And that's it.

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