I'm working with Tkinter and I need to make 600 buttons in a grid of 6x100. Is there a way to produce generic variables by going through a for loop? Or do I have to type all 600?

Recommended Answers

All 9 Replies

You don't have to type all 600 ! What code would you type to create only one ?

Well

from Tkinter import*
root = Tk()
frame = Frame(root)
b1=Button(frame,*various_options)

I'm not sure how to initiate separate buttons without calling the Button class. Would a dictionary help? Another question that came to mind, is when I click the button I want to switch the image on the button. I tried this.

b.image = image_name

The new image doesn't appear on the button.
Sorry if the code isn't in code boxes, this is the first time I've used it.
edit*For some reason it's putting a 1. next the second code, ignore that.

I suggest a button array

buttonlist = []
for i in range(600):
    buttonlist.append(Button(frame, *various_options))

Also, to change the image, i suggest something like

button.config(image=...)

but I normally don't use tkinter, so I'm not sure. This link may help.

Ok the configure method works, but this code won't place the buttons in the frame.

from Tkinter import*
root = Tk()
frame = Frame(root)
buttons=[]
for i in range(600):
    buttons.append(Button(frame,width=20,height=20))
buttons[1].pack()
                   
root.mainloop()

Anybody know why?

Here is a widget with the 600 buttons

from Tkinter import*
root = Tk()
frame = Frame(root)

frames = []
buttons=[]
for i in range(24):
    f = Frame(frame)
    frames.append(f)
    for j in range(25):
        n = 25 * i + j
        b = Button(f, text= "%03d" % n)
        buttons.append(b)
        b.pack(side="left")
    f.pack(side="top")
frame.pack(side="top")
                   
root.mainloop()

Wow that works perfectly. Now I can move forward with my project. Hopefully by the end of the week I can show the finished project and having 600 buttons will make sense.

The same code with a call back to show which button was pressed. Hopefully you will use a class structure for this as that eliminates shared variable problems.

from Tkinter import*

def cb_handler(event, cb_number ):
    print "cb_handler - button number pressed =", cb_number

root = Tk()
frame = Frame(root)

frames = []
buttons=[]
for i in range(24):
    f = Frame(frame)
    frames.append(f)
    for j in range(25):
        n = 25 * i + j
        b = Button(f, text= "%03d" % n)
        buttons.append(b)
        b.pack(side="left")

        def handler ( event, button_num=n ):
            return cb_handler( event, button_num )
        b.bind ( "<Button-1>", handler )

    f.pack(side="top")
frame.pack(side="top")

root.mainloop()

It's all personal preference, but I prefer a grid.

from Tkinter import*

def cb_handler(event, cb_number ):
    print "cb_handler - button number pressed =", cb_number

root = Tk()
frame = Frame(root)

buttons=[]
b_row=1
b_col=0
for j in range(1, 601):
         b = Button(frame, text = "%3d" % (j))
         buttons.append(b)
         b.grid(row=b_row, column=b_col)

         def handler ( event, button_num=j ):
                return cb_handler( event, button_num )
         b.bind ( "<Button-1>", handler )

        ## you can also use 2 for() loops as Gribouillis did
         b_col += 1
         if b_col > 24:
            b_col = 0
            b_row += 1

frame.grid(row=0, column=1)
root.mainloop()

Also on my screen, 30x20 looks better than 24x25.

Well here's what I've done with the code so far. All I need left is a scroll bar and a file--> export button to send the button states to a text file.

from Tkinter import*

def cb_handler(event, cb_number ):
    return cb_number

root = Tk()
frame = Frame(root)

#the button pictures
base = PhotoImage(file="BaseSquare.gif")
forward = PhotoImage(file="GreenSquare.gif")
backward = PhotoImage(file="RedSquare.gif")
jump = PhotoImage(file="JumpSquare.gif")
blank = PhotoImage(file="Blank.gif")

buttons={}
b_row=1
b_col=0
for j in range(1, 601):
         b = Button(frame,width=20,height=20,image=base)
         buttons[j]={1:b,2:0}
         b.grid(row=b_row, column=b_col)

         def handler ( event, button_num=j ):
                a=buttons[cb_handler( event, button_num )]
                #sets button states
                if a[2]<6:
                    a[2]+=1
                if a[2]==6:
                    a[2]=0
                if a[2]==0:
                    a[1].configure(image=base)
                #sets button pictures
                if a[2]==1:
                    a[1].configure(image=forward)
                if a[2]==2:
                    a[1].configure(image=backward)
                if a[2]==3:
                    a[1].configure(image=jump)
                if a[2]==4:
                    a[1].configure(image=blank)
         b.bind ( "<Button-1>", handler )

        ## you can also use 2 for() loops as Gribouillis did
         b_col += 1
         if b_col > 5:
            b_col = 0
            b_row += 1

frame.grid(row=0, column=1)
root.mainloop()

I realize it's a little messy, but it's meant to just be a small program. I'm using it to create a track generator for a 3d game I'm currently working on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.