0

I am trying to take input from the user using tkinter. It asks for a number. I then want to take that number and create the amount of excel cells. I have my "excelcells" function working on its own but when im trying to take variable x as a number and convert it into a list i get conversion errors that i cant get past.

from tkinter import *

window = Tk()

Label(window, text='Number of Cells').grid(row=0)
e1 = Entry(window)
e1.grid(row=0, column=1)
get = e1.get()
numfinal = int(get)


x = []

for i in range(0,int(numfinal)):
    x.append(i)

def excelcells(x):
    height = 1
    for i in range(height):
        for j, val in enumerate(x):
            b = Entry(window)
            b.insert(0, val)
            b.grid(row=i, column=j)
        window.mainloop()


Button(window, text='Create', command=excelcells(x)).grid()

window.mainloop()

This is the error i get "could not convert string to float: 'get'"

How can i pass the x list to my "excelcells" function when the create button is clicked? and convert the input given to the entry window from a string to an int?

part2:

How do i convert get variable from a string to an int?

current error: invalid literal for int() with base 10: ''

6
  • 1
    float('get')?Or do you mean float(get)? Commented Sep 11, 2020 at 15:48
  • command=excelcells(x) should be command=lambda: excelcells(x). Commented Sep 11, 2020 at 15:50
  • See stackoverflow.com/q/5767228/7432 for at least part of the problem. Commented Sep 11, 2020 at 15:53
  • @jizhihaoSAMA yeah sorry that was a previous mistake. It still throws an error with get Commented Sep 11, 2020 at 15:53
  • my issue is how do i convert get from a string to an int? my error with Label(window, text='Number of Cells').grid(row=0) e1 = Entry(window) e1.grid(row=0, column=1) get = e1.get() numfinal = int(get) x = [] for i in range(0,int(numfinal)): x.append(i) error: invalid literal for int() with base 10: '' Commented Sep 11, 2020 at 15:55

1 Answer 1

1

you have several problems here. first, the command in

Button(window, text='Create', command=excelcells(x)).grid()

should be a function, and writing excelcells(x) actually calls the function and not passing it. (can be done with command=lambda x: excelcells(x) but it's not really what you want here)

Now, the problem that causes the error is that this bit:

get = e1.get()
numfinal = int(get)

is done sequentially right after creating the entry and therefore you are reading before the program had a chance to get to the window.mainloop() which pops up the window and waits for user interaction.

Third problem is that you call mainloop within the function, which will cause unwanted behavior later.

So to conclude, I believe that what you want is something like that:

from tkinter import *

window = Tk()

Label(window, text='Number of Cells').grid(row=0)
e1 = Entry(window)
e1.grid(row=0, column=1)

def excelcells():
    get = int(e1.get())
    for i in range(get):
        b = Entry(window)
        b.insert(0, i)
        b.grid(row=i+2, column=0)


Button(window, text='Create', command=excelcells).grid()

window.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.