can someone please help me I am facing a problem;
I want to draw an image in the canvas depending on the value of the IntVar called position
however I get an 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\tk.py", line 20, in inn
    if read() ==1:
  File "C:\Documents and Settings\admen\Desktop\tk.py", line 18, in read
    return position.get()
AttributeError: 'function' object has no attribute 'get'

Here is my code: #note: the three rdiobuttons are in the function. i couldn't fix the spacing.

def fhelicase():
    global tophelicase
    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=inn).pack()
    bh2=Radiobutton(tophelicase,text='2',variable=position,value=2,bg="green",command=inn).pack()
    bh3=Radiobutton(tophelicase,text='3',variable=position,value=3,bg="green",command=inn).pack()

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

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)

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

window.mainloop()

What is position, and where is it initialized?

position is the IntVar variable. sorry i forgot to include that part!

I have made some changes to the code but it still doesn't work
here is the code with its initialization:

import ImageTk
from Tkinter import*

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

def fhelicase():
    global tophelicase
    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=inn).pack()
    bh2=Radiobutton(tophelicase,text='2',variable=position,value=2,bg="green",command=inn).pack()
    bh3=Radiobutton(tophelicase,text='3',variable=position,value=3,bg="green",command=inn).pack()

def inn():
    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)
    tophelicase.destroy()

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)
window=Canvas(root,width=800,height=550,bg="white")
window.grid(column=1, rowspan=4,row=0)
window.mainloop()

In addition to lambda, you can also use partial to pass a variable indicator to a function. Also, using pack() and grid() in the same program leads to unpredictable results.

from Tkinter import*
from functools import partial

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

def inn(variable_passed):
    positions = ((10, 10), (60, 50), (110, 110))
    xx, yy = positions[variable_passed]
    print "variable passed =", variable_passed, "----->", xx, yy

tophelicase=Toplevel(bg="green",bd=10)
Label(tophelicase,text="Where should helicase be placed?",bg="green").pack()
Radiobutton(tophelicase,text='1',bg="green",command=partial(inn, 0)).pack()
Radiobutton(tophelicase,text='2',bg="green",command=partial(inn, 1)).pack()
Radiobutton(tophelicase,text='3',bg="green",command=partial(inn, 2)).pack()

root.mainloop()

Here is an example of multiple Tkinter radio buttons and how to use IntVar() ...

'''tk_RadioButton_multi1.py
exploring multiple Tkinter radio buttons
radio buttons only allow one item to be selected/checked
original code from: Ene Uran
'''

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

def rb_selected():
    # show checked/selected radio button item
    ix = rb_v.get()
    label['text'] = 'you selected %s' % mylist[ix]

root = tk.Tk()

# used by the radio buttons as index
rb_v  = tk.IntVar()

mylist = [
'apple',
'orange',
'banana',
'pear',
'apricot'
]

# list(range()) needed for Python3
rb = list(range(len(mylist)))
for ix, text in enumerate(mylist):
    # command is optional and responds to any rb changes
    rb[ix] = tk.Radiobutton(root, text=text, value=ix,
        variable=rb_v, command=rb_selected)
    rb[ix].grid(row=ix, column=0, sticky='w')

label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, pady=5, sticky='w')

# you can preset one radio button
# default is first button (ix=0)
rb_v.set(2)
# show initial selection
rb_selected()

root.mainloop()

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

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.