Binding a function to a key is not working

后端 未结 1 838
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 15:48

My code:

import tkinter

master = tkinter.Tk()
master.title(\"test1\")
master.geometry(\"300x300\")

masterFrame = tkinter.Frame(master)

masterFrame.pack(fill=t         


        
相关标签:
1条回答
  • 2021-01-26 16:27

    When you bind a function fct to a key (or any other kind of event), the function is called with one argument like that fct(event), event having various attributes depending on the kind of event (mouse position, ...). Your problem is that the function you call drawCheckbox does not take any argument, so every time you press Enter, it raises an error

    TypeError: drawCheckbox() takes 0 positional arguments but 1 was given

    To correct it you can either define your function with a default argument,

    def drawCheckbox(event=None):
        ...
    

    or you can use a lambda function to do the binding

    master.bind('<Return>', lambda event: drawCheckbox())
    
    0 讨论(0)
提交回复
热议问题