cannot bind function in tkinter (gives error "takes 1 positional argument but 2 were given")

Viewed 23

I'm trying to bind the textbox and function, but keep giving errors ("Application. main() takes 1 positional argument but 2 were given"). I searched on google and most of them answered that putting "self" would solve the problem, but it doesn't work for me. Please teach me how to solve this problem.

here is my code:

import tkinter as tk
import PIL.Image
from PIL import Image, ImageTk
from tkinter import *
from pathlib import Path

OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path("./assets")

def relative_to_assets(path: str) -> Path:
    return ASSETS_PATH / Path(path)


class Application(tk.Tk):
    # 呪文
    
    def main(self):
        self.word= self.getUserInput()
        self.word = self.word.lower()
        resultA = self.y.checkWord(self.word)
        firstAlpha = self.y.getFirstAlpha(self.word)
        self.checkPrint(resultA)
        resultB = self.checkAlpha(firstAlpha)
        self.changeCondition(resultA, resultB)
        self.displayRecord(resultA, resultB, self.word)
        self.y.getTotalChar(resultA, resultB, self.word)

    def __init__(self, *args, **kwargs):
        self.y = gamePlay()
        # 呪文
        tk.Tk.__init__(self, *args, **kwargs)

        # ウィンドウタイトルを決定
        self.title("Homepage")

        # ウィンドウの大きさを決定
        self.geometry("703x981")

        self.frame3 = tk.Frame()
        self.frame3.grid(row=0, column=0, sticky= "nsew")

        self.entry_1 = tk.Entry(self.frame3, bd=0,bg="#D9D9D9", font=('Arial 24'), width=33)
        self.entry_1.place(x=109.0,y=450)


        self.entry_1.bind('<Return>', self.main)



class gamePlay:

      <<codes>>

gives error "Application.main() takes 1 positional argument but 2 were given" is there any problem with main method? or self.entry_1.bind('Return', self.main)?

1 Answers

The bind() callback is called with an Event argument. Either change main() to take another argument, or use a lambda to ignore the argument.

self.entry_1.bind('<Return>', lambda e: self.main())
Related