通过cx_Freeze将py转换为exe,启动时出现回溯错误

时间:2019-04-21 15:23:33

标签: python windows tkinter exe

当我花几天时间寻找一个可以运行的py到exe转换器时,我终于找到了一个cx_Freeze,然后花了1天的时间试图使其正常工作,现在终于可以了,现在当我启动exe时,我发现错误。 https://imgur.com/a/tZjcJep

我已经搜索了几个小时,但没有找到需要的东西。但是我确实认为这是由于tkinter造成的。

我编写的程序代码:

#Modules
from tkinter import *
from tkinter import messagebox
import os
import subprocess

if os.path.exists("./Settings"):
    pass
else:
    messagebox.showerror("FIND Error","FIND Err: Cannot find \"Settings\" folder")
    quit()

themeFile = open(".\\Settings\\theme.txt","r")
themeFileRead = themeFile.read()
themeFile.close()

#Variables
version = "2019.0"
title = "Application"
bg = "white"
geo = "500x500"
theme = themeFileRead
bgtheme = ""
bgthemebtn = ""
fgtheme = ""

if theme == "dark":
    bgtheme = "#101010"
    bgthemebtn = "#303030"
    fgtheme = "white"
if theme == "light":
    bgtheme = "white"
    bgthemebtn = "gray"
    fgtheme = "black"

#Defs

def checkUpdate():
    pass

def changetheme():
    if lblTheme["text"] == "light":
        themeFile2 = open(".\\Settings\\theme.txt","w")
        themeFile2.write("light")
        themeFile2.close()
    if lblTheme["text"] == "dark":
        themeFile2 = open(".\\Settings\\theme.txt","w")
        themeFile2.write("dark")
        themeFile2.close()

def sellighttheme():
    slt.destroy()
    lblTheme["text"] = "light"

def seldarktheme():
    slt.destroy()
    lblTheme["text"] = "dark"

def selecttheme():
    global slt
    slt = Tk()
    slt.title("Select Theme")
    slt.config(bg=bgtheme)
    slt.geometry("500x500")

    btn24 = Button(slt,text="Light",bg=bgthemebtn,fg=fgtheme,font="Arial",command=sellighttheme)
    btn24.grid(row=0,column=0)
    btn25 = Button(slt,text="Dark",bg=bgthemebtn,fg=fgtheme,font="Arial",command=seldarktheme)
    btn25.grid(row=0,column=1)

