简体   繁体   中英

Converting Python3 script to .exe with cx_Freeze, attribute error

Writing my first Python application and my first post here. I am running windows 10 with Python 3.7 and cx_Freeze version 5.1.1.

Once I've run the setup build and I double click the executable file I am meet with the error: AttributeError: 'NoneType' object has no attribute 'write'. I can't seem to figure it out myself.

(Also having problem with the picture that can't be found, so I commented it out in the script just to work on one issue at a time)

Tbh I am not sure where to start, I believe the error is thrown because of the write to textbox code:

def decorator(func):
    def inner(inputStr):
        try:
            textbox.insert(INSERT, inputStr)
            return func(inputStr)
        except:
            return func(inputStr)
    return inner

sys.stdout.write=decorator(sys.stdout.write)

This is the script I've created.

from tkinter import *

from PIL import Image, ImageTk



root = Tk()
root.title("Ghost task")
root.geometry("640x640+0+0")

#Image load
#load = Image.open('Ghost.png')
#ghost = ImageTk.PhotoImage(load)

#img = Label(root, image=ghost)
#img.image = ghost
#img.place(x=0, y=0)

#Text

heading = Label(root, text="Finish workflow script", font =("arial", 20, "bold"), fg="steelblue").place(x=200, y=0)
label1 = Label(root, text="Provide the wf_group:", font =("arial", 12, "bold"), fg="steelblue").place(x=200, y=80)
label2 = Label(root, text="Provide the client:", font =("arial", 12, "bold"), fg="steelblue").place(x=200, y=103)
label3 = Label(root, text="Provide the user:", font =("arial", 12, "bold"), fg="steelblue").place(x=200, y=126)
label4 = Label(root, text="Provide the voucher no:", font =("arial", 12, "bold"), fg="steelblue").place(x=200, y=149)
bottom = Label(root, text="Version 1.01", font = ("arial", 6, "bold"), fg="black").place(x=580, y=625)

#Boxes

wfgroup = StringVar()
client = StringVar()
user = StringVar()
voucher = StringVar()
entry_box = Entry(root, textvariable=wfgroup, width=28, bg="lightgreen").place(x=445, y=85)
entry_box = Entry(root, textvariable=client, width=28, bg="lightgreen").place(x=445, y=107)
entry_box = Entry(root, textvariable=user, width = 28, bg="lightgreen").place(x=445, y=129)
entry_box = Entry(root, textvariable=voucher, width = 28, bg="lightgreen").place(x=445, y=151)




#Results

def do_mssql():
    textbox.delete('1.0', END)
    textbox.update()
    print("Removed some useless information")



def do_oracle():
    textbox.delete('1.0', END)
    textbox.update()
    print("Removed some useless information")


#Buttons
work = Button(root, text="MsSQL Buu", width=30, height=5, bg="pink", command=do_mssql).place(x=50, y=200)
work2 = Button(root, text="Oracle Buu", width=30, height=5, bg="pink", command=do_oracle).place(x=360, y=200)

#Text output

textbox=Text(root, width = 77, height = 20)
textbox.place(x=10, y=300)



def decorator(func):
    def inner(inputStr):
        try:
            textbox.insert(INSERT, inputStr)
            return func(inputStr)
        except:
            return func(inputStr)
    return inner

sys.stdout.write=decorator(sys.stdout.write)


root.mainloop()

Appreciate any help I can get. //Fred

Figured it out.

It was the .write that caused the issue. I rewrote the script a bit. Did not need to but that's how I solved it.

def do_mssql():
    textbox.delete('1.0', END)
    textbox.update()
    sys.stdout("--This script is for MsSQL--\n")



def redirector(inputStr):
    textbox.insert(INSERT, inputStr)

sys.stdout = redirector

Removed .write from sys.stdout.write Changed the print() to sys.stdout and it worked. :)

//Fred

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