Python 如何将同一事件绑定到不同帧中的不同按钮

Python 如何将同一事件绑定到不同帧中的不同按钮,python,tkinter,frame,Python,Tkinter,Frame,我用不同的框架编写了一个应用程序。在每个帧中,有一个按钮,我想绑定到键盘的返回按钮。当我在一个特定的帧上时,我想按下“返回按钮”,按钮返回到特定的功能 我尝试了frame.bind,但它不起作用 from tkinter import * from tkinter import ttk root=Tk() root.geometry("500x500") def enter_frame1(event=None): Label(my_frame1, text=&q

我用不同的框架编写了一个应用程序。在每个帧中,有一个按钮,我想绑定到键盘的返回按钮。当我在一个特定的帧上时,我想按下“返回按钮”,按钮返回到特定的功能

我尝试了
frame.bind
,但它不起作用

from tkinter import *
from tkinter import ttk

root=Tk()
root.geometry("500x500")


def enter_frame1(event=None):
    Label(my_frame1, text="Say hi").pack()

def enter_frame2(event=None):
    Label(my_frame2, text="Be Happy").pack()

my_notebook=ttk.Notebook(root)
my_notebook.pack(pady=15)

my_frame1= Frame(my_notebook, width=500, height=500)
my_frame2= Frame(my_notebook, width=500, height=500)

my_frame1.pack()
my_frame2.pack()

my_notebook.add(my_frame1, text=1)
my_notebook.add(my_frame2, text=2)

Button(my_frame1, text='Enter', command=enter_frame1).pack()
Button(my_frame2, text='Enter', command=enter_frame2).pack()

my_frame1.bind('<Return>', enter_frame1)
my_frame2.bind('<Return>', enter_frame2)

root.mainloop()
从tkinter导入*
从tkinter导入ttk
root=Tk()
根几何(“500x500”)
def enter_frame1(事件=无):
标签(my_frame1,text=“Say hi”).pack()
def enter_frame2(事件=无):
标签(my_frame2,text=“Be Happy”).pack()
my_notebook=ttk.notebook(根目录)
我的笔记本包(pady=15)
my_frame1=框架(my_笔记本,宽度=500,高度=500)
my_frame2=框架(my_笔记本,宽度=500,高度=500)
我的_frame1.pack()
我的框架2.pack()
my_notebook.add(my_frame1,text=1)
my_notebook.add(my_frame2,text=2)
按钮(my_frame1,text='Enter',command=Enter_frame1).pack()
按钮(my_frame2,text='Enter',command=Enter_frame2).pack()
我的框架1.bind(“”,输入框架1)
我的框架2.bind(“”,输入框架2)
root.mainloop()

请尝试在root.mainloop()之前添加以下两行代码


请在评论中写下您想要的结果是什么?

您应该将焦点设置为该特定键。请参阅。请参阅我的编辑plsChange
def enter\u frame1(事件)
def enter\u frame1(事件=无)
。对
enter_frame2
执行同样的操作,但是当我按下enter按钮时,没有结果。谢谢!
my_frame1.focus()
my_frame2.focus()