英文:
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='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.
答案1
得分: 2
如TheLizzard指出的那样,将command = callback('Hello world!')
更改为command = lambda: callback('Hello world!')
,因为只是简单地放置函数会调用该函数,但如果将其放入lambda中,它就变成了像迷你函数一样,因为lambda是可调用的。如果我记得正确,给定的参数应该是可调用的,就像一个函数一样。
英文:
as TheLizzard has pointed out, change command = callback('Hello world!')
into command = lambda: callback('Hello world!')
, 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论