tkinter creating buttons in for loop passing command arguments

前端 未结 3 1322
陌清茗
陌清茗 2020-11-22 01:09

I am trying to create buttons in tkinter within a for loop. And with each loop pass the i count value out as an argument in the command value. So when the function is called

3条回答
  •  一向
    一向 (楼主)
    2020-11-22 01:52

    Simply attach your buttons scope within a lambda function like this:

    btn["command"] = lambda btn=btn: click(btn) where click(btn) is the function that passes in the button itself. This will create a binding scope from the button to the function itself.

    Features:

    • Customize gridsize
    • Responsive resizing
    • Toggle active state

    #Python2
    #from Tkinter import *
    #import Tkinter as tkinter
    #Python3
    from tkinter import *
    import tkinter
    
    root = Tk()
    frame=Frame(root)
    Grid.rowconfigure(root, 0, weight=1)
    Grid.columnconfigure(root, 0, weight=1)
    frame.grid(row=0, column=0, sticky=N+S+E+W)
    grid=Frame(frame)
    grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
    Grid.rowconfigure(frame, 7, weight=1)
    Grid.columnconfigure(frame, 0, weight=1)
    
    active="red"
    default_color="white"
    
    def main(height=5,width=5):
      for x in range(width):
        for y in range(height):
          btn = tkinter.Button(frame, bg=default_color)
          btn.grid(column=x, row=y, sticky=N+S+E+W)
          btn["command"] = lambda btn=btn: click(btn)
    
      for x in range(width):
        Grid.columnconfigure(frame, x, weight=1)
    
      for y in range(height):
        Grid.rowconfigure(frame, y, weight=1)
    
      return frame
    
    def click(button):
      if(button["bg"] == active):
        button["bg"] = default_color
      else:
        button["bg"] = active
    
    w= main(10,10)
    tkinter.mainloop()
    

提交回复
热议问题