How can I identify buttons, created in a loop?

后端 未结 4 1061
借酒劲吻你
借酒劲吻你 2020-12-06 15:29

I am trying to program a minesweeper game on python using tkinter. I started off by creating a grid of buttons using a two dimensional list of 10x10. Then I created each but

4条回答
  •  死守一世寂寞
    2020-12-06 16:16

    The following code generates 12 buttons ,4 in each row. The particular button required to be edited is called similar to calling a matrix element. As an example button[1,1] has been edited for background color and button[2,2] has been edited for foreground color and text. The programme is tested on on python3.6 pycharm console

    from tkinter import *
    root=Tk()
    Buts={}
    for r in range(3):
        for c in range(4):
            Buts[(r,c)]=Button(root,text='%s/%s'%(r,c),borderwidth=10)
            Buts[r,c].grid(row=r,column=c)
    Buts[1,1]['bg']='red'
    Buts[2,2]['text']=['BUTTON2']
    Buts[2,2]['fg']=['blue']
    root.mainloop()
    

提交回复
热议问题