2 Replies - 1237 Views - Last Post: 12 February 2013 - 09:38 AM Rate Topic: -----

#1 vod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-February 13

Tkinter, problem assosiate function to radiobutton

Posted 12 February 2013 - 09:15 AM

Hi, I'm quite new to python. Trying to write little utility to convert numbers from bin to dec and hexa,and vice versa.
I.m stack onradio buttons selection so far. Any ideas guys, how to "ask" def calc to get variables?
below is my code I've done so far:
import sys
from tkinter import *
from tkinter import messagebox
#my functions will go here...

def sel():#will create label to confirm selection on radio button
    mselection = int(var.get())
    mlabel8 = Label(mGui, text = mselection).grid(row=4, column =1, sticky=W)
    return

        
        
def calc():#main function
    mselection = int(var.get())
    if 'mselection'==1():
        mtext = ment.get()
        mlabel5 = Label(mGui, text=mtext).grid(row=5, column =1, sticky=W)
        return
    elif 'mselection'==2():
        mtext = ment.get()
        mlabel6 = Label(mGui, text=mtext).grid(row=6, column = 1, sticky = W)
        return
    elif 'mselection'==3():
        mtext = ment.get()
        mlabel7 = Label(mGui, text=mtext).grid(row=7, column =1, sticky=W)
        return
        
        
      
        
# my code to add widgets will go here...

mGui=Tk()
mGui.geometry('400x300+600+300')
mGui.title('Binar - Calculator')
ment=IntVar()
var=IntVar()


mlabel1 = Label(mGui,text="Enter your input:"). grid(row=0, column=0, sticky=E)
mEntry1 = Entry(mGui,textvariable = ment).grid(row=0, column=1, sticky=W)
r1 = Radiobutton(mGui, text="1)My input is binary",variable=var,value =1, command=sel).grid(row=1, sticky=W)
r2 = Radiobutton(mGui, text="2)My input is decimal",variable=var,value =2, command=sel).grid(row=2, sticky=W)
r3 = Radiobutton(mGui, text="3)My input is hexadecimal",variable=var,value =3, command=sel).grid(row=3, sticky=W)
mLabel9 = Label(mGui,text="Your selection is: ").grid(row=4, column=0, sticky=E)

mlabel2 = Label(mGui,text="Binary number is:").grid(row=5, column=0, sticky=E)
mlabel3 = Label(mGui,text="Decimal number is:").grid(row=6, column =0, sticky=E)
mlabel4 = Label(mGui,text="Hexadecimal number is:").grid(row=7, column =0, sticky=E)
mButton1 = Button(mGui,text="Calculate", command = calc,).grid(row=8, column=1, sticky =E)




mGui.mainloop()


I'm getting error message like:
>>>
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Python33\binar1.py", line 15, in calc
if 'mselection'==1():
TypeError: 'int' object is not callable
>>>

Is This A Good Question/Topic? 0
  • +

Replies To: Tkinter, problem assosiate function to radiobutton

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 109
  • Joined: 21-November 12

Re: Tkinter, problem assosiate function to radiobutton

Posted 12 February 2013 - 09:28 AM

In the calc function, you store the get() value in a variable, but compare a string to the return from the function 1(); the function call is where the error message is coming from.
def calc():#main function
    mselection = int(var.get())
    if 'mselection'==1():
TypeError: 'int' object [1()] is not callable:           ## string == function
#
# if should be similar to
def calc():#main function
    mselection = int(var.get())
    if mselection==1: 

This post has been edited by woooee: 12 February 2013 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 vod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-February 13

Re: Tkinter, problem assosiate function to radiobutton

Posted 12 February 2013 - 09:38 AM

Thanks a million, I should spotted myself :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1