Home > other >  Why Button is not working in tkinter, when i click it it shows an error
Why Button is not working in tkinter, when i click it it shows an error

Time:06-07

I am trying this code. I want to use don function for bind() and command. It is showing don() missing 1 required positional argument: 'Event'. how to fix it

my code

from tkinter import *
root = Tk()
root.geometry("600x500")

def don(Event):
    print("hello")


root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()

root.mainloop()

CodePudding user response:

All you need to do is get rid of Event or as the person said above me Event=None. Depends if you are going to build on it later and add some parameters

from tkinter import *
root = Tk()
root.geometry("600x500")

def don():
    print("hello")


root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()

root.mainloop()

CodePudding user response:

If you want to call don() with param, you can use lambda. It is allowed even without param

from tkinter import *
root = Tk()
root.geometry("600x500")


def don(event=None):
    event = "hello" if event is None else event
    print(event)


root.bind("<Return>", don)
test = "OK"
Button(root, text="check! ", command=lambda: don(test)).pack()

root.mainloop()

By the way, .pack() return None. If you want get object Button, please modify your code like this

btn = Button(root, text="check! ", command=lambda: don(test))
btn.pack()
  • Related