Hey this bit of code is giving a lot of trouble. I am new to python and have no idea why I am getting an error like this :

Traceback (most recent call last):
  File "D:/Python27/saved/digital_filter.py", line 83, in <module>
    app=Application(root)
  File "D:/Python27/saved/digital_filter.py", line 23, in __init__
    self.create_widgets()
  File "D:/Python27/saved/digital_filter.py", line 49, in create_widgets
    self.display_order()
  File "D:/Python27/saved/digital_filter.py", line 57, in display_order
    fp=float(f1)
ValueError: could not convert string to float: 

Please help. Thank you in advance

#Calculating coefficients of a digital filter

#Ishwar 03-02-2013

from Tkinter import *

pi=3.1416

#create root window

class Application(Frame):

    """A GUI app to calculate FIR filter coefficients"""

    def __init__(self,master):

        """Initialize the frame"""

        Frame.__init__(self,master)

        self.grid()

        self.create_widgets()

        self.display_order()

    def create_widgets(self):

        """To create all the widgets"""

        #lower cutoff frequency

        self.lf_lbl=Label(self,text="Enter the order Lower cutoff frequency")

        self.lf_lbl.grid(row=1,column=0,columnspan=2)

        self.lf_ent=Entry(self)

        self.lf_ent.grid(row=1,column=3,columnspan=2)

        #upper cutoff frequency

        self.hf_lbl=Label(self,text="Enter the order Lower cutoff frequency")

        self.hf_lbl.grid(row=3,column=0,columnspan=2)

        self.hf_ent=Entry(self)

        self.hf_ent.grid(row=3,column=3,columnspan=2)

    def display_order(self):

        f1=self.lf_ent.get()

        f2=self.hf_ent.get()

        fp=float(f1)

        fs=float(f2)

        dw=2*pi*(fs-fp)

        if dw<=0:

            self.ord_lbl=Label(self,text="invalid Cutoff frequencies")

        else:

            N=int(4*pi/dw)

            self.ord_lbl=Label(self,text=("The order of the Filter is ",N))

        self.ord_lbl.grid(row=4,col=0)

#main

root=Tk()

root.title("FIR Digital Filter")

root.geometry("640x480")

app=Application(root)

root.mainloop()

Recommended Answers

All 2 Replies

You are calling for the calculation before there are any values for f1 and f2.
Also self.ord_lbl should not be created everytime you do the calculation.

Study this updated code:

#Calculating coefficients of a digital filter

#Ishwar 03-02-2013

from Tkinter import *

pi=3.1416

#create root window

class Application(Frame):

    """A GUI app to calculate FIR filter coefficients"""

    def __init__(self,master):

        """Initialize the frame"""
        Frame.__init__(self,master)
        self.grid()

        self.create_widgets()
        #self.display_order()  # too early to call!!!!

    def create_widgets(self):

        """To create all the widgets"""

        #lower cutoff frequency
        self.lf_lbl=Label(self,text="Enter the order Lower cutoff frequency")
        self.lf_lbl.grid(row=1,column=0,columnspan=2)

        self.lf_ent=Entry(self)
        self.lf_ent.grid(row=1,column=3,columnspan=2)

        #upper cutoff frequency
        self.hf_lbl=Label(self,text="Enter the order Lower cutoff frequency")
        self.hf_lbl.grid(row=3,column=0,columnspan=2)

        self.hf_ent=Entry(self)
        self.hf_ent.grid(row=3,column=3,columnspan=2)

        calc_btn=Button(self,text="Calculate Order",command=self.display_order)
        calc_btn.grid(row=4,column=0)

        self.ord_lbl=Label(self)
        self.ord_lbl.grid(row=5,column=0)

    def display_order(self):

        f1=self.lf_ent.get()
        f2=self.hf_ent.get()
        # make sure values are there
        if f1 and f2:
            fp=float(f1)
            fs=float(f2)
            dw=2*pi*(fs-fp)
            print fp, fs, dw  # test only
            if dw<=0:
                self.ord_lbl['text']="invalid Cutoff frequencies"
            else:
                N=str(int(4*pi/dw))
                self.ord_lbl['text']="The order of the Filter is " + N

#main
root=Tk()
root.title("FIR Digital Filter")
root.geometry("640x480")

app=Application(root)

root.mainloop()

I wasnt aware of the Command attribute at all. Thanks for the help. Got a little more work, the rest being just calculations. Thanks for the help. I really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.