简体   繁体   English

我无法弄清楚为什么我的 python 脚本可以运行,但是当尝试使其成为可执行文件时它不起作用

[英]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.我相信它与 MEIXXXXX 有关,但我不确定如何补救。 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.下面的脚本在 python 3.10.5 中运行时运行完美,但是当我创建可执行文件时,该文件不会保存或创建。

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 mkdir newdircd newdir
  2. py -m venv venv and venv\Scripts\activate 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添加一个元组,其中包含条形码 fonts 文件夹的路径和应用程序运行时的目标路径。

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.就是这样。

暂无
暂无

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

相关问题 尝试进行API调用时,为什么我的python脚本停止执行? - Why does my python script stop executing when I try to make my API call? 为什么我不能从我的 Python 脚本创建可执行文件? - Why can't I create an executable from my Python script? 我正在尝试扫描我的网站以检查哪些端口是打开的。 我只是不明白为什么在使用循环时这个逻辑不起作用 - I'm trying to scan my website to check which ports are open. I just can't figure out why this logic does not work when using loops Python - 无法弄清楚为什么正则表达式在这些代码中有效和无效? - Python - Can't figure Out Why Regex Does and Doesn't Work In These Codes? 我无法弄清楚我的代码不起作用的原因 - I can't figure out the reasoning why my code doesn't work 我计算最大二进制间隙的函数不起作用,但我不知道为什么 - My Function To Count The Largest Binary Gap Doesn't Work But I Can't Figure Out Why 我不知道如何让这个 function 正常工作 - I can't figure out how to make this function work properly 我的脚本出现语法错误,但我不知道为什么以及如何修复它&gt; - My script comes up with syntax error but I can't figure out why and how to fix it> 无法弄清楚为什么我的 if/else 语句不像我想要的那样工作 - Can not figure out why my if/else statement doesn`t work like i intend Python-我的部分代码不起作用,我不知道为什么 - Python- Parts of my code aren't working and I can't figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM