Hello,
I need help with creating 6 buttons can I do that using some loop and how can that be done?
each button has a different image and different text...their bg, and dimensions are the same
Also after creating all the buttons I want to make a callback function for each the function is the same as the one I have created for the first button except for the text in the label.
Please help me, I am merely a beginner.
Thank you very much,
your help is appreciated.

Here is my code: #note:the 3 radiobuttons are in the function. anything after that is not in the function; i couldn't fix the spacing

import ImageTk
from Tkinter import*

root=Tk()
root.title("Replicate DNA yourself!")
position=IntVar()

def fhelicase():
    tophelicase=Toplevel(bg="green",bd=10)
    labhelicase=Label(tophelicase,text="Where should helicase be placed?",bg="green").pack()
    bh1=Radiobutton(tophelicase,text='1',variable=position,value=1,bg="green",command=tophelicase.destroy).pack()
    bh2=Radiobutton(tophelicase,text='2',variable=position,value=2,bg="green",command=tophelicase.destroy).pack()
    bh3=Radiobutton(tophelicase,text='3',variable=position,value=3,bg="green",command=tophelicase.destroy).pack()


pol3=ImageTk.PhotoImage(file="pol3.gif")
Pol3=Button(root,height=70,width=85,text="DNA Plymerase III",image=pol3,compound="top",bg="white",command=fhelicase)
Pol3.image=pol3
Pol3.grid(row=0)

gyrase=ImageTk.PhotoImage(file="gyrase.gif")
Gyrase=Button(root,text="gyrase",height=70,width=85,image=gyrase,compound="top",bg="white")
Gyrase.image=gyrase
Gyrase.grid(row=1)

ligase=ImageTk.PhotoImage(file="ligase.gif")
Ligase=Button(root,text="Ligase",height=70,width=85,image=ligase,compound="top",bg="white")
Ligase.image=ligase
Ligase.grid(row=2)

ssb=ImageTk.PhotoImage(file="ssbb.gif")
Ssb=Button(root,text="ssb",height=70,width=85,image=ssb,compound="top",bg="white")
Ssb.image=ssb
Ssb.grid(row=3)

primase=ImageTk.PhotoImage(file="primase.gif")
Primase=Button(root,text="primase",height=70,width=85,image=primase,compound="top",bg="white")
Primase.image=primase
Primase.grid(row=4)

helicase=ImageTk.PhotoImage(file="helicase.gif")
Helicase=Button(root,text="Helicase",height=70,width=85,image=helicase,compound="top",bg="white")
Helicase.image=helicase
Helicase.grid(row=5)

pol1=ImageTk.PhotoImage(file="pol1.gif")
Pol1=Button(root,text="DNA PolymeraseI",height=70,width=85,image=pol1,compound="top",bg="white")
Pol1.image=pol1
Pol1.grid(row=6)

window.mainloop()

To pass an argument in the button command use lambda:

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def click(s):
    """called with lambda"""
    if s == "one":
        root.title("Button one has been clicked")
    else:
        root.title("Button two has been clicked")


root = tk.Tk()

b1 = tk.Button( text="one", command=lambda: click("one") )
b1.pack(padx=150, pady=10)

b2 = tk.Button( text="two", command=lambda: click("two") )
b2.pack(pady=10)

root.mainloop()

You could create your buttons in a loop, but it would be quite cumbersome. It would be difficult to maintain a meaningful variable name for each button. Since you only have 6 buttons it wouldn't be worth it.

bh1=Radiobutton(tophelicase,text='1',variable=position,value=1,bg="green",command=tophelicase.destroy).pack()

Print bh1 to see what it contains and you should see that there is no reason to store the return from pack(), i.e. just use

   Radiobutton(tophelicase,text='1',variable=position,value=1,bg="green",
               command=fhelicase).pack()

A typical example ...

'''tk_button_loop1.py
create five Tkinter image buttons using a for loop
'''

# needs Python25 or higher
from functools import partial

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def click(val):
    s = "Button " + val + " clicked"
    root.title(s)  # test

root = tk.Tk()

# assume image files are in working folder
# or use ./img/led1.gif etc.
imagefile_list = [
'led1.gif',
'led2.gif',
'led3.gif',
'led4.gif',
'led5.gif']

button_label_list = [
'one',
'two',
'three',
'four',
'five']

print(list(zip(button_label_list, imagefile_list)))  # test

mybutton = list(range(len(button_label_list)))
print(mybutton)  # test
n = 0
for mylabel, myfile in zip(button_label_list, imagefile_list):
    print(mylabel, myfile)  # test
    myimage = tk.PhotoImage(file=myfile)
    # partial takes care of function and argument
    cmd = partial(click, mylabel)
    mybutton[n] = tk.Button(root, text=mylabel, image=myimage, 
        compound='top', command=cmd)
    mybutton[n].pack(side='left', padx=5, pady=3)
    # make this button's image persistent
    mybutton[n].image = myimage
    n += 1

root.mainloop()

Display --> http://prntscr.com/kzjpb

I have tried it with 2 buttons but it only displayed one...
what am i missing?

import ImageTk
from Tkinter import*
from functools import partial
root=Tk()
position=IntVar()

def funct():
    global top
    top=Toplevel(bg="green",bd=10)
    lab=Label(top,text="Where should it be placed?",bg="green").pack()
    bh1=Radiobutton(top,text='1',variable=position,value=1,bg="green",command=inn()).pack()
    bh2=Radiobutton(top,text='2',variable=position,value=2,bg="green",command=inn()).pack()
    bh3=Radiobutton(top,text='3',variable=position,value=3,bg="green",command=inn()).pack()

def inn():
    print position.get()
    if position.get()==1:
        xx,yy=10,10
    elif position.get()==2:
        xx,yy=60,60
    elif position.get()==3:
        xx,yy=110,110
    window.create_image(xx,yy,image=pol3)
    top.destroy()

mglst=["pol3.gif","gyrase.gif"]
txtlst=["DNA Pol3","Gyrase"]
mxlst=list(zip(txtlst,mglst))
print mxlst
btnz=list(range(len(txtlst)))
n=0
for txt, img in mxlst:
    imag=ImageTk.PhotoImage(file=img)
    btnz[n]=Button(root,text=txt,image=imag,height=70,width=85,compound="top",bg="white",command=funct)
    btnz[n].grid(row=n)
    btnz[n].image=imag
    n+=n

window=Canvas(root,width=800,height=550,bg="white")
window.grid(column=1, rowspan=7,row=0)
root.mainloop()

Small mistake in your code in line 37
use n += 1

ooopsy! I see thanx!
the buttons show now but when I click on one I get the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Documents and Settings\admen\Desktop\shortVersion.py", line 12, in funct
    bh1=Radiobutton(top,text='1',variable=position,value=1,bg="green",command=inn()).pack()
  File "C:\Documents and Settings\admen\Desktop\shortVersion.py", line 24, in inn
    window.create_image(xx,yy,image=pol3)
UnboundLocalError: local variable 'xx' referenced before assignment

position.get() most likely returns a string
in your if statements compare to a string not an integer

:( that didn't solve the problem! still i get the same error

Did you correct your code to this?

def inn():
    print position.get()
    if position.get()=='1':
        xx,yy=10,10
    elif position.get()=='2':
        xx,yy=60,60
    elif position.get()=='3':
        xx,yy=110,110
    window.create_image(xx,yy,image=pol3)
    top.destroy()

You bind the button press to the return from the function=inn(), not the function=inn. The button call later on to "funct" is coded correctly so note the difference. Also, you still mix grid and pack which can cause problems.

Oh ok!that fixed it thank you

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.