我真的很想让我的 Py 项目成为一个独立的 exe,如果您对如何改进有任何建议,我很乐意接受,谢谢
I would really like to make my Py project a stand alone exe and if you have any suggestions on how to improve i am wide open thank you
- 所以这是我的代码,我真的很想让它成为一个独立的可执行文件,我正在使用 python 3.8.3 .
- 如果您有任何改进建议或设法找到方法,我将非常高兴。
- 我已经尝试过命令-cd C:\Users\chris\OneDrive\Desktop\K.A.T ai offline 然后输入然后输入命令- pyinstaller --onefile K.A.T_AI_offline.py
- 但它并没有结束工作,它确实在 dist 文件夹中给了我一个文件。
- 所以请你帮我制作一个独立的可执行文件,这样我就可以给我的计算机科学老师和朋友们看,而他们不必下载 python 和其他模块。
# All the modules being imported
import sys
import time
from tkinter import Tk, messagebox, simpledialog
import PySimpleGUI as sg
import pyttsx3
# The voice settings
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id) #changing index changes voices but ony 0 and 1 are working here
# This creates a dictionary
the_master = {}
the_world = {}
the_word= {}
# Defining how to read the info in capital_data.txt
def read_from_file():
with open('capital_data.txt') as file:
for line in file:
line = line.rstrip('\n')
country, city = line.split('/')
the_world[country] = city
# Defining how to write new info in new_data.txt
def write_to_file(country_name, city_name):
with open('new_data.txt', 'a') as file:
file.write('\n' + country_name + '/' + city_name)
def read_from_master_file():
with open('master_data.txt') as file:
for line in file:
line = line.rstrip('\n')
MName, MName = line.split('/')
the_master[MName]= MName
def write_to_master_file(master_name):
with open('master_data.txt', 'a') as file:
file.write('\n' + master_name + '/' + master_name)
def read_from_alphabet_file():
with open('phonetic_alphabet.txt') as file:
for line in file:
line = line.rstrip('\n')
letter, word = line.split('/')
the_word[letter] = word
# Popup box to ask wether K.A.T know this user
sg.theme('DarkBlack')
layout =[[sg.Text('Are You One Of My Masters')],
[sg.ReadFormButton('Yes'),sg.ReadFormButton('No')]]
form = sg.FlexForm('Do I Know You')
form.Layout(layout)
engine.say('are you already one of my masters')
engine.runAndWait()
button, values = form.Read()
if button is button == 'No':
form.close()
print ('Well In That Case')
print ('Hello user'), engine.say('well, in that case, Hello user'), engine.runAndWait(), print ('I am KAT'), engine.say('I am KAT'), engine.runAndWait(), print ('Which stands for Know.Alot.of.Things'), engine.say('Which stands for, Know a lot of things'), engine.runAndWait()
# see's if K.A.T know the Master
if button is button == 'Yes':
form.close()
engine.say('oh, so which master are you?')
engine.runAndWait()
root = Tk()
root.withdraw()
read_from_master_file()
query_master = simpledialog.askstring('Name Of Master', 'Which Master Are You?')
query_master = query_master.capitalize()
if query_master in the_master:
master_result = the_master[query_master]
engine.say('My apologies master,' + master_result)
engine.runAndWait()
choice_of_country = ('Would you like to know the Capital city of a Country')
choice_of_phonetic_alphabet = ('Would you like to know how to spell a word or letter in the Phonetic Alphabet')
yes_master = print (choice_of_country), engine.say('Would you like to know the Capital city of a Country.'), engine.runAndWait(), print ('OR'), engine.say('or.'), engine.runAndWait, print (choice_of_phonetic_alphabet), engine.say('Would you like to know how to spell a word or letter in the Phonetic Alphabet'), engine.runAndWait(), engine.say('Which one would you like to know, just type 1 or 2'), engine.runAndWait(),
choice = input('Which one would you like to know, just type 1 or 2')
else:
engine.say('sorry you are not one of my masters')
engine.runAndWait()
if button is button == 'No':
form.close()
engine.say('oh, and, please may you enter the name, that you wish to be called.')
engine.runAndWait()
root = Tk()
root.withdraw()
new_master = simpledialog.askstring('New Master',
'Oh And Please May You Enter the name that you wish to be called')
new_master = new_master.capitalize()
if new_master == 'Oliver' :
print('OH well in that case you can just')
engine.say('OH well in that case you can just')
print('GET LOST')
engine.say('GET LOST')
engine.runAndWait()
sys.exit(0)
elif new_master in the_master:
print('You Are A Master Silly.')
engine.say('you are already a master, silly, you, are master, ' + new_master)
engine.runAndWait()
write_to_master_file == False
elif new_master != 'Oliver':
write_to_master_file(new_master)
root.destroy()
engine.say('hello,'+new_master)
engine.runAndWait()
choice_of_country = ('Would you like to know the Capital city of a Country')
choice_of_phonetic_alphabet = ('Would you like to know how to spell a word or letter in the Phonetic Alphabet')
no_master = print (choice_of_country), engine.say('Would you like to know the Capital city of a Country.'), engine.runAndWait(), print ('OR'), engine.say('or.'), engine.runAndWait, print (choice_of_phonetic_alphabet), engine.say('Would you like to know how to spell a word or letter in the Phonetic Alphabet'), engine.runAndWait(), engine.say('Which one would you like to know, just type 1 or 2'), engine.runAndWait(),
choice = input('Which one would you like to know, just type 1 or 2')
if simpledialog in (None, 'Ok'):
root.destroy()
new_master = new_master.capitalize()
new_master = simpledialog.askstring('New Master',
'Oh And Please May You Enter the name that you wish to be called')
the_master[new_master] = new_master
if choice == '1' :
capital_cities = ('Well just type in the Country into the little window that will pop up and i\'ll tell you the Capital City')
print (capital_cities)
engine.say('Well just type in the Country into the little window that will pop up and i\'ll tell you the Capital City')
engine.runAndWait()
# Creates the Tkinter window
root = Tk()
root.withdraw()
read_from_file()
engine.say('Now, type the name of the country you would like to know the capital city of.')
engine.runAndWait()
while True:
query_country = simpledialog.askstring('Country', 'Type the name of a country:')
query_country = query_country.capitalize()
if query_country in the_world:
result = the_world[query_country]
messagebox.showinfo('Answer',
'The capital city of ' + query_country + ' is ' + result + '!')
engine.say('The capital city of ' + query_country + ' is ' + result)
engine.runAndWait()
else:
new_city = simpledialog.askstring('Teach me',
'I don\'t know! ' +
'What is the capital city of ' + query_country + '?')
engine.say('i don\'t know!'+
'what the capital city of'+query_country+'is'+
'but if you know you can type it in the box')
engine.runAndWait()
the_world[query_country] = new_city
write_to_file(query_country, new_city)
break
elif choice == '2' :
phonetic_alphabet = ('Well just type in the Word or Letter into the little window that will pop up and i\'ll tell you how to write it out, but in using the phonetic alphabet')
print (phonetic_alphabet)
read_from_alphabet_file()
engine.say('Well just type in the Word or Letter into the little window that will pop up and i\'ll tell you how to write it out, but in using the phonetic alphabet')
engine.runAndWait()
root = Tk()
root.withdraw()
while True:
query_word = simpledialog.askstring('Word', 'Type a word or letter :')
if query_word in the_word:
word_result = the_word[query_word]
messagebox.showinfo('Answer',
'The way you would say' + query_word + ' in the phonetic alphabet is ' +word_result + '!')
engine.say('the way you would say'+query_country+'in the phonetic alphabet is'+result)
engine.runAndWait()
break
我认为它对你不起作用的原因是因为你的 exe 有依赖项,所以你需要 将它从 dist 文件夹中取出并执行它来自项目的主文件夹,以便它可以访问依赖项
这里有一个 link 应该告诉你你需要知道的关于它的一切:PyInstaller
- 所以这是我的代码,我真的很想让它成为一个独立的可执行文件,我正在使用 python 3.8.3 .
- 如果您有任何改进建议或设法找到方法,我将非常高兴。
- 我已经尝试过命令-cd C:\Users\chris\OneDrive\Desktop\K.A.T ai offline 然后输入然后输入命令- pyinstaller --onefile K.A.T_AI_offline.py
- 但它并没有结束工作,它确实在 dist 文件夹中给了我一个文件。
- 所以请你帮我制作一个独立的可执行文件,这样我就可以给我的计算机科学老师和朋友们看,而他们不必下载 python 和其他模块。
# All the modules being imported
import sys
import time
from tkinter import Tk, messagebox, simpledialog
import PySimpleGUI as sg
import pyttsx3
# The voice settings
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id) #changing index changes voices but ony 0 and 1 are working here
# This creates a dictionary
the_master = {}
the_world = {}
the_word= {}
# Defining how to read the info in capital_data.txt
def read_from_file():
with open('capital_data.txt') as file:
for line in file:
line = line.rstrip('\n')
country, city = line.split('/')
the_world[country] = city
# Defining how to write new info in new_data.txt
def write_to_file(country_name, city_name):
with open('new_data.txt', 'a') as file:
file.write('\n' + country_name + '/' + city_name)
def read_from_master_file():
with open('master_data.txt') as file:
for line in file:
line = line.rstrip('\n')
MName, MName = line.split('/')
the_master[MName]= MName
def write_to_master_file(master_name):
with open('master_data.txt', 'a') as file:
file.write('\n' + master_name + '/' + master_name)
def read_from_alphabet_file():
with open('phonetic_alphabet.txt') as file:
for line in file:
line = line.rstrip('\n')
letter, word = line.split('/')
the_word[letter] = word
# Popup box to ask wether K.A.T know this user
sg.theme('DarkBlack')
layout =[[sg.Text('Are You One Of My Masters')],
[sg.ReadFormButton('Yes'),sg.ReadFormButton('No')]]
form = sg.FlexForm('Do I Know You')
form.Layout(layout)
engine.say('are you already one of my masters')
engine.runAndWait()
button, values = form.Read()
if button is button == 'No':
form.close()
print ('Well In That Case')
print ('Hello user'), engine.say('well, in that case, Hello user'), engine.runAndWait(), print ('I am KAT'), engine.say('I am KAT'), engine.runAndWait(), print ('Which stands for Know.Alot.of.Things'), engine.say('Which stands for, Know a lot of things'), engine.runAndWait()
# see's if K.A.T know the Master
if button is button == 'Yes':
form.close()
engine.say('oh, so which master are you?')
engine.runAndWait()
root = Tk()
root.withdraw()
read_from_master_file()
query_master = simpledialog.askstring('Name Of Master', 'Which Master Are You?')
query_master = query_master.capitalize()
if query_master in the_master:
master_result = the_master[query_master]
engine.say('My apologies master,' + master_result)
engine.runAndWait()
choice_of_country = ('Would you like to know the Capital city of a Country')
choice_of_phonetic_alphabet = ('Would you like to know how to spell a word or letter in the Phonetic Alphabet')
yes_master = print (choice_of_country), engine.say('Would you like to know the Capital city of a Country.'), engine.runAndWait(), print ('OR'), engine.say('or.'), engine.runAndWait, print (choice_of_phonetic_alphabet), engine.say('Would you like to know how to spell a word or letter in the Phonetic Alphabet'), engine.runAndWait(), engine.say('Which one would you like to know, just type 1 or 2'), engine.runAndWait(),
choice = input('Which one would you like to know, just type 1 or 2')
else:
engine.say('sorry you are not one of my masters')
engine.runAndWait()
if button is button == 'No':
form.close()
engine.say('oh, and, please may you enter the name, that you wish to be called.')
engine.runAndWait()
root = Tk()
root.withdraw()
new_master = simpledialog.askstring('New Master',
'Oh And Please May You Enter the name that you wish to be called')
new_master = new_master.capitalize()
if new_master == 'Oliver' :
print('OH well in that case you can just')
engine.say('OH well in that case you can just')
print('GET LOST')
engine.say('GET LOST')
engine.runAndWait()
sys.exit(0)
elif new_master in the_master:
print('You Are A Master Silly.')
engine.say('you are already a master, silly, you, are master, ' + new_master)
engine.runAndWait()
write_to_master_file == False
elif new_master != 'Oliver':
write_to_master_file(new_master)
root.destroy()
engine.say('hello,'+new_master)
engine.runAndWait()
choice_of_country = ('Would you like to know the Capital city of a Country')
choice_of_phonetic_alphabet = ('Would you like to know how to spell a word or letter in the Phonetic Alphabet')
no_master = print (choice_of_country), engine.say('Would you like to know the Capital city of a Country.'), engine.runAndWait(), print ('OR'), engine.say('or.'), engine.runAndWait, print (choice_of_phonetic_alphabet), engine.say('Would you like to know how to spell a word or letter in the Phonetic Alphabet'), engine.runAndWait(), engine.say('Which one would you like to know, just type 1 or 2'), engine.runAndWait(),
choice = input('Which one would you like to know, just type 1 or 2')
if simpledialog in (None, 'Ok'):
root.destroy()
new_master = new_master.capitalize()
new_master = simpledialog.askstring('New Master',
'Oh And Please May You Enter the name that you wish to be called')
the_master[new_master] = new_master
if choice == '1' :
capital_cities = ('Well just type in the Country into the little window that will pop up and i\'ll tell you the Capital City')
print (capital_cities)
engine.say('Well just type in the Country into the little window that will pop up and i\'ll tell you the Capital City')
engine.runAndWait()
# Creates the Tkinter window
root = Tk()
root.withdraw()
read_from_file()
engine.say('Now, type the name of the country you would like to know the capital city of.')
engine.runAndWait()
while True:
query_country = simpledialog.askstring('Country', 'Type the name of a country:')
query_country = query_country.capitalize()
if query_country in the_world:
result = the_world[query_country]
messagebox.showinfo('Answer',
'The capital city of ' + query_country + ' is ' + result + '!')
engine.say('The capital city of ' + query_country + ' is ' + result)
engine.runAndWait()
else:
new_city = simpledialog.askstring('Teach me',
'I don\'t know! ' +
'What is the capital city of ' + query_country + '?')
engine.say('i don\'t know!'+
'what the capital city of'+query_country+'is'+
'but if you know you can type it in the box')
engine.runAndWait()
the_world[query_country] = new_city
write_to_file(query_country, new_city)
break
elif choice == '2' :
phonetic_alphabet = ('Well just type in the Word or Letter into the little window that will pop up and i\'ll tell you how to write it out, but in using the phonetic alphabet')
print (phonetic_alphabet)
read_from_alphabet_file()
engine.say('Well just type in the Word or Letter into the little window that will pop up and i\'ll tell you how to write it out, but in using the phonetic alphabet')
engine.runAndWait()
root = Tk()
root.withdraw()
while True:
query_word = simpledialog.askstring('Word', 'Type a word or letter :')
if query_word in the_word:
word_result = the_word[query_word]
messagebox.showinfo('Answer',
'The way you would say' + query_word + ' in the phonetic alphabet is ' +word_result + '!')
engine.say('the way you would say'+query_country+'in the phonetic alphabet is'+result)
engine.runAndWait()
break
我认为它对你不起作用的原因是因为你的 exe 有依赖项,所以你需要 将它从 dist 文件夹中取出并执行它来自项目的主文件夹,以便它可以访问依赖项
这里有一个 link 应该告诉你你需要知道的关于它的一切:PyInstaller