def preferences():
    pref = Tk()
    pref.title("Preferences")
    pref.config(bg=bgtheme)
    pref.geometry("500x500")
    pref.iconbitmap(".\Images\create.ico")

    lbl28 = Label(pref,text="Theme: (Requires restart)",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl28.grid(row=0,column=0)
    btn22 = Button(pref,text="Select",bg=bgthemebtn,fg=fgtheme,font="Arial",command=selecttheme)
    btn22.grid(row=0,column=1)
    global lblTheme
    lblTheme = Label(pref,text="Theme: " + theme,bg=bgtheme,fg=fgtheme,font="Arial")
    lblTheme.grid(row=0,column=2)
    btn23 = Button(pref,text="OK",bg=bgthemebtn,fg=fgtheme,font="Arial",command=changetheme)
    btn23.grid(row=0,column=3)

def exitf():
    quit()

#Create folder
def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
         messagebox.showerror("OS Error"," OS Err: Cannot create directory")

def createentry():
    ename = e13.get()
    ebg = e14.get()
    efg = e15.get()
    ewidth = e16.get()
    erow = e17.get()
    ecolumn = e18.get()

    try:
        if not os.path.exists("./" + name + "/" + ename):
            os.mkdir("./" + name + "/" + ename)
    except OSError:
        messagebox.showerror("OS Error","OS Err: Cannot create directory")

    file = open(".\\" + name + "\\" + ename + "\\name.txt","w")
    file.write(ename)
    file.close()
    file2 = open(".\\" + name + "\\" + ename + "\\bg.txt","w")
    file2.write(ebg)
    file2.close()
    file3 = open(".\\" + name + "\\" + ename + "\\fg.txt","w")
    file3.write(efg)
    file3.close()
    file4 = open(".\\" + name + "\\" + ename + "\\width.txt","w")
    file4.write(ewidth)
    file4.close()
    file5 = open(".\\" + name + "\\" + ename + "\\row.txt","w")
    file5.write(erow)
    file5.close()
    file6 = open(".\\" + name + "\\" + ename + "\\column.txt","w")
    file6.write(ecolumn)
    file6.close()

    ename = Entry(app,bg=ebg,fg=efg,font="Arial")
    ename.config(width=ewidth)
    ename.grid(row=erow,column=ecolumn)

    cree.destroy()

def createentrywin():
    ent.destroy()

    #Create label window
    global cree
    cree = Tk()
    cree.title("Create input")
    cree.config(bg=bgtheme)
    cree.geometry("1850x75")

    lbl22 = Label(cree,text="Name: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl22.grid(row=0,column=0)
    global e13
    e13 = Entry(cree,font="Arial")
    e13.grid(row=0,column=1)
    lbl23 = Label(cree,text="BG Color (color name or #hex): ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl23.grid(row=0,column=2)
    global e14
    e14 = Entry(cree,font="Arial")
    e14.grid(row=0,column=3)
    lbl24 = Label(cree,text="Text Color (color name or #hex): ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl24.grid(row=0,column=4)
    global e15
    e15 = Entry(cree,font="Arial")
    e15.grid(row=0,column=5)
    lbl25 = Label(cree,text="Width (Default = 25): ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl25.grid(row=0,column=6)
    global e16
    e16 = Entry(cree,font="Arial")
    e16.grid(row=0,column=7)
    lbl26 = Label(cree,text="Row: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl26.grid(row=0,column=8)
    global e17
    e17 = Entry(cree,font="Arial")
    e17.grid(row=0,column=9)
    lbl27 = Label(cree,text="Column: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl27.grid(row=0,column=10)
    global e18
    e18 = Entry(cree,font="Arial")
    e18.grid(row=1,column=0)
    btn21 = Button(cree,text="OK",bg=bgthemebtn,fg=fgtheme,font="Arial",command=createentry)
    btn21.grid(row=1,column=1)

def loadentry():
    enter = e19.get()

    try:
        file = open(".\\" + name + "\\" + enter + "\\name.txt","r")
        ename = file.read()
        file.close()
        file2 = open(".\\" + name + "\\" + enter + "\\bg.txt","r")
        ebg = file2.read()
        file2.close()
        file3 = open(".\\" + name + "\\" + enter + "\\fg.txt","r")
        efg = file3.read()
        file3.close()
        file4 = open(".\\" + name + "\\" + enter + "\\width.txt","r")
        ewidth = file4.read()
        file4.close()
        file5 = open(".\\" + name + "\\" + enter + "\\row.txt","r")
        erow = file5.read()
        file5.close()
        file6 = open(".\\" + name + "\\" + enter + "\\column.txt","r")
        ecolumn = file6.read()
        file6.close()

        ename = Entry(app,bg=ebg,fg=efg,font="Arial")
        ename.config(width=ewidth)
        ename.grid(row=erow,column=ecolumn)
    except:
        messagebox.showerror("Loading Error","Loading Err: Cannot load entry")

    loe.destroy()

def loadentrywin():
    global loe
    loe = Tk()
    loe.title("Load input")
    loe.config(bg=bgtheme)
    loe.geometry("400x50")

    lbl21 = Label(loe,text="Input name: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl21.grid(row=0,column=0)
    global e19
    e19 = Entry(loe,font="Arial")
    e19.grid(row=0,column=1)
    btn16 = Button(loe,text="OK",bg=bgthemebtn,fg=fgtheme,font="Arial",command=loadentry)
    btn16.grid(row=0,column=2)

    ent.destroy()

def entry():
    cre.destroy()

    #Label window
    global ent
    ent = Tk()
    ent.title("Create input")
    ent.config(bg=bgtheme)
    ent.geometry("200x100")

    #Buttons
    btn18 = Button(ent,text="Create",bg=bgthemebtn,fg=fgtheme,font="Arial",command=createentrywin)
    btn18.grid(row=0,column=0)
    lbl19 = Label(ent,text=" or ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl19.grid(row=0,column=1)
    btn120 = Button(ent,text="Load",bg=bgthemebtn,fg=fgtheme,font="Arial",command=loadentrywin)
    btn120.grid(row=0,column=2)

def loadlabel():
    enter = e12.get()

    try:
        file = open(".\\" + name + "\\" + enter + "\\name.txt","r")
        lblname = file.read()
        file.close()
        file2 = open(".\\" + name + "\\" + enter + "\\text.txt","r")
        lbltext = file2.read()
        file2.close()
        file3 = open(".\\" + name + "\\" + enter + "\\bg.txt","r")
        lblbg = file3.read()
        file3.close()
        file4 = open(".\\" + name + "\\" + enter + "\\fg.txt","r")
        lblfg = file4.read()
        file4.close()
        file5 = open(".\\" + name + "\\" + enter + "\\row.txt","r")
        lblrow = file5.read()
        file5.close()
        file6 = open(".\\" + name + "\\" + enter + "\\column.txt","r")
        lblcolumn = file6.read()
        file6.close()

        lblname = Label(app,text=lbltext,bg=lblbg,fg=lblfg,font="Arial")
        lblname.grid(row=lblrow,column=lblcolumn)
    except:
        messagebox.showerror("Loading Error","Loading Err: Cannot load label")

    lol.destroy()

def loadlabelwin():
    global lol
    lol = Tk()
    lol.title("Load label")
    lol.config(bg=bgtheme)
    lol.geometry("400x50")

    lbl21 = Label(lol,text="Label name: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl21.grid(row=0,column=0)
    global e12
    e12 = Entry(lol,font="Arial")
    e12.grid(row=0,column=1)
    btn16 = Button(lol,text="OK",bg=bgthemebtn,fg=fgtheme,font="Arial",command=loadlabel)
    btn16.grid(row=0,column=2)

    lab.destroy()

def createlabel():
    lblname = e6.get()
    lbltext = e7.get()
    lblbg = e8.get()
    lblfg = e9.get()
    lblrow = e10.get()
    lblcolumn = e11.get()

    try:
        if not os.path.exists("./" + name + "/" + lblname):
            os.mkdir("./" + name + "/" + lblname)
    except OSError:
        messagebox.showerror("OS Error","OS Err: Cannot create directory")

    file = open(".\\" + name + "\\" + lblname + "\\name.txt","w")
    file.write(lblname)
    file.close()
    file2 = open(".\\" + name + "\\" + lblname + "\\text.txt","w")
    file2.write(lbltext)
    file2.close()
    file3 = open(".\\" + name + "\\" + lblname + "\\bg.txt","w")
    file3.write(lblbg)
    file3.close()
    file4 = open(".\\" + name + "\\" + lblname + "\\fg.txt","w")
    file4.write(lblfg)
    file4.close()
    file5 = open(".\\" + name + "\\" + lblname + "\\row.txt","w")
    file5.write(lblrow)
    file5.close()
    file6 = open(".\\" + name + "\\" + lblname + "\\column.txt","w")
    file6.write(lblcolumn)
    file6.close()

    lblname = Label(app,text=lbltext,bg=lblbg,fg=lblfg,font="Arial")
    lblname.grid(row=lblrow,column=lblcolumn)

    crel.destroy()

def createlabelwin():
    lab.destroy()

    #Create label window
    global crel
    crel = Tk()
    crel.title("Create label")
    crel.config(bg=bgtheme)
    crel.geometry("1850x50")

    #Create label
    lbl16 = Label(crel,text="Name: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl16.grid(row=0,column=0)
    global e6
    e6 = Entry(crel,font="Arial")
    e6.grid(row=0,column=1)
    lbl17 = Label(crel,text="Text: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl17.grid(row=0,column=2)
    global e7
    e7 = Entry(crel,font="Arial")
    e7.grid(row=0,column=3)
    lbl18 = Label(crel,text="BG Color (color name or #hex): ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl18.grid(row=0,column=4)
    global e8
    e8 = Entry(crel,font="Arial")
    e8.grid(row=0,column=5)
    lbl18 = Label(crel,text="Text Color (color name or #hex): ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl18.grid(row=0,column=6)
    global e9
    e9 = Entry(crel,font="Arial")
    e9.grid(row=0,column=7)
    lbl19 = Label(crel,text="Row: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl19.grid(row=0,column=8)
    global e10
    e10 = Entry(crel,font="Arial")
    e10.grid(row=0,column=9)
    lbl20 = Label(crel,text="Column: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl20.grid(row=0,column=10)
    global e11
    e11 = Entry(crel,font="Arial")
    e11.grid(row=0,column=11)
    btn15 = Button(crel,text="OK",bg=bgthemebtn,fg=fgtheme,font=fgtheme,command=createlabel)
    btn15.grid(row=0,column=12)

def label():
    cre.destroy()

    #Label window
    global lab
    lab = Tk()
    lab.title("Create label")
    lab.config(bg=bgtheme)
    lab.geometry("200x100")

    #Buttons
    btn13 = Button(lab,text="Create",bg=bgthemebtn,fg="white",font=fgtheme,command=createlabelwin)
    btn13.grid(row=0,column=0)
    lbl15 = Label(lab,text=" or ",bg=bgtheme,fg="white",font=fgtheme)
    lbl15.grid(row=0,column=1)
    btn14 = Button(lab,text="Load",bg=bgthemebtn,fg=fgtheme,font="Arial",command=loadlabelwin)
    btn14.grid(row=0,column=2)

def changegeo():
    geo = e4.get() + "x" + e5.get()
    app.geometry(geo)
    file = open(".\\" + name + "\\geometry.txt","w")
    file.write(geo)
    file.close()

def loadgeo():
    file = open(".\\" + name + "\\geometry.txt","r")
    fileRead = file.read()
    file.close()
    geo = fileRead
    app.geometry(geo)

def titlechange():
    title = e3.get()
    app.title(title)
    file = open(".\\" + name + "\\title.txt","w")
    file.write(title)
    file.close

def titleload():
    file = open(".\\" + name + "\\title.txt","r")
    fileRead = file.read()
    file.close()
    title = fileRead
    app.title(title)

def bgcolor():
    bg = e2.get()
    app.config(bg=bg)
    file = open(".\\" + name + "\\bgcolor.txt","w")
    file.write(bg)
    file.close()

def bgcolorload():
    file = open(".\\" + name + "\\bgcolor.txt","r")
    fileRead = file.read()
    file.close()
    bg = fileRead
    app.config(bg=bg)

#Creations tab
def creations():
    #Creations window
    global cre
    cre = Tk()
    cre.title("Creations")
    cre.config(bg=bgtheme)
    cre.geometry("500x500")
    cre.iconbitmap(".\Images\plus.ico")

    #Title
    lbl8 = Label(cre,text="Creations",bg=bgtheme,fg=fgtheme,font="Arial 44 bold")
    lbl8.grid(row=0,column=0)

    #Buttons
    btn12 = Button(cre,text="Create label",bg=bgthemebtn,fg=fgtheme,font="Arial",command=label)
    btn12.grid(row=1,column=0)
    btn17 = Button(cre,text="Create input",bg=bgthemebtn,fg=fgtheme,font="Arial",command=entry)
    btn17.grid(row=1,column=1)


def application():
    global app
    app = Tk()
    app.title(title)
    app.config(bg=bg)
    app.geometry("500x500")
    app.iconbitmap(".\Images\create.ico")

#Settings of application
def settings():
    #Settings window
    global sts
    sts = Tk()
    sts.title("Settings of " + name)
    sts.config(bg=bgtheme)
    sts.geometry("900x500")
    sts.iconbitmap(".\Images\gear.ico")

    #Title
    lbl7 = Label(sts,text="Settings",bg=bgtheme,fg=fgtheme,font="Arial 44 bold")
    lbl7.grid(row=0,column=0)

    #Change background color
    lbl9 = Label(sts,text="BG color: (color name or #hex)",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl9.grid(row=1,column=0)
    global e2
    e2 = Entry(sts,font="Arial")
    e2.grid(row=1,column=1)
    btn6 = Button(sts,text="Change",bg=bgthemebtn,fg=fgtheme,font="Arial",command=bgcolor)
    btn6.grid(row=1,column=2)
    lbl10 = Label(sts,text=" or ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl10.grid(row=1,column=3)
    btn7 = Button(sts,text="Load",bg=bgthemebtn,fg=fgtheme,font="Arial",command=bgcolorload)
    btn7.grid(row=1,column=4)

    #Change title
    lbl11 = Label(sts,text="Title: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl11.grid(row=2,column=0)
    global e3
    e3 = Entry(sts,font="Arial")
    e3.grid(row=2,column=1)
    btn8 = Button(sts,text="Change",bg=bgthemebtn,fg=fgtheme,font="Arial",command=titlechange)
    btn8.grid(row=2,column=2)
    lbl12 = Label(sts,text=" or ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl12.grid(row=2,column=3)
    btn9 = Button(sts,text="Load",bg=bgthemebtn,fg=fgtheme,font="Arial",command=titleload)
    btn9.grid(row=2,column=4)

    #Change window size
    lbl13 = Label(sts,text="Window size (width X height): ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl13.grid(row=3,column=0)
    global e4
    e4 = Entry(sts,font="Arial")
    e4.grid(row=3,column=1)
    lbl14 = Label(sts,text=" X ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl14.grid(row=3,column=2)
    global e5
    e5 = Entry(sts,font="Arial")
    e5.grid(row=3,column=3)
    inv7 = Label(sts,text="",bg=bgtheme)
    inv7.grid(row=3,column=4)
    btn10 = Button(sts,text="Change",bg=bgthemebtn,fg=fgtheme,font="Arial",command=changegeo)
    btn10.grid(row=3,column=5)
    lbl15 = Label(sts,text=" or ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl15.grid(row=3,column=6)
    btn11 = Button(sts,text="Load",bg=bgthemebtn,fg=fgtheme,font="Arial",command=loadgeo)
    btn11.grid(row=3,column=7)

#Manager
def initialize():
    #Close the begin screen and create / load screen
    beg.destroy()
    pro.destroy()

    global man
    man = Tk()

    #Menubar
    menubar = Menu(man)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="Application", command=application)
    filemenu.add_command(label="Preferences", command=preferences)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=exitf)
    menubar.add_cascade(label="File", menu=filemenu)

    #Manager window
    man.title(name + " on BE Studio | " + version)
    man.config(bg=bgtheme,menu=menubar)
    man.geometry("1300x600")
    man.iconbitmap(".\Images\icon.ico")

    #Application that the user creates
    application()

    #Tools
    lbl5 = Label(man,text="Tools",bg=bgtheme,fg=fgtheme,font="Arial 44 bold")
    lbl5.grid(row=0,column=0)
    btn4 = Button(man,text="Settings",bg=bgthemebtn,fg=fgtheme,font="Arial",command=settings)
    btn4.config(width=16,height=5)
    btn4.grid(row=1,column=0)
    btn5 = Button(man,text="Creations",bg=bgthemebtn,fg=fgtheme,font="Arial",command=creations)
    btn5.config(width=16,height=5)
    btn5.grid(row=2,column=0)

    #Title
    inv2 = Label(man,text="",bg=bgtheme)
    inv2.grid(row=0,column=1)
    inv3 = Label(man,text="",bg=bgtheme)
    inv3.grid(row=0,column=2)
    inv4 = Label(man,text="",bg=bgtheme)
    inv4.grid(row=0,column=3)
    lbl6 = Label(man,text=name,bg=bgtheme,fg=fgtheme,font="Arial 76 bold")
    lbl6.grid(row=0,column=4)

#Load project
def loadproj():
     global name
     name = e.get()
     initialize()

#Create project
def createproj():
     global name
     name = e.get()
     createFolder("./" + name)
     initialize()

#Create or load project
def start():
    #Project screen
    global pro
    pro = Tk()
    pro.title("BE Studio | " + version)
    pro.config(bg=bgtheme)
    pro.geometry("600x200")
    pro.iconbitmap(".\Images\icon.ico")

    #Title
    lbl3 = Label(pro,text="Start",bg=bgtheme,fg=fgtheme,font="Arial 76 bold")
    lbl3.grid(row=0,column=0)

    #Name
    lbl4 = Label(pro,text="Name: ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl4.grid(row=1,column=0)
    global e
    e = Entry(pro,font="Arial")
    e.grid(row=1,column=1)
    inv = Label(pro,text="",bg=bgtheme)
    inv.grid(row=1,column=2)
    btn2 = Button(pro,text="Create",bg=bgthemebtn,fg=fgtheme,font="Arial",command=createproj)
    btn2.grid(row=1,column=3)
    lbl5 = Label(pro,text=" or ",bg=bgtheme,fg=fgtheme,font="Arial")
    lbl5.grid(row=1,column=4)
    btn3 = Button(pro,text="Load",bg=bgthemebtn,fg=fgtheme,font="Arial",command=loadproj)
    btn3.grid(row=1,column=5)

#Create or load project
def startplug():
    if os.path.exists("./Plugins"):
        pass
    else:
        messagebox.showerror("PLUGIN Error","PLUGIN Err: \"Plugins\" folder not found")

def beginscreen():
    global beg
    beg = Tk()
    beg.title("BE Studio | " + version)
    beg.config(bg=bgtheme)
    beg.geometry("500x750")
    beg.iconbitmap(".\Images\icon.ico")

    #Title
    lbl = Label(beg,text="BE Studio",bg=bgtheme,fg=fgtheme,font="Arial 76 bold")
    lbl.grid(row=0,column=0)
    lbl2 = Label(beg,text=version,bg=bgtheme,fg=fgtheme,font="Arial 14")
    lbl2.grid(row=1,column=0)

    #Start
    btn = Button(beg,text="Create or load Application Project",bg=bgthemebtn,fg=fgtheme,font="Arial",command=start)
    btn.config(width=30,height=15)
    btn.grid(row=2,column=0)
    btn = Button(beg,text="Create or load Plugin Project",bg=bgthemebtn,fg=fgtheme,font="Arial",command=startplug)
    btn.config(width=30,height=15)
    btn.grid(row=3,column=0)

checkUpdate()
beginscreen()

0 个答案:

没有答案