-3

I have to take complex number as an input from Entry widget of tkinter and perform the conjugate operation on that complex number. I applied explicit conversion method but my code is not able to convert Entry widget string into complex number and showing error "ValueError: complex() arg is a malformed string" Can anyone help me? Thank you in advance.

    lbl_shh=Label(second_root,text="Enter parameter Shh",fg="red").grid(column=0,row=7,padx=20,pady=20)
    e_shh = Entry(second_root)
    lbl_svv=Label(second_root,text="Enter parameter Svv",fg="red").grid(column=0,row=8,padx=20,pady=20)
    e_svv = Entry(second_root)
    e_shh.grid(column=1,row=7)
    e_svv.grid(column=1,row=8)

    shh=e_shh.get()
    svv=e_svv.get()

    shh=shh.replace(" ","")
    svv=svv.replace(" ","")

    shh=complex(shh)
    svv=complex(svv)

    #shh=complex(''.join(shh.split()))
    #svv=complex(''.join(svv.split()))

    shhs=np.conjugate(shh)
    svvs=np.conjugate(svv)

    num= svv*svvs
    dem=shh*shhs
    f=np.power(num/dem, 0.25)

I have to print the value of f

varsha
  • 1
  • 1
  • 3
  • What is the value of the string you are passing to `complex` when it errors? – khelwood Sep 06 '19 at 08:33
  • I just executed my code and it is showing error before taking any value in Entry widget. – varsha Sep 06 '19 at 08:47
  • 1
    You created the `Entry` widgets, and then try to convert the values right away when it is still empty. – Henry Yik Sep 06 '19 at 09:01
  • Then how I get the Entry widget value and perform operation. Where I will store it? What is the other way to take complex number as an i/p from GUI and perform required operation? – varsha Sep 06 '19 at 09:56
  • a couple of comments - 1) use a `Button` to `get` the values from the `Entry` widgets; and 2) the `lbl_shh` and `lbl_svv` will store `None`. Try to not use `.grid` when you are assigning a tkinter widget to a variable – FrainBr33z3 Sep 06 '19 at 10:03
  • You are already taking the `Entry` widget value, but the value is `''`, which does not look like a complex number at all. – Stop harming Monica Sep 06 '19 at 10:08
  • 1
    @FrainBr33z3 the OP is using grid perfectly fine here. This is not part of their problem. – Mike - SMT Sep 06 '19 at 12:01
  • @Mike-SMT I know. Just gave it as a tip for the future. The variable is useless if used in the manner that OP has used – FrainBr33z3 Sep 06 '19 at 21:14
  • Hey! Thank you all. I found the solution by taking inputs real and imaginary part separately from the user and make the events to perform required operation by clicking a button. :) – varsha Sep 07 '19 at 06:15

1 Answers1

1

I think you misunderstand how to properly get information within tkinter and probably Python in general.

You cannot just use .get() when your code is just initializing. It will always return an empty string unless you have some code that sets the value prior to get and at that point its just redundant to use get.

What you need to do is have some code like a button that will pull the value of your entry(s) after someone has added something to them.

Also I noticed in your example code you have second_root and this leads me to believe you are using 2 instances of Tk() in your code. If that is the case this may also be part of your problem. You should only ever have one instance of Tk() when coding in tkinter.

To ilistrate your problem Take this example:

I added some print statements, a function and a button to show what was actually being grabbed by get() or rather to show it is an empty string. If you do not have anything in the field by the time get() is executed.

enter image description here

And here is an example result from when you put a proper value that complex() can use.

enter image description here

See below example to get an idea of how get() works:

import tkinter as tk


root = tk.Tk()

entry = tk.Entry(root)
entry.pack()


def print_entry():
    print(entry.get())


tk.Button(root, text='Print Entry', command=print_entry).pack()

root.mainloop()

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79