如何在Python Tkinter中向函数添加参数

huangapple go评论62阅读模式
英文:

How to add an argument to a function in python Tkinter

问题

I'm really sorry about the title being so unclear. Here is my issue. Say I have a code like this:

from tkinter import *

root = Tk()

def callback(str):
    print(str)
    root.destroy()

btn = Button(root, text='destroy', command=callback('Hello world!'))

btn.pack()

root.mainloop()

However, when I executed this an error popped out:

Traceback (most recent call last):
  File "/Users/abc/Documents/test/test.py", line 8, in <module>
    btn = Button(root, text='destroy', command=callback('Hello world!'))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2647, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2569, in __init__
    self.tk.call(
_tkinter.TclError: can't invoke "button" command: application has been destroyed

I figured out that I can't write command=callback() with the parentheses, instead I should write command=callback in order to make the program function correctly. However, it seemed that it only worked when no argument is required. If I need to pass an argument, the argument(s) should be in a pair of parentheses [e.g. "callback('hello world!')"]. How can I pass an argument without writing a pair of parentheses? Thanks.

英文:

I'm really sorry about the title being so unclear. Here is my issue. Say I have a code like this:

from tkinter import *

root = Tk()

def callback(str):
    print(str)
    root.destroy()

btn = Button(root,text=&#39;destroy&#39;,command=callback(&#39;Hello world!&#39;))

btn.pack()

root.mainloop()

However, when I executed this an error popped out:

Traceback (most recent call last):
  File &quot;/Users/abc/Documents/test/test.py&quot;, line 8, in &lt;module&gt;
    btn = Button(root,text=&#39;destroy&#39;,command=callback(&#39;Hello world!&#39;))
  File &quot;/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py&quot;, line 2647, in __init__
    Widget.__init__(self, master, &#39;button&#39;, cnf, kw)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py&quot;, line 2569, in __init__
    self.tk.call(
_tkinter.TclError: can&#39;t invoke &quot;button&quot; command: application has been destroyed

I figured out that I can't write command=callback() with the parentheses, instead I should write command=callback in order to make the program function correctly. However, it seemed that it only worked when no argument is required. If I need to pass an argument, the argument(s) should be in a pair of parentheses [e.g."callback('hello world!')"]. How can I pass an argument without writing a pair of parentheses? Thanks.

答案1

得分: 2

如TheLizzard指出的那样,将command = callback('Hello world!')更改为command = lambda: callback('Hello world!'),因为只是简单地放置函数会调用该函数,但如果将其放入lambda中,它就变成了像迷你函数一样,因为lambda是可调用的。如果我记得正确,给定的参数应该是可调用的,就像一个函数一样。

英文:

as TheLizzard has pointed out, change command = callback(&#39;Hello world!&#39;) into command = lambda: callback(&#39;Hello world!&#39;), since just putting without lambda calls the function, but if you put it in a lambda it becomes like a mini function since lambdas are callable, the argument for command if I remember correctly, The given argument should callable, like a function for example

huangapple
  • 本文由 发表于 2023年1月9日 18:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定