它被写成一个电话拨号盘,我需要把每个数字变成按钮,然后打印点击过的数字。
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()发布于 2015-12-16 01:21:46
使用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()https://stackoverflow.com/questions/34295299
复制相似问题