Reputation: 1
It's written to be a phone dial and I need to make each of the numbers into buttons and then print the numbers that have been clicked on.
from tkinter import Tk, Label, RAISED
root = Tk()
labels = [['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']]
for r in range(4):
for c in range(3):
#create label for row r and column c
label = Label(root,
relief=RAISED,
padx=15,
text=labels[r][c])
#place label in row r and column c
label.grid(row=r, column=c)
root.mainloop()
Upvotes: 0
Views: 48
Reputation: 143187
Use Button(... , command=lambda x=some_value: some_function(x) )
from tkinter import Tk, Label, RAISED
labels = [['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']]
def my_function(text):
print(text)
root = Tk()
for r in range(4):
for c in range(3):
#create button for row r and column c
Button(root,
relief=RAISED,
padx=15,
command=lambda x=labels[r][c]: my_function(x),
text=labels[r][c]).grid(row=r, column=c)
root.mainloop()
Upvotes: 1