为什么.bind()方法与Tkinter中的框架小部件配合使用? [英] Why doesn't the .bind() method work with a frame widget in Tkinter?

查看:190
本文介绍了为什么.bind()方法与Tkinter中的框架小部件配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是将命令绑定到一个框架的尝试,即。当Escape键被按下时,窗口应该被销毁。

This code is an attempt to bind a command to a frame, ie. when the "Escape" key is pressed, the window should be destroyed.

from tkinter import *
from tkinter import ttk

root=Tk()
root.geometry("400x400")

frame1=ttk.Frame(root)
frame1.pack()

def Exit(event):
    root.destroy()

frame1.bind("<Escape>", Exit)

root.mainloop()

if frame1.bind() / code>被替换为 root.bind(),代码按照我的期望工作。为什么我上面写的不是上面的工作?

if frame1.bind() is replaced by root.bind(), the code works as I would expect it to. Why doesn't what I've written above work?

推荐答案

绑定工作,但事件只会触发帧有焦点,默认情况下,框架没有键盘焦点。

The bind works, but the event will only trigger if the frame has focus, and by default a frame does not have the keyboard focus.

尝试使用设置焦点frame1.focus_set()

这篇关于为什么.bind()方法与Tkinter中的框架小部件配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