如何在Python中的Tkinter的窗口小部件上使用Tcl/Tk绑定函数? [英] how to use Tcl/Tk bind function on Tkinter's widgets in Python?

查看:117
本文介绍了如何在Python中的Tkinter的窗口小部件上使用Tcl/Tk绑定函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管Tkinter是从Tcl/Tk派生的,但它不如Tcl/Tk完整. Tcl/Tk绑定函数具有tkinter不具备的一些属性(例如,%d返回事件

Although Tkinter's derived from Tcl/Tk but it's not as complete as Tcl/Tk. Tcl/Tk bind function has some attributes which tkinter doesn't (e.g. %d that returns The detail field from the event https://www.tcl.tk/man/tcl8.4/TkCmd/bind.htm#M24).
Tcl/Tk scripts can be used by "eval" function in python but i don't know how to declare a tkinter widget in Tcl/Tk script.

那么我如何在Tkinter小部件上使用此功能及其属性?

so how can i use this function and its attributes on Tkinter widget?

推荐答案

如果您要询问如何创建一个使用数据"字段的绑定(即:%d替换),则必须调用一些tcl代码可以实现这一目标.这需要两个步骤:创建一个调用python函数的tcl命令,并使用tcl将事件绑定到该函数.

If you are asking how to create a binding that makes use of the "data" field (ie: %d substitution), you will have to call some tcl code to make that happen. This requires two steps: create a tcl command that calls a python function, and use tcl to bind an event to that function.

首先,让我们创建一个可以创建事件并设置数据"字段的python程序(假定存在名为root的全局变量,稍后将创建).调用此函数时,它将生成一个自定义事件,其中的数据字段由字符串填充.

First, let's create a python program that can create an event and set the "data" field (this assumes the existence of a global variable named root, which we'll create later). When this function is called, it will generate a custom event with the data field populated by a string.

def create_custom_event():
    root.event_generate("<<Custom>>", data="Hello, world")

接下来,让我们创建一个程序来在按下按钮时调用该功能

Next, lets create a program to call that function on a button press

import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text="click me", command=create_custom_event)
button.pack(side="top", padx=20, pady=20)
root.mainloop()

接下来,我们需要创建一个在生成事件时调用的函数.我们计划设置data字段,因此此函数必须接受一个值,该值是%d替换的值.

Next we need to create a function that is called when the event is generated. We plan to set the data field, so this function must accept one value which is the value of the %d substitution.

def callback(detail):
    print("detail: %s" % detail)

因为要使用%d替换,所以我们必须通过Tcl创建绑定.但是,tcl不会自动了解我们的python函数,因此我们必须向tcl注册该函数.然后,只需通过tcl接口调用bind来设置绑定即可.

Because you want to use the %d substitution we'll have to create the binding via Tcl. However, tcl doesn't automatically know about our python functions so we have to register the function with tcl. Then it's just a matter of calling bind via the tcl interface to set up the binding.

然后,第一步是注册回调.我们已经创建了函数,我们只需要创建一个调用该函数的tcl函数即可.幸运的是,tkinter为我们提供了一种使用register命令执行此操作的方法.您可以这样使用它:

The first step, then, is to register the callback. We've already created the function, we just have to create a tcl function that calls this function. Fortunately, tkinter gives us a way to do that with the register command. You use it like this:

cmd = root.register(callback)

这需要一个python函数(在本例中为callback),并创建一个tcl命令来调用该函数. register返回该tcl命令的名称,该名称存储在名为cmd的变量中(名称无关)

This takes a python function (callback in this case), and creates a tcl command which will call that function. register returns the name of that tcl command which we store in a variable named cmd (the name is irrelevant)

接下来,我们需要通过Tcl建立绑定以调用此命令.如果我们在实际的tcl脚本中执行此操作,则该命令将类似于以下内容(其中."代表根窗口):

Next, we need to set up a binding via Tcl to call this command. If we were doing this in an actual tcl script, the command would look something like this (where "." represents the root window):

bind . <<Custom>> {callback %d}

等效的python是这样的:

The python equivalent is this:

root.tk.call("bind", root, "<<Custom>>", cmd + " %d")

请注意,call的参数与tcl参数之间存在1:1的对应关系.方便的是,tkinter小部件的默认字符串表示形式是内部tcl名称,因此我们可以在调用中直接使用该小部件(尽管,从理论上讲,也许我们应该使用str(root)).

Notice that there is a 1:1 correspondence between an argument to call and a tcl argument. Conveniently, the default string representation of a tkinter widget is the internal tcl name, so we can directly use the widget in the call (though, pedantically, perhaps we should use str(root)).

将所有内容放在一起可以为我们提供帮助,当您单击按钮时,它会打印出详细信息:Hello,world":

Putting it all together gives us this, which prints out "detail: Hello, world" when you click the button:

import tkinter as tk

def callback(detail):
    print("detail: %s" % detail)

def create_custom_event():
    root.event_generate("<<Custom>>", data="Hello, world")

root = tk.Tk()

button = tk.Button(root, text="click me", command=create_custom_event)
button.pack(side="top", padx=20, pady=20)

cmd = root.register(callback)
root.tk.call("bind", root, "<<Custom>>", cmd + " %d")

root.mainloop()

这篇关于如何在Python中的Tkinter的窗口小部件上使用Tcl/Tk绑定函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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