Home > Software design >  TypeError: schedule_translation() missing 1 required positional argument: 'event'
TypeError: schedule_translation() missing 1 required positional argument: 'event'

Time:12-17

I'm new to python and therefore I can't figure out what the problem is. When I try to call the function: def schedule_translation(event), an error appears in the function: def ex_button(). TypeError: schedule_translation() missing 1 required positional argument: 'event'

from languages import lang, lang_to_translate
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from googletrans import Translator
import pyperclip as clipboard

after_id = None
language_abbreviation = []

def copy_text():
    text = text2.get('1.0', END).strip()
    clipboard.copy(text)

def clear_text():
    text1.delete('1.0', END)

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()

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')
    get_choice2('event')

    text = text2.get('1.0', END).strip()
    text2.delete('1.0', END)
    text1.delete('1.0', END)
    text1.insert(1.0, text)

    schedule_translation()


def do_translation():
    pass
    # global language_abbreviation
    # global choice_language1
    # global choice_language2
    # dest = lang.index(choice_language2)
    # language_abbreviation = lang_to_translate[dest]

    #text = text1.get('1.0', END).strip()

    # translator = Translator()
    # translated_text = translator.translate(text, dest=language_abbreviation)
    #
    # text2.delete('1.0', END)
    # text2.insert(END, translated_text.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(500, do_translation)


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,
                          bg='#2ee59d',highlightthickness=0,
                          bd=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='#FFC0CB')
text1.bind('<Any-KeyRelease>', schedule_translation)
text1.pack(side="left", fill="both", expand=True)

clear_button = PhotoImage(file='clear_button.png')
clear_button_label = Label(image=clear_button)
clear_button = clear_button.subsample(25, 25)
clear_button1 = Button(text1, image=clear_button,
                       highlightthickness = 0, bd = 0,bg='#FFC0CB',
                       activebackground='#FFC0CB', command=clear_text)
clear_button1.pack(side='top', anchor='e', pady=5, padx=5)

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='#FFC0CB')
text2.pack(side="left", fill="both", expand=True)

copying_text = PhotoImage(file='copying_text.png')
copying_text_label = Label(image=copying_text)
copying_text = copying_text.subsample(18, 18)
copying_text1 = Button(text2, image=copying_text,
                       highlightthickness = 0, bd = 0, bg='#FFC0CB',
                       activebackground='#FFC0CB', command=copy_text)
copying_text1.pack(side='top', anchor='e')

root.mainloop()

P.S: I commented out the def do_translation() function: because Google issues a ban for frequent requests.

CodePudding user response:

Based on the current design of schedule_translation(), it can be re-designed to not depending on the passed event argument and make it having default value as below:

# set default value to argument event
def schedule_translation(event=None):
    global after_id
    # use root.after() instead of event.widget.after()
    if after_id is not None:
        root.after_cancel(after_id)
    after_id = root.after(500, do_translation)

Then the function can be called with (via binding) or without (via direct call) the event argument.

  • Related