【问题标题】:Can a button's command use variables from the function including the button?按钮的命令可以使用函数中的变量,包括按钮吗?
【发布时间】:2021-08-09 14:06:52
【问题描述】:

我有一个函数可以创建一些标签和输入字段以及一个按钮。我希望按钮的命令功能管理此功能的对象。我可以在不使变量全局化的情况下做到这一点吗?

代码如下:

def add_clicked():
    cuspsInfo.append((int(xInput.get()), int(yInput.get()), indexInput.get()))
    xInput.delete(0, END)
    yInput.delete(0, END)
    indexInput.delete(0, END)

def startup_menu():
    global cuspsInfo
    cuspsInfo = []
    global label
    label = Label(window, text="Add a cusp:", font=("Arial", 20, "bold"), 
        fg=SECOND_COLOR, bg=FIRST_COLOR)
    label.place(relx=0.5, rely=0.3, anchor=CENTER)

    global xInput
    global yInput
    global indexInput
    xInput = Entry(window)
    xInput.place(relx=0.52, rely=0.4, anchor=CENTER)
    yInput = Entry(window)
    yInput.place(relx=0.52, rely=0.45, anchor=CENTER)
    indexInput = Entry(window)
    indexInput.place(relx=0.52, rely=0.5, anchor=CENTER)

    global xLabel
    global yLabel
    global indexLabel
    xLabel = Label(window, text="x(0-500):", font=(
        "Arial", 20, "bold"), fg=SECOND_COLOR, bg=FIRST_COLOR)
    xLabel.place(relx=0.34, rely=0.4, anchor=CENTER)
    yLabel = Label(window, text="y(0-500):", font=(
        "Arial", 20, "bold"), fg=SECOND_COLOR, bg=FIRST_COLOR)
    yLabel.place(relx=0.34, rely=0.45, anchor=CENTER)
    indexLabel = Label(window, text="name:", font=(
        "Arial", 20, "bold"), fg=SECOND_COLOR, bg=FIRST_COLOR)
    indexLabel.place(relx=0.36, rely=0.5, anchor=CENTER)

    global addButton
    global runButton
    addButton = Button(window, text="add", command=add_clicked)
    addButton.place(relx=0.45, rely=0.6, anchor=CENTER)
    runButton = Button(window, text="run", command=run_clicked)
    runButton.place(relx=0.55, rely=0.6, anchor=CENTER)

【问题讨论】:

  • 您可以使用 OOP 方法
  • @Sujay 我已经想到了,但是我可以在没有 OOP 的情况下做到这一点吗?我真的不希望它用于我的程序的这一部分 ://
  • 非常感谢,我会试试的。我不知道如何将参数传递给命令函数。

标签: python tkinter tkinter-button


【解决方案1】:

我会“部分应用”该功能。首先,将您的回调更改为接受四个参数:

def add_clicked(x_in, y_in, index_input, cusps_info):
    cusps_info.append((int(x_in.get()), int(y_in.get()), index_input.get()))
    x_in.delete(0, END)
    y_in.delete(0, END)
    index_input.delete(0, END)

现在,将对该回调的调用包装在另一个函数中,然后提供参数。这可以通过简单的lambda 来完成:

addButton = Button(window, text="add", command=lambda: add_clicked(xInput, yInput, indexInput, cuspsInfo))

functools.partial:

from functools import partial

addButton = Button(window, text="add", command=partial(add_clicked, xInput, yInput, indexInput, cuspsInfo))

两者具有相同的效果:参数是“预先提供的”,以便稍后可以在没有任何参数的情况下调用回调。

正如您所看到的,随着参数数量的增加,这变得越来越混乱。这种技术最适合较少数量的参数。如果您有大量数据需要传递到函数中,则将它们放入 NamedTuple 或完整的自定义类可能是有意义的(如果它们都非常相似并且一起有意义的话)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    相关资源
    最近更新 更多