button1 = Button(window,text=" ",bg="skyblue",command=process1)
button2 = Button(window,text=" ",bg="skyblue",command=process2)
button3 = Button(window,text=" ",bg="skyblue",command=process3)
button4 = Button(window,text=" ",bg="skyblue",command=process4)
button5 = Button(window,text=" ",bg="skyblue",command=process5)
button6 = Button(window,text=" ",bg="skyblue",command=process6)
button7 = Button(window,text=" ",bg="skyblue",command=process7)
button8 = Button(window,text=" ",bg="skyblue",command=process8)
button9 = Button(window,text=" ",bg="skyblue",command=process9)
button10 = Button(window,text=" ",bg="skyblue",command=process10)
Do I have to write a repeat sentence? How can I reduce it?
python
Yes. Write a repeat sentence.
There are many ways.
How to use containers such as lists
eventPtrs = (process1, process2, process3)
buttonContainer = []
for eventPtr in eventPtrs:
buttonContainer.append(Button(window,text=" ",bg="skyblue",command=eventPtr))
buttonContainer[0] #First button
How to use reflection
import sys
mod = sys.modules[__name__]
eventPtrs = (process1, process2, process3)
for k, eventPtr in enumerate(eventPtrs ):
setattr(mod, 'button{}'.format(k), Button(window,text=" ",bg="skyblue",command=eventPtr))
Button0 # First button
© 2024 OneMinuteCode. All rights reserved.