我有一个非常简单的.py程序,它使用一个用tkinter构建的GUI来提示用户输入两个数字,然后使用来自reportlab.pdfgen导入画布的'在PDF中输出答案。这是下面的代码。
from tkinter import *
def add_numbers():
num1 = (e1.get())
num2 = (e2.get())
print(float(num1) + float(num2))
master = Tk()
# Modify the window
master.title("Add two numbers")
master.geometry("800x500")
Label(master, text = "Input parameters below:").grid(row=0, column=0, sticky=W)
Label(master, text="a = ").grid(row=2, column=0, sticky=E)
Label(master, text = "mm").grid(row=2, column=2, sticky=W)
Label(master, text="b = ").grid(row=3, column=0, sticky=E)
Label(master, text = "mm").grid(row=3, column=2, sticky=W)
Label(master, text = "Press 'Input' & then 'Calculate'").grid(row=5, column=0)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=2, column=1)
e2.grid(row=3, column=1)
Button(master, text='Input', command=add_numbers).grid(row=7, column=0, sticky=W, pady=4)
Button(master, text='Calculate', command=master.quit).grid(row=8, column=0, sticky=W, pady=4)
mainloop()
c2 = e1.get()
d2 = e2.get()
print(float(c2) * float(d2))
a1 = float(c2) * float(d2)
from reportlab.pdfgen import canvas
c = canvas.Canvas("test1.pdf")
c.drawString(100,750, "a = " + c2)
c.drawString(100,730, "b = " + d2)
c.drawString(100,710, "area = " + str(a1) + "mm2")
c.save()
是否可以创建一个独立的.exe程序,用户可以从tkinter GUI中的输入生成PDF而无需在其系统上安装Python?我目前在setup.py文件中使用以下代码:
import sys
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\\bra\\AppData\\Local\\Programs\\Python\\Python36-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\\bra\\AppData\\Local\\Programs\\Python\\Python36-32\\tcl\\tcl8.6"
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os","reportlab.pdfgen","tkinter"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "my_first_prog.py",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("my_first_prog.py", base=base)])
当我这样做时,我可以创建一个' build'目录,我可以找到一个'应用程序'类型文件,名为my_first_prog。我假设这是.exe文件?当我点击它时,我在底部得到以下内容:" import _tkinter#如果失败,则可能没有为Tk"配置Python。和" ImportError:DLL加载失败:找不到指定的模块。"
我在这里找不到任何容易的事吗?任何帮助将不胜感激!