如何将回车键绑定到tkinter中的函数?

问题描述 投票:26回答:2

我是一个Python开始自学者,在MacOS上运行。

我在tkinter中创建了一个带有文本解析器GUI的程序,在Entry小部件中键入命令,然后点击Button小部件,触发我的parse()功能,等等,将结果打印到Text小部件,文本冒险样式。

>绕过按钮

戴夫,我不能让你这样做。

我试图找到一种方法来摆脱每次用户发出命令时将鼠标拖到Button上的需要,但事实证明这比我想象的更难。

我猜测正确的代码看起来像self.bind('<Return>', self.parse())?但我甚至不知道把它放在哪里。 root__init__parse()create_widgets()不想要它。

要清楚,任何人都应该在prog中输入的唯一原因是触发parse(),因此不需要特别支持Entry小部件。它工作的任何地方都很好。

针对7stud,基本格式:

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()
python python-3.x tkinter key-bindings
2个回答
42
投票

尝试运行以下程序。当你点击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 clickhitting Return调用相同的函数 - 因为命令函数需要是一个不带参数的函数,而bind函数需要是一个带有一个参数的函数(事件对象):

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:

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()

7
投票

另一种选择是使用lambda:

ent.bind("<Return>", (lambda event: name_of_function()))

完整代码:

from tkinter import *
from tkinter.messagebox import showinfo

def reply(name):
    showinfo(title="Reply", message = "Hello %s!" % name)


top = Tk()
top.title("Echo")
top.iconbitmap("Iconshock-Folder-Gallery.ico")

Label(top, text="Enter your name:").pack(side=TOP)
ent = Entry(top)
ent.bind("<Return>", (lambda event: reply(ent.get())))
ent.pack(side=TOP)
btn = Button(top,text="Submit", command=(lambda: reply(ent.get())))
btn.pack(side=LEFT)

top.mainloop()

如您所见,使用未使用的变量“event”创建lambda函数可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.