0

I am currently using Canopy to run my code. I was trying to fetch some integer data from the entry box in my GUI according to the number that I give in the for loop and print it. It is not working.

Here is the error:

Traceback (most recent call last):
File "C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win- x86_64\lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args)
File "C:\Users\SIMULATOR\Desktop\python\life cycle graph\try4.py", line 24, in const
z=int(s)
ValueError: invalid literal for int() with base 10: ''

My code is:

from Tkinter import *

root = Tk()

root.geometry('1000x600+400+400')

def const():

    const_entries = []

    for i in range(0, 4):

        en = Entry(root)
        en.pack()
        en.place(x=50, y = 200 + 25*i)

        s = en.get()
        z = int(s)

        const_entries.append(z)

        j = i + 1

        label = Label(root, text="Alternative %r"%j)
        label.pack()
        label.place(x = 200, y = 200 + 25*i)

        print const_entries

    button1 = Button(root, text="Construction cost", command = const).grid(row = 15, column = 0)

    root.mainloop()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saif uddin
  • 9
  • 1
  • 3
  • It is obvious that i need to convert the Entry into integer to save it into the list but the program shows error as soon as i declare the Entry as Integer, – Saif uddin Dec 16 '15 at 08:09
  • ``z=int(s)`` fails because ``s`` is an empty string. Did you miss something at ``s=en.get()``? – jbndlr Dec 16 '15 at 08:09
  • Is there an error like `TclError: cannot use geometry manager pack inside . which already has slaves managed by grid` – Kenly Dec 16 '15 at 08:17

4 Answers4

3

Clarification: Assuming that s isn't empty (en.get() returns some number).

Check out this example:

int('55.500000')

It gets you a: ValueError: invalid literal for int() with base 10: '55.500000'

And:

float('55.500000')

Gets you: 55.5

So just cast to float instead of int.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Idos
  • 15,053
  • 14
  • 60
  • 75
0

When literal s couldn't be converted to int, int(s) failed.

try:
    z = int(s)
except ValueError:
    z = 0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
  • So you want that your code works wrong? Because `0` is not the value which is expected during the runtime. – Psytho Dec 16 '15 at 09:29
0

When you call en.get() at this point, it will return '' and then int('') will raise an error. Maybe before getting the integer you want to set an integer as string to the entry.

When you write button1 = Button(root, text="Construction cost", command = const).grid(row = 15, column = 0) button1 will not be a Button instance; button1 should be the result of the grid function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kenly
  • 24,317
  • 7
  • 44
  • 60
0

As already stated in the other answers, your code is failing, because your entries are empty when you call the const method.

Assuming that you want to enter something in your entry boxes and then print the alternatives I guess code like this would do the job:

from Tkinter import *

root=Tk()

root.geometry('1000x600+400+400')

def const():
    const_entries = []
    for i in range(4):
        s = entryArray[i].get()
        z = int(s)
        const_entries.append(z)

    print const_entries

button1 = Button(root, text="Construction cost", command = const).grid(row = 15, column = 0)

entryArray = []

for i in range(0,4):
    entryArray.append(Entry(root))

    #Insert default value 0
    entryArray[i].insert(0, 0)
    entryArray[i].pack()
    entryArray[i].place(x = 50, y = 200 + 25*i)
    label = Label(root, text="Alternative %d"%(i+1))
    label.pack()
    label.place(x = 200, y = 200 + 25*i)

root.mainloop()

This way you create the entries at startup and you give the user the time to enter something. When you press the Construction cost button the const function is called which will read the content of your entries and print them out.

Update

If you then want the user to enter the number of entries I'd suggest using a class and defining two buttons:

  • One for creating the entries after the user has entered the number

  • One for processing the number the user enters in the created entries

 

from Tkinter import *

root=Tk()

root.geometry('1000x600+400+400')

class MainWindow():
    def __init__(self):
        Label(root, text='Number of Entries').grid(row = 15, column = 0)
        self.numEntry = Entry(root)
        self.numEntry.grid(row = 15, column = 1)
        self.numEntry.insert(0, 0)
        Button(root, text="Build entries", command = self.buildEntries).grid(row = 15, column = 3)
        Button(root, text = "Construction cost", command = self.const).grid(row = 18, column = 0)

    def buildEntries(self):
        self.entryArray = []
        numEntries = int(self.numEntry.get())

        for i in range(numEntries):
            self.entryArray.append(Entry(root))
            #Insert default value 0
            self.entryArray[i].insert(0, 0)
            self.entryArray[i].pack()
            self.entryArray[i].place(x = 50, y = 200 + 25*i)
            label=Label(root, text="Alternative %d"%(i+1))
            label.pack()
            label.place(x = 200, y = 200 + 25*i)

    def const(self):
        const_entries = []
        numVal = len(self.entryArray)
        for i in range(numVal):
            s = self.entryArray[i].get()
            z = int(s)
            const_entries.append(z)

        print 'Const Entries:', const_entries

m = MainWindow()

root.mainloop()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
toti08
  • 2,448
  • 5
  • 24
  • 36
  • Thanks for the answer..it really helped..i just want to add one more question here...What if i want the user to input the number of Entry boxes first and then take the user input in those entries into a list for further calculation... – Saif uddin Dec 16 '15 at 12:33
  • You mean to create the entries based on an user input and then having the user fill these entries with values? – toti08 Dec 16 '15 at 12:40
  • I would really appreciate it, if you could help me make it..! – Saif uddin Dec 16 '15 at 12:44
  • the No. of Entries should be input by user in an Entry box..! – Saif uddin Dec 16 '15 at 12:47