python - Python 文件执行失败
问题描述
我正在使用 python 和 tkinter 制作一个小地址簿应用程序,当我使用 Pyinstaller 时,程序将成功构建,但是当我运行 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
让您拥有控制台。这只是为了确定错误来自哪里
推荐阅读
- javascript - jQuery-为什么我的 .on('click') 事件处理程序不起作用?
- r - Dplyr:计算组数并将其变异为表格
- jmeter - 我想通过 Jmeter 报告每个页面的响应时间,虽然在页面加载时进行了多个 API 调用。怎么做?
- java - 为什么即使在 sql developer 中启动相同的命令有效,resultSet next 也不会从我的 oracle SQL 数据库返回任何值?
- excel - 如果一行中的值满足特定条件,如何对列中的值求和?
- java - 创建验证用户 ID 的方法
- php - AnimGif 创建具有透明背景的 Gif
- java - 构建发布构建的签名 APK 失败,但调试成功。缺课
- html - 我想用 html 和 css 解决所有这些评估
- jquery - Moment.js:时间不基于计算机时间