Python tkinter button command Lambda.

Asked 2 years ago, Updated 2 years ago, 146 views

Python tkinter in use

An attempt was made to execute a function through the command in the button widget.

But I'm asking you this question because it doesn't work normally.

TestBtn = Button (MainForm, text="OK", command= test01(10)) TestBtn.pack(side=LEFT, padx=10)

def test01(data): print("Confirmation Test")

If you click button through the code above, the information "Confirmation Test" and 10 is included in the data of test01 I wanted to make sure that it was printed normally.

However, the test (10) was executed immediately after the first run, but it did not work properly when clicked.

TestBtn = Button (MainForm, text="OK", command=test01) TestBtn.pack(side=LEFT, padx=10)

def test01(data): print("Confirmation Test")

You only used the function name, and this method is used when you run a function that does not have any parameters This is a normal operating form. Currently, the parameter data cannot be filled

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\ksh\AppData\Local\Programs\Python\Python37\lib\tkinter_init.py", line 1702, in __call_ return self.func(*args)

TestBtn = Button (MainForm, text="OK", command=lambda: test (10)) TestBtn.pack(side=LEFT, padx=10)

def test01(data): print("Confirmation Test")

It was confirmed that it was working properly by adding lambda as follows. If you use Lambda, the parameters contain information and click button I was able to confirm that it was working properly.

What I want to ask you is

I look forward to your favorable reply.

tkinter lambda command python

2022-09-21 19:07

1 Answers

The reason why the code does not work first is that the return value after the function test01 is assigned to the command.

A function that does not specify a return value in Python returns None Command = None.

The second attempt was correct. The function itself should be registered in the command, not the execution form, to mean 'execute this function at a click'. That's the right expression. However, the expression cannot pass the parameter as you said.

The third is because the part declared with the keyword lambda: means an unnamed function, an anonymous function. We put parameter 10 into the test function and we put a function that calls it.

def a(data):
  print(data)

def b():
  a(10)

TestBtn = Button (MainForm, text="OK", command=b)

You can think of it as almost the same meaning.

Lambda: I don't mind if I use it I mean, if you want to use it in a more flexible and functional way, it's more flexible and functional Consider the partial function of functionools.

https://docs.python.org/2/library/functools.html#functools.partial

A function (?!?!) that returns a function that holds a parameter to a particular function It's also commonly called the curry technique.

https://stackoverflow.com/questions/277922/python-argument-binders

Like this.


2022-09-21 19:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.