I am using tkinter and scipy to make a program that integrates a function, lets say with respect to x. this is my code:
def newtest1():
intwindow = Tk()
Label(intwindow, text = 'enter here', font = ('Symbol')).grid(row=0)
Label(intwindow, text = 'enter upper bound', font = ('Symbol')).grid(row=1)
Label(intwindow, text = 'enter lower bound', font = ('Symbol')).grid(row=2)
get1 = IntVar()
Entry(intwindow, textvariable = get1).grid(row=0, column=1)
getupper = IntVar()
Entry(intwindow, textvariable = getupper).grid(row=1, column=1)
getlower = IntVar()
Entry(intwindow, textvariable = getlower).grid(row=2, column=1)
bty = Button(intwindow, text='click', command = lambda: newfunction()).grid(row=3)
def newfunction():
x69 = get1.get()
x_upper = getupper.get()
x_lower = getlower.get()
testz = int(x69)
upperz= int(x_upper)
lowerz = int(x_lower)
x9 = lambda x: x69
res = integrate.quad(x9, x_lower,x_upper)
randomwindow = Tk()
label = Label(randomwindow, text = 'you got {} '.format(round(res[0])))
label.grid(row=1)
# Label(randomwindow, text = 'you got %s ' % res).grid(row=1)
randomwindow.mainloop()
mainloop()
newtest1()
if you look inside the function newfunction() there is the code
x9 = lambda x: x69
res = integrate.quad(x9, x_lower,x_upper)
I was hoping that in the pop up box, I could enter something like say 2*x and it would integrate. However previously I used get1 = IntVar() when testing to see if numbers work in the entry box. I tried removing this for what Im currently trying to achieve but that gives some logical errors. Why can't I enter my own function in terms of x in the pop up box so that python can integrate it?
EDIT
@furas this is the code i use now
import numpy
from numpy import *
import scipy.integrate as integrate
import scipy.special as special
from tkinter import *
def newtest1():
intwindow = Tk()
Label(intwindow, text = 'enter here', font = ('Symbol')).grid(row=0)
Label(intwindow, text = 'enter upper bound', font = ('Symbol')).grid(row=1)
Label(intwindow, text = 'enter lower bound', font = ('Symbol')).grid(row=2)
get1 = StringVar()
Entry(intwindow, textvariable = get1).grid(row=0, column=1)
getupper = IntVar()
Entry(intwindow, textvariable = getupper).grid(row=1, column=1)
getlower = IntVar()
Entry(intwindow, textvariable = getlower).grid(row=2, column=1)
bty = Button(intwindow, text='click', command = lambda: newfunction()).grid(row=3)
def newfunction():
x69 = get1.get()
x_upper = getupper.get()
x_lower = getlower.get()
#testz = int(x69)
upperz= int(x_upper)
lowerz = int(x_lower)
x9 = eval(f"lambda x: {x69}")
res = integrate.quad(x9, x_lower,x_upper)
randomwindow = Tk()
label = Label(randomwindow, text = 'you got {} '.format(round(res[0])))
label.grid(row=1)
# Label(randomwindow, text = 'you got %s ' % res).grid(row=1)
randomwindow.mainloop()
mainloop()
newtest1()
And this is the error i get
Exception in Tkinter callback
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Users/isa/Desktop/Diff Eqns/diffeqn.py", line 45, in <lambda>
bty = Button(intwindow, text='click', command = lambda: newfunction()).grid(row=3)
File "/Users/isa/Desktop/Diff Eqns/diffeqn.py", line 58, in newfunction
x9 = eval(lambda x: {x69})
TypeError: eval() arg 1 must be a string, bytes or code object