【发布时间】:2015-12-11 20:10:46
【问题描述】:
您好,我使用 for 循环创建了 15 个按钮,每个按钮传递一个名称并打开一个新的顶级窗口,在顶级窗口内还有 2 个按钮,一个称为“打卡”和“打卡”我想根据是否有人打卡来更改初始按钮(其中包含名称的按钮)的背景,但是我不知道如何处理在 for 循环中创建的各个按钮。
from Tkinter import *
master = Tk()
master.geometry('800x480+0+0')
master.title('Gateway Logger')
names = ['Annette', 'Heather', 'Tracey', 'Amy', 'Josh', 'Chris'
, 'Lekh', 'Ellie', 'Sarah', 'Richard', 'Paula', 'Michelle'
, 'Martin', 'Dave', 'Phil']
visitors = ['Visitor 1', 'Visitor2', 'Visitor 3', 'Visitor 4', 'Visitor 5', 'Visitor 6']
name_width = 20
name_height = 5
xpad = 5
ypad = 5
col = 0
roww = 0
sarah='out'
def clock_mast(n):
iowid=40
iohi=15
global clock
print "This is: " +str(n)
clock = Toplevel(width=300, height =300)
clock.title('Clock In/Out')
staff_name = Label (clock, text = n, font = "Verdana 24")
staff_name.grid(row=1,column=1,columnspan=2)
inbut=Button(clock, width=iowid, height=iohi, text='Clock In', bg='green',command=lambda:clock_in())
inbut.grid(row=2, column=1, padx=10,pady=50, columnspan=1)
outbut=Button(clock, width=iowid, height=iohi, text='Clock Out', bg='red',command=lambda:clock_out())
outbut.grid(row=2, column=2, padx=10,pady=50, columnspan=1)
def clock_in():
global clock
clock.destroy()
def clock_out():
global clock
clock.destroy()
for n in names:
b = Button(master,text=n,width = name_width, height = name_height,command=lambda n=n: clock_mast(n))
b.grid(row = roww, column = col,padx = xpad, pady = ypad)
col += 1
if col == 3:
col = 0
roww += 1
master.mainloop()
【问题讨论】:
标签: python loops button for-loop tkinter