如何将Enter键绑定到tkinter中的函数? [英] How do I bind the enter key to a function in tkinter?
问题描述
我是Python入门自学者,在MacOS上运行.
I am a Python beginning self-learner, running on MacOS.
我正在tkinter中使用文本解析器GUI编写程序,在其中您在Entry
小部件中键入命令,然后单击Button
小部件,这将触发我的parse()
功能,以打印结果.到Text
小部件(文字冒险风格).
I'm making a program with a text parser GUI in tkinter, where you type a command in a Entry
widget, and hit a Button
widget, which triggers my parse()
funct, ect, printing the results to a Text
widget, text-adventure style.
>绕开按钮
> Circumvent the button
我不能让你那样,戴夫.
I can't let you do that, Dave.
我正在尝试寻找一种方法,以消除每次用户发出命令时都将鼠标拖到Button
上的需要,但是结果比我想象的要难.
I'm trying to find a way to get rid of the need to haul the mouse over to the Button
every time the user issues a command, but this turned out harder than I thought.
我猜正确的代码看起来像self.bind('<Return>', self.parse())
吗?但我什至不知道该放在哪里. root
,__init__
,parse()
和create_widgets()
不需要它.
I'm guessing the correct code looks like self.bind('<Return>', self.parse())
? But I don't even know where to put it. root
, __init__
, parse()
, and create_widgets()
don't want it.
要清楚,任何人都应该在编中按回车键的唯一原因是触发parse()
,因此不需要专门支持Entry
小部件.任何可行的方法都很好.
To be clear, the only reason anyone should hit enter in the prog is to trigger parse()
, so it doesn't need to be espoused to the Entry
widget specifically. Anywhere it works is fine.
响应7stud,基本格式:
In response to 7stud, the basic format:
from tkinter import *
import tkinter.font, random, re
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, ...)
self.grid()
self.create_widgets()
self.start()
def parse(self):
...
def create_widgets(self):
...
self.submit = Button(self, text= "Submit Command.", command= self.parse, ...)
self.submit.grid(...)
root = Tk()
root.bind('<Return>', self.parse)
app = Application(root)
root.mainloop()
推荐答案
尝试运行以下程序.您只需要确保在单击返回"时您的窗口具有焦点即可–要确保确实如此,请先多次单击按钮,直到看到一些输出,然后再单击其他任何位置,然后单击返回".
Try running the following program. You just have to be sure your window has the focus when you hit Return--to ensure that it does, first click the button a couple of times until you see some output, then without clicking anywhere else hit Return.
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
def func(event):
print("You hit return.")
root.bind('<Return>', func)
def onclick():
print("You clicked the button")
button = tk.Button(root, text="click me", command=onclick)
button.pack()
root.mainloop()
然后,当使button click
和hitting Return
都调用相同的函数时,您只需稍作调整-因为命令函数需要是一个不带参数的函数,而bind函数需要是一个不带参数的函数.需要一个参数(事件对象)的函数:
Then you just have tweak things a little when making both the button click
and hitting Return
call the same function--because the command function needs to be a function that takes no arguments, whereas the bind function needs to be a function that takes one argument(the event object):
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
def func(event):
print("You hit return.")
def onclick(event=None):
print("You clicked the button")
root.bind('<Return>', onclick)
button = tk.Button(root, text="click me", command=onclick)
button.pack()
root.mainloop()
或者,您可以放弃使用按钮的命令参数,而使用bind()将onclick函数附加到按钮,这意味着该函数需要一个参数-就像使用Return一样:
Or, you can just forgo using the button's command argument and instead use bind() to attach the onclick function to the button, which means the function needs to take one argument--just like with Return:
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
def func(event):
print("You hit return.")
def onclick(event):
print("You clicked the button")
root.bind('<Return>', onclick)
button = tk.Button(root, text="click me")
button.bind('<Button-1>', onclick)
button.pack()
root.mainloop()
这里是在课堂设置中:
import tkinter as tk
class Application(tk.Frame):
def __init__(self):
self.root = tk.Tk()
self.root.geometry("300x200")
tk.Frame.__init__(self, self.root)
self.create_widgets()
def create_widgets(self):
self.root.bind('<Return>', self.parse)
self.grid()
self.submit = tk.Button(self, text="Submit")
self.submit.bind('<Button-1>', self.parse)
self.submit.grid()
def parse(self, event):
print("You clicked?")
def start(self):
self.root.mainloop()
Application().start()
这篇关于如何将Enter键绑定到tkinter中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!