'I am trying to get an int from entry
import tkinter as tk
import math
window = tk.Tk()
label = tk.Label(text = 'Want to find a root?')
label.pack()
entry = tk.Entry(fg = 'blue')
entry.pack()
n = entry.get()
number = int(n)
answers = {}
roots = [x for x in range(2, 100)]
def search(number):
for i in roots:
if number > i:
if number//i**2 != 0:
if number//i**2 != 1:
if (i**2)*(number//i**2) == number:
answers[i] = number//i**2
print(answers)
search(number)
window.mainloop()
So I need to get a integer from entry and work with it as a int, but entry gives me a string with which i can't work.I can't type a int in entry because the programm doesn't start due to an error Error:number = int(n) ValueError: invalid literal for int() with base 10: ''
Solution 1:[1]
You are getting the value of an Entry you created seconds ago. Of course it will be nothing! Use entry.get() when you need it, not anytime before. Put the .get() in the search function, and then call int on it.
Updated search function:
def search(number):
number = int(entry.get())
for i in roots:
if number > i:
if number//i**2 != 0:
if number//i**2 != 1:
if (i**2)*(number//i**2) == number:
answers[i] = number//i**2
And remove the two lines in your code that get the entry value and turn it into an int.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shib |
