本帖最后由 safedragon 于 2023-12-1 11:32 编辑
初学者,闲来无事,给大佬加个界面,有能力的自己美化一番,已经写了出处了@zhurui
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
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:吾爱破解 @zhurui 翻译器')
root.geometry('700x500') # 设置窗口大小
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()
|