python 生成的exe一直循环运行
本帖最后由 风韵天成 于 2023-12-6 16:23 编辑python 抄论坛网友的自动翻译代码,让chatgpt稍微改了一下,生成的exe一直循环运行关都关不掉,大佬帮看一下哪里不对劲!!!!
我是纯纯小白!chatgpt改了几次都不行,exe文件还是一直循环运行。
# -*- coding: utf-8 -*-
import PyInstaller.__main__
import requests
from requests.exceptions import RequestException
from json import JSONDecodeError
import tkinter as tk
from tkinter.font import Font
def translate_text(event=None):
text = entry.get()
url = f"https://findmyip.net/api/translate.php?text={text}"
response = requests.get(url)
try:
data = response.json()
if response.status_code == 200:
if data['code'] == 200:
translation = data['data']['translate_result']
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, translation)
result.config(state='disabled')
elif data['code'] == 400:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, data['error'])
result.config(state='disabled')
else:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, "内部接口错误,请联系开发者")
result.config(state='disabled')
else:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, "内部接口错误,请联系开发者")
result.config(state='disabled')
except JSONDecodeError as e:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, f"JSON decoding error: {e}")
result.config(state='disabled')
except RequestException as e:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, f"Request error: {e}")
result.config(state='disabled')
root = tk.Tk()
root.title('by:今领图文广告 @Mark 翻译器')
root.geometry('700x200')# 设置窗口大小
fontStyle = Font(family="Lucida Grande", size=20)# 设置字体和大小
frame = tk.Frame(root)
frame.pack(fill=tk.X)
label = tk.Label(frame, text='请输入需要翻译的文字:', font=fontStyle)
label.pack(side=tk.LEFT)
entry = tk.Entry(frame, font=fontStyle)
entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
entry.bind('<Return>', translate_text)# 绑定回车键
button = tk.Button(frame, text='翻译', command=translate_text, font=fontStyle)
button.pack(side=tk.RIGHT)
result = tk.Text(root, state='disabled', font=fontStyle)
result.pack(fill=tk.BOTH, expand=True)
root.mainloop()
# 自动生成 exe 文件的命令
pyinstaller_command = [
'--onefile', # 生成单一可执行文件
'--noconsole',# 隐藏控制台窗口(如果你不希望显示)
'翻译软件.py'# 将 'your_script.py' 替换为你的实际 Python 脚本文件名
]
# 运行 PyInstaller
PyInstaller.__main__.run(pyinstaller_command)
本帖最后由 打金者BT 于 2023-12-6 17:35 编辑
import requests
from requests.exceptions import RequestException
from json import JSONDecodeError
import tkinter as tk
from tkinter.font import Font
def translate_text(event=None):
text = entry.get()
url = f"https://findmyip.net/api/translate.php?text={text}"
response = requests.get(url)
try:
data = response.json()
if response.status_code == 200:
if data['code'] == 200:
translation = data['data']['translate_result']
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, translation)
result.config(state='disabled')
elif data['code'] == 400:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, data['error'])
result.config(state='disabled')
else:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, "内部接口错误,请联系开发者")
result.config(state='disabled')
else:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, "内部接口错误,请联系开发者")
result.config(state='disabled')
except JSONDecodeError as e:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, f"JSON decoding error: {e}")
result.config(state='disabled')
except RequestException as e:
result.config(state='normal')
result.delete(1.0, tk.END)
result.insert(tk.END, f"Request error: {e}")
result.config(state='disabled')
root = tk.Tk()
root.title('by:今领图文广告 @mark 翻译器')
root.geometry('700x200')# 设置窗口大小
fontStyle = Font(family="Lucida Grande", size=20)# 设置字体和大小
frame = tk.Frame(root)
frame.pack(fill=tk.X)
label = tk.Label(frame, text='请输入需要翻译的文字:', font=fontStyle)
label.pack(side=tk.LEFT)
entry = tk.Entry(frame, font=fontStyle)
entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
entry.bind('<Return>', translate_text)# 绑定回车键
button = tk.Button(frame, text='翻译', command=translate_text, font=fontStyle)
button.pack(side=tk.RIGHT)
result = tk.Text(root, state='disabled', font=fontStyle)
result.pack(fill=tk.BOTH, expand=True)
root.mainloop()
命令行:pyinstaller -F xxx.py
注:生成单exe文件,显示控制台窗口
命令行:pyinstaller -F -w xxx.py
注:生成单exe文件,不显示控制台窗口
试试看是不是你需要的样子,生成的exe文件在dist文件夹中,还有源码中那个url好像已经不能用了。
本帖最后由 bin2163239 于 2023-12-6 20:54 编辑
import requests
from tkinter import Tk, Frame, Label, Entry, Button, Text, font
def translate_text(event=None):
text = entry.get()
url = f"https://findmyip.net/api/translate.php?text={text}"
response = requests.get(url)
try:
data = response.json()
if response.status_code == 200:
if data['code'] == 200:
translation = data['data']['translate_result']
elif data['code'] == 400:
translation = data['error']
else:
translation = "内部接口错误,请联系开发者"
else:
translation = "内部接口错误,请联系开发者"
except (JSONDecodeError, RequestException) as e:
translation = f"错误: {e}"
result.config(state='normal')
result.delete(1.0, 'end')
result.insert('end', translation)
result.config(state='disabled')
root = Tk()
root.title('by:今领图文广告 @mark 翻译器')
# 获取屏幕宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 设置窗口大小和位置
window_width = 700
window_height = 200
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f'{window_width}x{window_height}+{x}+{y}')
fontStyle = font.Font(family="Lucida Grande", size=20)# 设置字体和大小
frame = Frame(root)
frame.pack(fill='x')
label = Label(frame, text='请输入需要翻译的文字:', font=fontStyle)
label.pack(side='left')
entry = Entry(frame, font=fontStyle)
entry.pack(side='left', fill='x', expand=True)
entry.bind('<Return>', translate_text)# 绑定回车键
button = Button(frame, text='翻译', command=translate_text, font=fontStyle)
button.pack(side='right')
result = Text(root, state='disabled', font=fontStyle)
result.pack(fill='both', expand=True)
root.mainloop() 本帖最后由 风韵天成 于 2023-12-6 16:25 编辑
{:1_904:} 各位大佬指正,小弟不胜感激!!! Chatgpt还可以改代码,好厉害啊 打金者BT 发表于 2023-12-6 17:33
import requests
from requests.exceptions import RequestException
from jso ...
URL 可以用。但是还是生不成exe,要不还是循环! pyinstaller -F -w 123.py打包单文件运行没问题啊,可以关掉。 写一个判断,生成exe就跳出循环就解决了 先学走,在学飞 本帖最后由 打金者BT 于 2023-12-7 09:08 编辑
风韵天成 发表于 2023-12-6 18:24
URL 可以用。但是还是生不成exe,要不还是循环!
可以生成啊,我这边都没问题,你是怎么操作的{:1_904:}
页:
[1]
2