ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 26 Mar 2018 09:32:24 -0500TypeError when trying to read a string entered in a graphical user interface and convert it to an expressionhttp://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/Hi!
I am trying to read a string entered by a user in a GUI constructed with tkinter, and convert this string to a function. However, Sage rises a TypeError when I do this. A minimal example follows.
import tkinter
def compute_phaser_polynomials():
var('omega')
str_GroupDelaySpec = entry1.get() # string with the expression of the group delay as a function of omega
#str_GroupDelaySpec = 'omega + 5/2'
print(str_GroupDelaySpec)
print(type(str_GroupDelaySpec))
gds.<omega> = QQ[]
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
root = tkinter.Tk()
entry1 = tkinter.Entry(root)
entry1.grid()
button1 = tkinter.Button(root, text="Compute phaser polynomials", command=compute_phaser_polynomials)
button1.grid(row=1)
root.mainloop()
When tkinter is not involved, the conversion works.
gds.<omega> = QQ[]
f(omega) = gds('omega + 5/2')
print(f(omega))
print(f(omega).parent())
print(type(f(omega)))
Out: omega + 5/2
Out: Symbolic Ring
Out: <type 'sage.symbolic.expression.Expression'>
Am I doing something wrong? Is this a bug?
Thanks!Thu, 08 Mar 2018 07:17:48 -0600http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/Answer by joaoff for <p>Hi!</p>
<p>I am trying to read a string entered by a user in a GUI constructed with tkinter, and convert this string to a function. However, Sage rises a TypeError when I do this. A minimal example follows.</p>
<pre><code>import tkinter
def compute_phaser_polynomials():
var('omega')
str_GroupDelaySpec = entry1.get() # string with the expression of the group delay as a function of omega
#str_GroupDelaySpec = 'omega + 5/2'
print(str_GroupDelaySpec)
print(type(str_GroupDelaySpec))
gds.<omega> = QQ[]
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
root = tkinter.Tk()
entry1 = tkinter.Entry(root)
entry1.grid()
button1 = tkinter.Button(root, text="Compute phaser polynomials", command=compute_phaser_polynomials)
button1.grid(row=1)
root.mainloop()
</code></pre>
<p>When tkinter is not involved, the conversion works.</p>
<pre><code>gds.<omega> = QQ[]
f(omega) = gds('omega + 5/2')
print(f(omega))
print(f(omega).parent())
print(type(f(omega)))
Out: omega + 5/2
Out: Symbolic Ring
Out: <type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>Am I doing something wrong? Is this a bug?</p>
<p>Thanks!</p>
http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?answer=41447#post-id-41447Hi, @dan_fulea!
Thank you for the answer, it really works. However, I'm still confused, as your solution returned an object of type "ring", and I was expecting an object of type "symbolic". Being honest, as my background is in engineering, I do not know what a "ring" is, I have never being introduced to the concept of "rings" before. Nevertheless, as you observed the issue in preparsing the function, I searched a little more and found another solution that returns a symbolic object. If I change
gds.<omega> = QQ[]
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
by
group_delay_spec(omega) = sage_eval(str_GroupDelaySpec, locals={'omega':omega})
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
the code returns
omega + 5/2
<type 'str'>
omega + 5/2
<type 'sage.symbolic.expression.Expression'>
Thu, 08 Mar 2018 14:58:31 -0600http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?answer=41447#post-id-41447Comment by slelievre for <p>Hi, <a href="/users/23512/dan_fulea/">@dan_fulea</a>!</p>
<p>Thank you for the answer, it really works. However, I'm still confused, as your solution returned an object of type "ring", and I was expecting an object of type "symbolic". Being honest, as my background is in engineering, I do not know what a "ring" is, I have never being introduced to the concept of "rings" before. Nevertheless, as you observed the issue in preparsing the function, I searched a little more and found another solution that returns a symbolic object. If I change</p>
<pre><code>gds.<omega> = QQ[]
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
</code></pre>
<p>by</p>
<pre><code>group_delay_spec(omega) = sage_eval(str_GroupDelaySpec, locals={'omega':omega})
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
</code></pre>
<p>the code returns</p>
<pre><code>omega + 5/2
<type 'str'>
omega + 5/2
<type 'sage.symbolic.expression.Expression'>
</code></pre>
http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?comment=41773#post-id-41773A ring is mostly just a place where you can add and multiply,
with multiplication and addition working well together.
The integers form a ring, the rationals form a ring, the reals form a ring, the complex numbers form a ring.
Polynomials with coefficients in a given ring also form a ring.
Matrices with entries in a given ring also form a ring.Mon, 26 Mar 2018 09:32:24 -0500http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?comment=41773#post-id-41773Answer by dan_fulea for <p>Hi!</p>
<p>I am trying to read a string entered by a user in a GUI constructed with tkinter, and convert this string to a function. However, Sage rises a TypeError when I do this. A minimal example follows.</p>
<pre><code>import tkinter
def compute_phaser_polynomials():
var('omega')
str_GroupDelaySpec = entry1.get() # string with the expression of the group delay as a function of omega
#str_GroupDelaySpec = 'omega + 5/2'
print(str_GroupDelaySpec)
print(type(str_GroupDelaySpec))
gds.<omega> = QQ[]
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
root = tkinter.Tk()
entry1 = tkinter.Entry(root)
entry1.grid()
button1 = tkinter.Button(root, text="Compute phaser polynomials", command=compute_phaser_polynomials)
button1.grid(row=1)
root.mainloop()
</code></pre>
<p>When tkinter is not involved, the conversion works.</p>
<pre><code>gds.<omega> = QQ[]
f(omega) = gds('omega + 5/2')
print(f(omega))
print(f(omega).parent())
print(type(f(omega)))
Out: omega + 5/2
Out: Symbolic Ring
Out: <type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>Am I doing something wrong? Is this a bug?</p>
<p>Thanks!</p>
http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?answer=41446#post-id-41446After replacing
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
by
group_delay_spec = gds(str_GroupDelaySpec)
print(group_delay_spec)
print(type(group_delay_spec))
the above worked for me, the Tkinter input box was opened, i typed `omega + 5/2` inside and pressed the run button. Results in the sage console launching the Tk box:
omega + 5/2
<type 'str'>
omega + 5/2
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
Inside of the Tk-function there is no way to "preparse" the "function" `group_delay_spec(omega)` (depending on no omega in fact), the left side in `group_delay_spec(omega) = gds(str_GroupDelaySpec)` must have a sense "as is". If a function is really needed, the the lambda definition or a clear `def` block should work.Thu, 08 Mar 2018 13:45:44 -0600http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?answer=41446#post-id-41446Comment by joaoff for <p>After replacing </p>
<pre><code>group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))
</code></pre>
<p>by</p>
<pre><code>group_delay_spec = gds(str_GroupDelaySpec)
print(group_delay_spec)
print(type(group_delay_spec))
</code></pre>
<p>the above worked for me, the Tkinter input box was opened, i typed <code>omega + 5/2</code> inside and pressed the run button. Results in the sage console launching the Tk box:</p>
<pre><code>omega + 5/2
<type 'str'>
omega + 5/2
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
</code></pre>
<p>Inside of the Tk-function there is no way to "preparse" the "function" <code>group_delay_spec(omega)</code> (depending on no omega in fact), the left side in <code>group_delay_spec(omega) = gds(str_GroupDelaySpec)</code> must have a sense "as is". If a function is really needed, the the lambda definition or a clear <code>def</code> block should work.</p>
http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?comment=41448#post-id-41448@dan_fulea your answer was very helpful. Based on it, I've formulated another answer that I added to my own question.Thu, 08 Mar 2018 15:03:05 -0600http://ask.sagemath.org/question/41438/typeerror-when-trying-to-read-a-string-entered-in-a-graphical-user-interface-and-convert-it-to-an-expression/?comment=41448#post-id-41448