有什么方法可以优化代码以加快工作速度吗?
Is there any way to optimize the code for faster work?
我使用 Python 和 Tkinter 完成了翻译程序的编写。我使用了自动翻译,没有按钮,因此,应用程序变慢了很多。有什么方法可以优化或加快翻译过程吗?
如果您有任何建议,我很乐意倾听,但我是 Python 的新手。
And I'm also interested in the question, is it possible to change the text inside the combobox?
/主要
from languages import lang, lang_to_translate
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from googletrans import Translator
def defocus(event):
event.widget.master.focus_set()
def get_choice1(event):
global choice_language1
choice_language1 = language_selection1.get()
def get_choice2(event):
global choice_language2
choice_language2 = language_selection2.get()
dest = lang.index(choice_language2)
global language_abbreviation
language_abbreviation = lang_to_translate[dest]
print(language_abbreviation)
def ex_button():
c1 = language_selection1.get()
c2 = language_selection2.get()
label1.configure(text=c1)
label2.configure(text=c2)
language_selection1.set(c2)
language_selection2.set(c1)
get_choice1('event')
def do_translation(event):
choice_language1 = language_selection1.get()
choice_language2 = language_selection2.get()
text = text1.get("1.0", END).strip()
translator = Translator()
translated_text = translator.translate(text, dest=f'{language_abbreviation}')
text2.delete("1.0", END)
text2.insert(END, translated_text.text)
root = Tk()
app_width = 800
app_height = 500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y = (screen_height / 2) - (app_height / 2)
root.title('Переводчик')
root['bg'] = '#1D1B26'
root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
root.resizable(width=False, height=False)
language_selection1 = ttk.Combobox(root, values = lang, font='Comfortaa 10', )
language_selection1.current(1)
language_selection1.place(relx=0.16,y=50)
language_selection1.bind('<<ComboboxSelected>>', get_choice1)
language_selection1.bind('<FocusIn>', defocus)
label1 = Label(root)
exchange_button = PhotoImage(file='transfer.png')
img_label = Label(image=exchange_button)
exchange_button = exchange_button.subsample(18,18)
exchange_button1 = Button(root, image=exchange_button,background='#2ee59d',borderwidth=0, command=ex_button)
exchange_button1.place(relx=0.49,y=50)
language_selection2 = ttk.Combobox(root, values = lang, font='Comfortaa 10', )
language_selection2.set('Выберите язык')
language_selection2.place(relx=0.66,y=50)
language_selection2.bind('<<ComboboxSelected>>', get_choice2)
language_selection2.bind('<FocusIn>', defocus)
first_frame = Frame(root, bg='Black')
first_frame.place(x=41, y=100,width= 250, height=200) #127
text1 = Text(first_frame, bg = 'White')
text1.bind('<Any-KeyRelease>', do_translation)
text1.place(x=0,y=0,width= 250, height=200)
label2 = Label(root)
second_frame = Frame(root, bg='Black')
second_frame.place(x=528, y=100,width= 250, height=200) #441
text2 = Text(second_frame, bg = 'White')
text2.place(x=0,y=0,width= 250, height=200)
root.mainloop()
/语言
lang = ['Belarusian',
'English',
'German',
'Italian',
'Japanese',
'Kazakh',
'Kyrgyz',
'Norwegian',
'Polish',
'Russian',
'Spanish',
'Swedish',
'Turkish',
'Ukrainian', ]
lang_to_translate =['be',
'en',
'de',
'it',
'ja',
'kk',
'ky',
'no',
'pl',
'ru',
'es',
'sv',
'tr',
'uk', ]
Is there any way to optimize or speed up the translation process?
您不应该在用户每次按键时都尝试进行翻译。由于翻译需要超过几百毫秒,您应该等到用户停止输入,这样您就不会在每个字符之间强制暂停。
您可以使用 tkinter 的 after
方法安排翻译在将来发生。每次按键释放时,您都可以取消和重新安排翻译。如果用户停止打字一两秒钟,就会进行翻译。
我无法 运行 你的代码,但这里是该技术的简要说明:
import tkinter as tk
import googletrans
after_id = None
translator = googletrans.Translator()
def do_translation():
source = input_text.get("1.0", "end").strip()
translated = translator.translate(source, dest="it")
output_text.delete("1.0", "end")
output_text.insert("end", translated.text)
def schedule_translation(event):
global after_id
if after_id is not None:
event.widget.after_cancel(after_id)
after_id = event.widget.after(1000, do_translation)
root = tk.Tk()
input_text = tk.Text(root)
output_text = tk.Text(root)
input_text.pack(side="left", fill="both", expand=True)
output_text.pack(side="left", fill="both", expand=True)
input_text.bind("<Any-KeyRelease>", schedule_translation)
root.mainloop()
要获得更强大的解决方案,您可以在单独的线程或进程中进行转换,但多线程和多处理会增加很多复杂性。
我使用 Python 和 Tkinter 完成了翻译程序的编写。我使用了自动翻译,没有按钮,因此,应用程序变慢了很多。有什么方法可以优化或加快翻译过程吗? 如果您有任何建议,我很乐意倾听,但我是 Python 的新手。 And I'm also interested in the question, is it possible to change the text inside the combobox?
/主要
from languages import lang, lang_to_translate
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from googletrans import Translator
def defocus(event):
event.widget.master.focus_set()
def get_choice1(event):
global choice_language1
choice_language1 = language_selection1.get()
def get_choice2(event):
global choice_language2
choice_language2 = language_selection2.get()
dest = lang.index(choice_language2)
global language_abbreviation
language_abbreviation = lang_to_translate[dest]
print(language_abbreviation)
def ex_button():
c1 = language_selection1.get()
c2 = language_selection2.get()
label1.configure(text=c1)
label2.configure(text=c2)
language_selection1.set(c2)
language_selection2.set(c1)
get_choice1('event')
def do_translation(event):
choice_language1 = language_selection1.get()
choice_language2 = language_selection2.get()
text = text1.get("1.0", END).strip()
translator = Translator()
translated_text = translator.translate(text, dest=f'{language_abbreviation}')
text2.delete("1.0", END)
text2.insert(END, translated_text.text)
root = Tk()
app_width = 800
app_height = 500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y = (screen_height / 2) - (app_height / 2)
root.title('Переводчик')
root['bg'] = '#1D1B26'
root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
root.resizable(width=False, height=False)
language_selection1 = ttk.Combobox(root, values = lang, font='Comfortaa 10', )
language_selection1.current(1)
language_selection1.place(relx=0.16,y=50)
language_selection1.bind('<<ComboboxSelected>>', get_choice1)
language_selection1.bind('<FocusIn>', defocus)
label1 = Label(root)
exchange_button = PhotoImage(file='transfer.png')
img_label = Label(image=exchange_button)
exchange_button = exchange_button.subsample(18,18)
exchange_button1 = Button(root, image=exchange_button,background='#2ee59d',borderwidth=0, command=ex_button)
exchange_button1.place(relx=0.49,y=50)
language_selection2 = ttk.Combobox(root, values = lang, font='Comfortaa 10', )
language_selection2.set('Выберите язык')
language_selection2.place(relx=0.66,y=50)
language_selection2.bind('<<ComboboxSelected>>', get_choice2)
language_selection2.bind('<FocusIn>', defocus)
first_frame = Frame(root, bg='Black')
first_frame.place(x=41, y=100,width= 250, height=200) #127
text1 = Text(first_frame, bg = 'White')
text1.bind('<Any-KeyRelease>', do_translation)
text1.place(x=0,y=0,width= 250, height=200)
label2 = Label(root)
second_frame = Frame(root, bg='Black')
second_frame.place(x=528, y=100,width= 250, height=200) #441
text2 = Text(second_frame, bg = 'White')
text2.place(x=0,y=0,width= 250, height=200)
root.mainloop()
/语言
lang = ['Belarusian',
'English',
'German',
'Italian',
'Japanese',
'Kazakh',
'Kyrgyz',
'Norwegian',
'Polish',
'Russian',
'Spanish',
'Swedish',
'Turkish',
'Ukrainian', ]
lang_to_translate =['be',
'en',
'de',
'it',
'ja',
'kk',
'ky',
'no',
'pl',
'ru',
'es',
'sv',
'tr',
'uk', ]
Is there any way to optimize or speed up the translation process?
您不应该在用户每次按键时都尝试进行翻译。由于翻译需要超过几百毫秒,您应该等到用户停止输入,这样您就不会在每个字符之间强制暂停。
您可以使用 tkinter 的 after
方法安排翻译在将来发生。每次按键释放时,您都可以取消和重新安排翻译。如果用户停止打字一两秒钟,就会进行翻译。
我无法 运行 你的代码,但这里是该技术的简要说明:
import tkinter as tk
import googletrans
after_id = None
translator = googletrans.Translator()
def do_translation():
source = input_text.get("1.0", "end").strip()
translated = translator.translate(source, dest="it")
output_text.delete("1.0", "end")
output_text.insert("end", translated.text)
def schedule_translation(event):
global after_id
if after_id is not None:
event.widget.after_cancel(after_id)
after_id = event.widget.after(1000, do_translation)
root = tk.Tk()
input_text = tk.Text(root)
output_text = tk.Text(root)
input_text.pack(side="left", fill="both", expand=True)
output_text.pack(side="left", fill="both", expand=True)
input_text.bind("<Any-KeyRelease>", schedule_translation)
root.mainloop()
要获得更强大的解决方案,您可以在单独的线程或进程中进行转换,但多线程和多处理会增加很多复杂性。