简体   繁体   中英

Why will my button work, but not my key bind?

In this section of the code, I try to set the button's command to goRun , and also bind the Return key to goRun .

def goRun():
    Run.runData(ENTRY=symbolEntry.get(), GREATER=greaterEntry.get(), BETWEEN=betweenEntry.get(), LESSER=lesserEntry.get())
    #^ "Run" is a seperate class.

button = Button(app, text="Go Data", command=goRun, fg="blue")
button.pack(side="top", pady=2, padx=10)

app.bind('<Return>', goRun)

When I run the app, and press the Return key, I get this error:

TypeError: goRun() takes no arguments (1 given)

But when I press the button, it runs the command and works just fine.

What am I doing wrong?

Assuming the GUI library you are using is tkinter , this is because when a callback is activated by a keypress, a parameter called event is passed. Try modifying your function definition so it is like this:

def goRun(event=None):

So you are getting the error as your function received a parameter with which it doesn't have a clue what to do. With the event=None we are saying "Don't worry about that argument when it comes along, it's nothing."

You get this error with the Return key because it gives this argument, but it works with your Button as it does not send the argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM