【问题标题】:Binding and returning values from a function within a function (Tkinter)从函数中的函数绑定和返回值(Tkinter)
【发布时间】:2021-08-12 00:38:38
【问题描述】:

我已经简化了我在代码中遇到的问题。

我在函数中有几个函数,我发现很难从绑定触发的函数中捕获返回值。

from tkinter import *

root = Tk()
root.title("Test example")
root.geometry("500x500")

def funcl(event):
    print("funcl")

    def inner_funcl():
        print("inner fucntion")
        x = 15
        return x

    x=inner_funcl()

    return x

def ppprinter(x):
    print(x)

z=funcl(event)
ppprinter(z)
my_button = Button(root,text="Button")
my_button.pack()
my_button.bind("<Button-1>",funcl)
root.mainloop()

【问题讨论】:

  • 您无法返回值。您必须使用全局变量或使用 OOP
  • 在其他函数中定义函数被认为是不好的做法。还有你想对返回的值做什么?

标签: python tkinter bind


【解决方案1】:

z = funcl(event)

您传递给函数的event 变量未声明。并且函数funcl中传递的event在任何地方都没有使用。

我不明白这段代码及其功能的意义。

解决方案: 您可以创建具有任何值的变量event(例如:event = "Hello")。 这将允许您运行脚本。

【讨论】:

  • 这是我遇到的问题的简化版本。所以我的理解是当你绑定一个函数时,它也绑定了它的事件。只需 event=my_button.bind("",funcl),但您不需要变量 event,因为它是由绑定自动创建的。从上面的回复看来是不可能的。
猜你喜欢
  • 2011-12-19
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2012-10-17
  • 2021-07-20
相关资源
最近更新 更多