问题描述
我是 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.
>绕过按钮
我不能让你那样做,戴夫.
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.
需要明确的是,任何人都应该在 prog 中按 Enter 的唯一原因是触发 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()
推荐答案
尝试运行以下程序.你只需要确保你的窗口在你点击 Return 时有焦点——为了确保它有焦点,首先点击按钮几次直到你看到一些输出,然后不要点击其他任何地方点击 Return.
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
和 hiting Return
调用相同的函数时,您只需稍微调整一下 - 因为命令函数需要是一个函数不接受任何参数,而绑定函数需要是一个接受一个参数(事件对象)的函数:
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()
这篇关于如何将回车键绑定到 tkinter 中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!