繁体   English   中英

我如何使用我定义的 function 作为参数来激活另一个 function?

[英]How could I use my defined function as an argument to activate another function?

我想要做的是在文本框为空时将按钮状态设置为 DISABLED。 我在下面重新创建了一段代码:

import tkinter
from tkinter import *

root = tkinter.Tk()

textbox = tkinter.Text(root, height=4)
textbox.pack()

button = tkinter.Button(text="Button", height=2, width=20, state=ACTIVE)
button.pack()


def check_if_textbox_is_empty():
    if textbox.get("1.0", tkinter.END) == "":
        return True


def if_textbox_is_empty():
    button["state"] = DISABLED

# Here is the .bind method I tried, but since it doesn't take func as the first arg it doesn't work for me.
# root.bind(check_if_textbox_is_empty, if_textbox_is_empty)


root.mainloop()

go应该怎么办呢?

我想要发生的是,当我的 tkinter.Text 中没有文本时 textbox.get textbox.get("1.0", tkinter.END) == ""我希望我的按钮小部件被禁用button["state"] = DISABLED 我尝试过的是.bind方法,但它似乎只需要 tkinter 个事件,例如: "<Keypress>"作为参数。

基于正确的@JRiggles 答案

仅当我在if textbox.get("1.0", tkinter.END).strip() == "": button started change state 中添加.strip()时。

import tkinter as tk

root = tk.Tk()

textbox = tk.Text(root, height=4)
textbox.pack()

button = tk.Button(text="Button", height=2, width=20, state=tk.ACTIVE)
button.pack()


def on_textbox_change(_event=None):
    if textbox.get("1.0", tk.END).strip() == "":
        button["state"] = tk.DISABLED
    else:
        button["state"] = tk.NORMAL


textbox.bind('<KeyRelease>', on_textbox_change)

root.mainloop()

首先:你不需要两者

import tkinter

from tkinter import *

通常的做法是

import tkinter as tk

之后你可以像这样实例化小部件(注意 state 的常量如何tk. - 正如你可能已经猜到的那样,这些常量是 tkinter 的一部分)

button = tk.Button(text="Button", height=2, width=20, state=tk.ACTIVE)

但是,暂时坚持使用import tkinter


有了这个...

为什么不简单地在check_if_textbox_is_empty()if子句中启用/禁用按钮? 您可以将'<KeyRelease>'事件绑定到您的文本框并让它调用检查 function。

您有多种选择,所有这些最终都将以相同的方式工作。


选项1

给 check_if_textbox_is_empty 添加一个_event参数check_if_textbox_is_empty

前导下划线_是让人们知道该值未被 function 使用的约定, event是 tkinter 中事件驱动函数采用的事件参数的约定名称。默认值None不是绝对必要的,但是这是很好的做法。

def check_if_textbox_is_empty(_event = None):
    if textbox.get("1.0", tkinter.END) == "":
        button["state"] = tkinter.DISABLED
    else:
        button["state"] = tkinter.NORMAL


# bind an event handler to your textbox to check it as the user types
textbox.bind('<KeyRelease>', check_if_textbox_is_empty)

选项 2

在您的 function 中使用*_args以允许它接受任意数量的 arguments。同样, _是未使用值的约定,而args是此类参数的约定

def check_if_textbox_is_empty(*_args):  # accept any number of arguments
    if textbox.get("1.0", tkinter.END) == "":
        button["state"] = tkinter.DISABLED
    else:
        button["state"] = tkinter.NORMAL


# bind an event handler to your textbox to check it as the user types
textbox.bind('<KeyRelease>', check_if_textbox_is_empty)

选项 3

使用lambda吸收事件并调用check...作为匿名 function

def check_if_textbox_is_empty():  # no params needed!
    if textbox.get("1.0", tkinter.END) == "":
        button["state"] = tkinter.DISABLED
    else:
        button["state"] = tkinter.NORMAL


# bind an event handler to your textbox to check it as the user types
textbox.bind('<KeyRelease>', lambda _event: check_if_textbox_is_empty())

奖金信息!

添加_event参数(或使用*_args吸收它或使用lambda )的原因是 tkinter 事件通常在触发时生成一个event值,因此您的代码必须能够容纳该值。

当您将 function 绑定到一个事件并触发该事件时,无论您是否愿意event object 都会传递给您的 function!

您可能希望 function 使用event的值的原因有很多,但对于这种特殊情况,您不需要它。 您可以使用上述任何一种方法基本上将其安全地扔掉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM