python Bulk Added button uses the same click event approach
- 2021-07-22 09:58:54
- OfStack
python batch-added button uses the same 1 click event to differentiate according to the parameters passed.
def clear_text():
print ' I'm just emptying out '
def clear_text(index):
print ' I'm just emptying out ' +str(index)
button = Button(framet_title, text=' Empty ', command=clear_text)
In this way, it is no problem for a single button to correspond to a single click event
If you are
for i in Range(10):
button = Button(framet_title, text=' Empty ', command=clear_text_list(i))
This way, when your program starts, the callback function will be executed directly, and there is no response when you click the button. The way to use button. bind is almost the same
There is no problem with this writing if it is in JAVA C HTML C + +. Please forgive me for being a programmer who develops android. What puzzles me most is that I obviously set a callback for each button. Why is there an early callback, and there is no response when clicking.
for i in Range(10):
button = Button(framet_title, text=' Empty ', command=lambda : clear_text_list(i))
If the lambda program starts, there will be no callback. However, the index obtained by each click is still the last one.
Google 1 lap (to be honest, I don't know what keywords to use...) http://stackoverflow.com/I tried python buttons command lambda above
Found http://stackoverflow.com/questions/20596892/disabling-buttons-after-click-in-tkinter
The correct posture is
for index in range(9):
n=letters[index]
button = Button(root, bg="White", text=n, width=5, height=1, relief=GROOVE,
command=lambda index=index, n=n: appear(index, n))
After seeing the answer, start to find the reason why the answer is right. He made a set of click events with lambda expression, which corresponds to the set of button.