使用forloop - tkinter将多个命令关联到多个按钮

时间:2016-08-20 08:02:06

标签: python python-3.x for-loop tkinter tk

在以下代码中,创建了3个按钮,并与3个不同的动作相关联。现在每个按钮都必须执行其操作。

import tkinter as tk

class App():
    def __init__(self,parent):
        self.parent = parent
        self.do_oper()


    def do_oper(self):
        self.labelframe = tk.Frame(self.parent).grid()
        m = ['a','b','c']
        n = ['self.a','self.b','self.c']
        dict_cmds = dict(zip(m,n))
        for i,cmd in enumerate(dict_cmds):
            self.Label=tk.Button(self.labelframe,command=lambda cmd=cmd:dict_cmds[cmd])
            self.Label.config(text=cmd)
            self.Label.grid(row=1,column=i)


    def a(self):
        print('Inside -> a')

    def b(self):
        print('Inside -> b')

    def c(self):
        print('Inside -> c')

if __name__ =='__main__':
    root = tk.Tk()
    App(root)
    root.mainloop()

0 个答案:

没有答案