簡體   English   中英

Python 文件執行失敗

[英]Python file failed to execute

我正在使用 python 和 tkinter 制作一個小地址簿應用程序,當我使用 Pyinstaller 時,程序將成功構建,但是當我 go 運行 exe 文件時,它無法執行腳本安裝程序?

當我運行 pyinstaller 我的命令是pyinstaller --onefile -w AddressBook.py


    from tkinter import *
    from tkinter import filedialog
    import sqlite3

    bg_color="lightblue"

    root=Tk()
    root.title("Address Book")
    root.geometry("300x400")
    root.configure(bg=bg_color)
    root.iconbitmap(r'address_book.ico')



    #DATABASES

    #create a database or connect to one
    connection = sqlite3.connect('address_book.db')
    #create a cursor, command executer
    cursor = connection.cursor()

    #create table
    '''
    cursor.execute(""" CREATE TABLE addresses(
                first_name text,
                last_name text,
                address text,
                city text,
                state text,
                zicode integer)
    """)
    '''

    def update():
        connection = sqlite3.connect('address_book.db')
        cursor = connection.cursor()

        record_id = delete_box.get()
        cursor.execute('''UPDATE addresses SET
            first_name = :first,
            last_name = :last,
            address = :address,
            city = :city,
            state = :state

            WHERE oid = :oid''',
            {
            'first': f_name_editor.get(),
            'last': l_name_editor.get(),
            'address': address_editor.get(),
            'city': city_editor.get(),
            'state': state_editor.get(),
            'oid': record_id
            })

        connection.commit()
        connection.close()

        editor.destroy()



    def edit():
        global editor
        editor = Tk()
        editor.title("Update ID")
        editor.geometry("300x200")
        connection = sqlite3.connect('address_book.db')
        cursor = connection.cursor()

        # Create global variables for text box names
        global f_name_editor
        global l_name_editor
        global address_editor
        global city_editor
        global state_editor
        global zipcode_editor

        #text boxes
        f_name_editor = Entry(editor, width=30)
        f_name_editor.grid(row=0, column=1, padx=20, pady=(10, 0))
        l_name_editor = Entry(editor, width=30)
        l_name_editor.grid(row=1, column=1, padx=20)
        address_editor = Entry(editor, width=30)
        address_editor.grid(row=2, column=1, padx=20)
        city_editor = Entry(editor, width=30)
        city_editor.grid(row=3, column=1, padx=20)
        state_editor = Entry(editor, width=30)
        state_editor.grid(row=4, column=1, padx=20)
        zipcode_editor = Entry(editor, width=30)
        zipcode_editor.grid(row=5, column=1, padx=20)
        #delete_box_editor = Entry(editor, width=30)
        #delete_box_editor.grid(row=10, column=1)

        # create text label
        f_name_label_editor = Label(editor, text="First Name")
        f_name_label_editor.grid(row=0, column=0, pady=(10, 0))
        l_name_label_editor = Label(editor, text="Last Name")
        l_name_label_editor.grid(row=1, column=0)
        address_label_editor = Label(editor, text="Address")
        address_label_editor.grid(row=2, column=0)
        city_label_editor = Label(editor, text="City")
        city_label_editor.grid(row=3, column=0)
        state_label_editor = Label(editor, text="State")
        state_label_editor.grid(row=4, column=0)
        zipcode_label_editor = Label(editor, text="ZipCode")
        zipcode_label_editor.grid(row=5, column=0)
        #delete_box_label_editor = Label(editor, text="Select ID")
        #delete_box_label_editor.grid(row=10, column=0)

        # create Submit Button
        #submit_button_editor = Button(editor, text="Add Record", command=submit)
        #submit_button_editor.grid(row=6, column=0, columnspan=2, pady=5, padx=10, ipadx=100)

        # create a query button
        #query_button = Button(editor, text="Show Records", command=query)
        #query_button.grid(row=7, column=0, columnspan=2, pady=5, padx=10, ipadx=95)

        # create a delete button
        #delete_button_editor = Button(editor, text="Delete ID", command=delete)
        #delete_button_editor.grid(row=11, column=0, columnspan=2, pady=5, padx=10, ipadx=109)

        #edit_button = Button(editor, text="Update ID", command=edit)
        #edit_button.grid(row=12, column=0, columnspan=2, pady=5, padx=10, ipadx=106)

        save_button_editor = Button(editor, text="Save ID", command=update)
        save_button_editor.grid(row=7, column=0, columnspan=2, pady=5, padx=10, ipadx=106)

        record_id = delete_box.get()
        #query the database
        cursor.execute("SELECT *FROM addresses WHERE oid = " + record_id)
        records = cursor.fetchall()
        #loop through results
        for record in records:
            f_name_editor.insert(0, record[0])
            l_name_editor.insert(1, record[1])
            address_editor.insert(2, record[2])
            city_editor.insert(3, record[3])
            state_editor.insert(4, record[4])
            zipcode_editor.insert(5, record[5])

        connection.commit()
        connection.close()

        return

    def delete():
        connection = sqlite3.connect('address_book.db')
        cursor = connection.cursor()

        #delete a record
        cursor.execute("DELETE from addresses WHERE oid= " + delete_box.get())

        connection.commit()
        connection.close()

        delete_box.delete(0, END)

        return

    def submit():
        # create a database or connect to one
        connection = sqlite3.connect('address_book.db')
        # create a cursor, command executer
        cursor = connection.cursor()

        #insert into table
        connection.execute("INSERT INTO addresses VALUES (:f_name, :l_name, :address, :city, :state, :zipcode)",
                           {
                               'f_name': f_name.get(),
                               'l_name': l_name.get(),
                               'address': address.get(),
                               'city': city.get(),
                               'state': state.get(),
                               'zipcode': zipcode.get()
                           })

        connection.commit()

        connection.close()


        #clear the text boxes
        f_name.delete(0, END)
        l_name.delete(0, END)
        address.delete(0, END)
        city.delete(0, END)
        state.delete(0, END)
        zipcode.delete(0, END)


    def query():
        connection = sqlite3.connect('address_book.db')
        cursor = connection.cursor()

        #query the database
        cursor.execute("SELECT *, oid FROM addresses")
        records = cursor.fetchall()
        print(records)

        #loop through results
        print_records = ''
        for record in records:
            print_records += str(record[0]) + " " + str(record[1]) + " " + "\t\t" +  str(record[6]) + "\n"

        query_label = Label(root, text=print_records, bg=bg_color)
        query_label.grid(row=13, column=0, columnspan=2)

        connection.commit()
        connection.close()


    #create text boxes
    f_name = Entry(root, width=30)
    f_name.grid(row=0, column=1, padx=20, pady=(10,0))
    l_name = Entry(root, width=30)
    l_name.grid(row=1, column=1, padx=20)
    address = Entry(root, width=30)
    address.grid(row=2, column=1, padx=20)
    city = Entry(root, width=30)
    city.grid(row=3, column=1, padx=20)
    state = Entry(root, width=30)
    state.grid(row=4, column=1, padx=20)
    zipcode = Entry(root, width=30)
    zipcode.grid(row=5, column=1, padx=20)
    delete_box = Entry(root, width=30)
    delete_box.grid(row=10, column=1, pady=(15,0))


    #create text label
    f_name_label = Label(root, text="First Name", bg=bg_color)
    f_name_label.grid(row=0, column=0, pady=(10,0))
    l_name_label = Label(root, text="Last Name", bg=bg_color)
    l_name_label.grid(row=1, column=0)
    address_label = Label(root, text="Address", bg=bg_color)
    address_label.grid(row=2, column=0)
    city_label = Label(root, text="City", bg=bg_color)
    city_label.grid(row=3, column=0)
    state_label = Label(root, text="State", bg=bg_color)
    state_label.grid(row=4, column=0)
    zipcode_label = Label(root, text="ZipCode", bg=bg_color)
    zipcode_label.grid(row=5, column=0)
    delete_box_label = Label(root, text="Select ID", bg=bg_color)
    delete_box_label.grid(row=10, column=0, pady=(15,0))

    #create Submit Button
    submit_button = Button(root, text="Add Record", command=submit)
    submit_button.grid(row=6, column=0, columnspan=2, pady=5, padx=10, ipadx=100)

    #create a query button
    query_button=Button(root, text="Show Records", command=query)
    query_button.grid(row=7, column=0, columnspan=2, pady=5, padx=10, ipadx=95)

    #create a delete button
    delete_button=Button(root, text="Delete ID", command=delete)
    delete_button.grid(row=11, column=0, columnspan=2, pady=5, padx=10, ipadx=109)

    edit_button=Button(root, text="Update ID", command=edit)
    edit_button.grid(row=12, column=0, columnspan=2, pady=5, padx=10, ipadx=106)




    #commit change
    connection.commit()
    #close connection
    connection.close()


    root.mainloop()

運行 python 文件而不將其轉換為 EXE。 如果可行,請嘗試pyinstaller --onefile AddressBook.py以便擁有控制台。 這只是為了確定錯誤來自哪里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM