![](/img/trans.png)
[英]Why does my python script stop executing when I try to make my API call?
[英]I can't figure out why my python script works but when try to make it an executable it does not work
我在將腳本作為可執行文件運行時遇到問題。 我相信它與 MEIXXXXX 有關,但我不確定如何補救。 下面的腳本在 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()
這些是我為使其工作而采取的步驟。
mkdir newdir
和cd newdir
py -m venv venv
and venv\Scripts\activate
py -m pip install --upgrade pip pyinstaller pillow python-barcode
touch 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()
我做了一些非常小的改動,以確保將圖像寫入用戶當前目錄,而不是將它們寫入臨時運行時文件夾。
pyinstaller -F main.py
main.spec
添加一個元組,其中包含條形碼 fonts 文件夾的路徑和應用程序運行時的目標路徑。主要規范
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=[('./venv/Lib/site-packages/barcode/fonts', './barcode/fonts')], # add this
...
pyinstaller main.spec
dist\main.exe
就是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.