Function performs differently in Tkinter

Post here if you need help with creating a Graphical User Interface in Python.

Function performs differently in Tkinter

I have written a simple function for converting decimal numbers into binary format. i,e. 3----->11

The function works fine on its own and when run in the commands line and returns a numpy array i.e. [1., 1,]

However, when I try and put it into a GUI using Tkinter, everything works fine except the output is wrong. i.e. 7 spits out [0, 0, 0, 0]. Was wondering if someone could explain what's wrong here?

I understand there a better ways to write a binary converter but I am learning and I'm really just interested in running a function with a Tkinter GUI front end at this stage.

The code with tkinter is below.

Code: Select all
`'''' import Tkinter and themed widgets '''from Tkinter import *import ttk import numpy as np''' Calculation Section '''def calculate (*args):   a = int(decimal.get())   i = 0      while a/2**i > 1:      i = i + 1         biarray = np.zeros(i + 1)      for j in range (0, i + 1):      test = a % 2       if test == 0:           biarray [j] = 0       else:           biarray [j] = 1       a = a/2      z = np.array_str(biarray)     binary.set(z)      ''' setup window with Tk '''root = Tk()root.title('Binary Convertor')mainframe = ttk.Frame(root, padding = '3 3 12 12')mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))mainframe.columnconfigure (0, weight = 1)mainframe.rowconfigure (0, weight = 1)''' create the widgets in program '''decimal = StringVar()binary = StringVar()# define entry box for decimal numberdecimal_entry = Entry(mainframe, width = 7, textvariable = decimal)# define position in grid of entry boxdecimal_entry.grid(column = 2, row = 1, sticky = (W, E))# define position of label and convert buttonttk.Label(mainframe, textvariable = binary).grid(column = 2, row = 2, sticky = (W,E))ttk.Button(mainframe, text = 'Calculate', command = calculate).grid(column = 3, row = 3, sticky = W)''' Create static text labels '''ttk.Label(mainframe, text = 'Decimal').grid(column = 3, row = 1, sticky = W)ttk.Label(mainframe, text = 'is written as').grid(column = 1, row = 2, sticky = E)ttk.Label(mainframe, text = 'in binary form').grid (column = 3, row = 2, sticky = W)''' Finishing touches '''# add padding around all the widgets for spacefor child in mainframe.winfo_children(): child.grid_configure(padx = 10, pady = 10)#focus when mouse of hovered over GUIdecimal_entry.focus()# calculate when Return key is pressed (calculate command function defined above)root.bind('<Return>', calculate)      ''' Event Loop '''# required to make app runroot.mainloop();               `

Here is the function that returns the correct values.
Code: Select all
`'''' import Tkinter and themed widgets '''from Tkinter import *import ttk import numpy as npdef calculate(*args):    a = int(*args)    i=0    while a/2**i > 1:        i = i + 1            biarray = np.zeros(i + 1)        for j in range (i, -1, -1):        test = a % 2        if test == 0:            biarray[j] = 0        else:             biarray[j] = 1        a = a/2        #z = np.array_str(biarray)      #print z    print biarraycalculate(3)calculate(7)`
Last edited by stranac on Wed Apr 30, 2014 2:08 pm, edited 1 time in total.
Reason: First post lock.
mboyd02255

Posts: 2
Joined: Wed Apr 30, 2014 9:47 am

Re: Function performs differently in Tkinter

####SOLVED#####

I found out what was wrong with the code below. The functions were entered correctly, however the tabbing was off.

I normally run the code from the terminal on Linux Ubunutu and use Gedit to write the code, however I opened in in IDLE and it informed me that there was tabbing errors.

To solve:
I selected all the code
Format-> Untabify Region
Set tab equal to 4 columns and it works.

Code: Select all
`'''' import Tkinter and themed widgets '''from Tkinter import *import ttk import numpy as np''' Calculation Section '''def calculate (*args):    a = int(decimal.get())    b = a    i = 0        while b/2**i > 1:        i = i + 1            biarray = np.zeros(i + 1)        for j in range (i , -1, -1):        test = b % 2        if test == 0:            biarray [j] = 0        else:            biarray [j] = 1        b = b/2        z = np.array_str(biarray)      binary.set(z)    print b    print biarray    ''' setup window with Tk '''root = Tk()root.title('Binary Convertor')mainframe = ttk.Frame(root, padding = '3 3 12 12')mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))mainframe.columnconfigure (0, weight = 1)mainframe.rowconfigure (0, weight = 1)''' create the widgets in program '''decimal = StringVar()binary = StringVar()# define entry box for decimal numberdecimal_entry = Entry(mainframe, width = 2, textvariable = decimal)# define position in grid of entry boxdecimal_entry.grid(column = 2, row = 1, sticky = (W, E))# define position of label and convert buttonttk.Label(mainframe, textvariable = binary).grid(column = 2, row = 2, sticky = (W,E))ttk.Button(mainframe, text = 'Calculate', command = calculate).grid(column = 3, row = 3, sticky = W)''' Create static text labels '''ttk.Label(mainframe, text = 'Decimal').grid(column = 3, row = 1, sticky = W)ttk.Label(mainframe, text = 'is written as').grid(column = 1, row = 2, sticky = E)ttk.Label(mainframe, text = 'in binary form').grid (column = 3, row = 2, sticky = W)''' Finishing touches '''# add padding around all the widgets for spacefor child in mainframe.winfo_children(): child.grid_configure(padx = 10, pady = 10)#focus when mouse of hovered over GUIdecimal_entry.focus()# calculate when Return key is pressed (calculate command defined above)root.bind('<Return>', calculate)        ''' Event Loop '''# required to make app runroot.mainloop();                    `
mboyd02255

Posts: 2
Joined: Wed Apr 30, 2014 9:47 am