0
import tkinter

root = tkinter.Tk()
root.geometry("200x200")

buttons=[i for i in range(10)]

def getNext():
    #If specific button is pressed, output "YES"

for num in buttons:
    btn=tkinter.Button(root, text=num,command=getNext)
    btn.pack(side=tkinter.TOP)
2
Contributors
1
Reply
8
Views
4 Months
Discussion Span
Last Post by woooee
1

Use partial to pass something to the function, and the function will do something based on the value. This example prints what was passed to the function. Note that the Python Style Guide suggests function names should be lower case letters and underscores https://www.python.org/dev/peps/pep-0008/

root = tkinter.Tk()
root.geometry("200x300")
##buttons=range(10)
button_list=[]

def get_next(button_num):
    print("button number =", button_num)
    ## change the color of the button pressed to show how the list works
    button_list[button_num].config(bg="lightblue")

for num in range(10):
    btn=tkinter.Button(root, text="Button %d" % (num),
                       command=partial(get_next, num))
    btn.pack(side=tkinter.TOP)
    ## save a reference to the button so it can be used in get_next
    ## the button number corresponds to the offset in the list
    button_list.append(btn)

root.mainloop()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.