【问题标题】:Python tkinter change individual button parameters created in for loopPython tkinter 更改在 for 循环中创建的单个按钮参数
【发布时间】: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


    【解决方案1】:

    您当前程序的问题是,在for 循环中,您实际上是在创建新按钮时覆盖先前创建的按钮,因此无法再访问它们(因为没有剩余的参考脚本中的那些按钮)。

    我建议您可以创建一个字典并使用 n(名称)作为键存储按钮,然后将父按钮的名称传递给 clock_in()clock_out() 函数,然后使用它名称以获取对按钮的引用并配置其属性。

    示例代码-

    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 n=n:clock_in(n))
        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 n=n:clock_out(n))
        outbut.grid(row=2, column=2, padx=10,pady=50, columnspan=1)
    
    
    
    def clock_in(n):
        global clock
        global button_dict             #This is not really necessary, but I put it there to be explicit.
        button_dict[n].configure(bg='green')     #Choose whatever properties you want to choose to configure
        clock.destroy()
    
    def clock_out(n):
        global clock
        global button_dict             #This is not really necessary, but I put it there to be explicit.
        button_dict[n].configure(bg='red')     #Choose whatever properties you want to choose to configure
        clock.destroy()
    
    button_dict = {}
    
    for n in names:  
        button_dict[n] = Button(master,text=n,width = name_width, height = name_height,command=lambda n=n: clock_mast(n))
        button_dict[n].grid(row = roww, column = col,padx = xpad, pady = ypad)
        col += 1
        if col == 3:
            col = 0
            roww += 1
    
    
    
    master.mainloop()
    

    【讨论】:

    • 绝对精彩!我不能感谢你,我从来没有想过这样做!正是我想要的:-)
    • 很高兴能为您提供帮助。如果答案有帮助,我想请您接受答案(通过单击答案左侧的勾号),这将对社区有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多