#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Stuck with tkinter


    I am trying to make a program for my final year highschool project, which counts towards my exam. In the programm, you input a frequency and it gives you the harmonic, and the graph of the final harmonic.

    I'm new to python and started learning python just for this project. I am pretty far, only I am stuck, I can't get my N variable to receive the value written in the box.
    I tried searching but they ways I found were with classes, and I am not that far in python yet. I also can't convert it directly to a number, because I have to see if the entry is a numeric value (in that case, it can be calculated directly) or the name of a note. For example, when the user enters Do4 I need to convert “it” to the corresponding frequency, in this case 523,25 HZ.

    Any help would be greatly appreciated.


    My code:
    Code:
     
    from tkinter import *
    from math import pi
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    
    # plotting function: clear current, plot & redraw
    def plot(x, y):
        plt.clf()
        plt.plot(x,y)
        # just plt.draw() won't do it here, strangely
        plt.gcf().canvas.draw()
    
    # just to see the plot change
    plotShift = 0
    def main():
        global plotShift
    
        x = np.arange(0.0,3.0,0.01)
        y = np.sin(2*np.pi*x + plotShift)
        plot(x, y)
            #Not ready yet, still template
    
    #GUI
    
    fen1 = Tk()
    
    txt1 = Label(fen1, text= "Frequence (en Hz) :")
    entr1 = Entry(fen1, textvariable=N)
    
    
    can1 = Canvas(fen1, width =717, height =312, bg="gray")
    photo = PhotoImage(file ="tabel de frequences_small2.gif")
    item = can1.create_image(358, 156, image =photo)
    
    txt1.grid(row =1, sticky =E)
    entr1.grid(row =1, column =2)
    can1.grid(row =1, column =4, rowspan =4, padx =10, pady =5)
    
    
    #Everything concerning the Scrollbar
    scrollbar = Scrollbar(fen1)
    scrollbar.grid(row =2, column =2 )
    
    mylist = Listbox(fen1, yscrollcommand = scrollbar.set )
    for i in range(20):
        K = N*2*pi*(i+1)
        mylist.insert(END, "F("+str(i+1)+")= sin("+str(K)+"*X)")
        mylist.insert(END,"N"+str(i+1)+"="+str(N*(i+1)))
    mylist.grid(row =2, column =2 )
    scrollbar.config( command = mylist.yview )
    
    #Everthing within the GUI conerning the Graph (mathplotlib)
    
    fig = plt.figure()
    
    canvas = FigureCanvasTkAgg(fig, master=fen1)
    toolbar = NavigationToolbar2TkAgg(canvas, fen1)
    canvas.get_tk_widget().grid(row=0,column=1, columnspan=4)
    toolbar.grid(row=0,column=1, columnspan=4, sticky=S)
    
    fen1.mainloop()
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Although it is supposed to work that way, and in fact does in Tk, itself, I have never gotten Tkinter to recognize the textvariable parameter. I just scrape the entry widget and assign the variable when I need it:

    pth=pth+self.lb1.get(i)

    for example.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Code:
    from Tkinter import *
    from math import pi
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    
    # plotting function: clear current, plot & redraw
    def plot(x, y):
        plt.clf()
        plt.plot(x,y)
        # just plt.draw() won't do it here, strangely
        plt.gcf().canvas.draw()
    
    # just to see the plot change
    plotShift = 0
    def main():
        global plotShift
    
        x = np.arange(0.0,3.0,0.01)
        y = np.sin(2*np.pi*x + plotShift)
        plot(x, y)
            #Not ready yet, still template
    
    #GUI
    
    fen1 = Tk()
    
    N = StringVar(fen1,'1')########################################
    txt1 = Label(fen1, text= "Frequence (en Hz) :")
    entr1 = Entry(fen1, textvariable=N)
    
    
    can1 = Canvas(fen1, width =717, height =312, bg="gray")
    photo = PhotoImage(file ='/tmp/wallpaper.gif')
    item = can1.create_image(358, 156, image =photo)
    
    txt1.grid(row =1, sticky =E)
    entr1.grid(row =1, column =2)
    can1.grid(row =1, column =4, rowspan =4, padx =10, pady =5)
    
    
    #Everything concerning the Scrollbar
    scrollbar = Scrollbar(fen1)
    scrollbar.grid(row =2, column =2 )
    
    mylist = Listbox(fen1, yscrollcommand = scrollbar.set )
    for i in range(20):
        K = float(N.get())*2*pi*(i+1)################################
        mylist.insert(END, "F("+str(i+1)+")= sin("+str(K)+"*X)")
        mylist.insert(END,"N"+str(i+1)+"="+str(float(N.get())*(i+1)))#################################################
    mylist.grid(row =2, column =2 )
    scrollbar.config( command = mylist.yview )
    
    #Everthing within the GUI concerning the Graph (matplotlib)
    
    fig = plt.figure()
    
    canvas = FigureCanvasTkAgg(fig, master=fen1)
    toolbar = NavigationToolbar2TkAgg(canvas, fen1)
    canvas.get_tk_widget().grid(row=0,column=1, columnspan=4)
    toolbar.grid(row=0,column=1, columnspan=4, sticky=S)
    
    fen1.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Got it working, Thanks
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    You imported pi from math
    but use np.pi

    y = np.sin(2*np.pi*x + plotShift)


    Please consider plotting the sum of the first few harmonics.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You imported pi from math
    but use np.pi

    y = np.sin(2*np.pi*x + plotShift)


    Please consider plotting the sum of the first few harmonics.
    Thanks for the tip

    I got everything working now, only one struggle left. I tried to convert it to an .exe with cx_freeze but so far no succes. Could anyone convert it to an executable and explain how you did it for in the future?

    Name: Fourtrance (named after Joseph Fourier, the one who discovered the mathematical transform)
    Icon: http://www.iconarchive.com/show/mega-pack-2-icons-by-ncrow/AoA-Audio-Extractor-icon.html
    Image used: http://s9.postimage.org/hgutytttr/tabel_de_frequences_small2.gif



    Code:
    Code:
     
    from tkinter import *
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    import matplotlib.pyplot as plt
    from pylab import *
    
    #--------------------------------DEFS--------------------------------------------------
    
    def fillList(sender):
        global entr1, mylist, N
    
        try:
            N = float(entr1.get())
    
            mylist.delete(0, END)
    
            for i  in range(20):
                K = N*2.0*pi*(i+1)
                mylist.insert(END, "F("+str(i+1)+")= sin("+str(K)+"*X)")
                mylist.insert(END,"N"+str(i+1)+"="+str(N*(i+1)))
    
    
    
            t = arange(0.0, 2.0, 0.01)
            s = (sin(2*np.pi*t*N)+sin(2*2*np.pi*t*N)+sin(3*2*np.pi*t*N)+sin(4*2*np.pi*t*N)+ sin(5*np.pi*t*N)+sin(6*2*np.pi*t*N)+sin(7*2*np.pi*t*N)+sin(8*2*np.pi*t*N)
            +sin(9*np.pi*t*N)+sin(10*2*np.pi*t*N)+sin(11*2*np.pi*t*N)+sin(12*2*np.pi*t*N)+sin(13*np.pi*t*N)+sin(14*2*np.pi*t*N)+sin(15*2*np.pi*t*N)+sin(16*2*np.pi*t*N)
            +sin(17*np.pi*t*N)+sin(18*2*np.pi*t*N)+sin(19*2*np.pi*t*N)+sin(20*2*np.pi*t*N))
    
            plot(t, s, linewidth=1.0)
    
            xlabel('Temps (s)')
            ylabel('Fréquence (Hz)')
            title('Application Transformée de Fourier pour N ')
            grid(True)
            show()
    
        except:
            pass
    
    #-----------------------------------GUI-------------------------------------------------
    
    fen1 = Tk()
    fen1.wm_title("Application Informatique du Transformée de Fourier")
    
    txt1 = Label(fen1, text= "Fréquence (en Hz) :")
    entr1 = Entry(fen1)
    entr1.bind("<Return>", fillList)
    
    can1 = Canvas(fen1, width =717, height =312, bg="gray")
    photo = PhotoImage(file ="tabel de frequences_small2.gif")
    item = can1.create_image(358, 156, image =photo)
    
    txt1.grid(row =1, sticky =E)
    entr1.grid(row =1, column =2)
    can1.grid(row =1, column =4, rowspan =4, padx =10, pady =5)
    
    
    #Everything concerning the Scrollbar
    scrollbar = Scrollbar(fen1)
    scrollbar.grid(row =2, column =2 )
    
    mylist = Listbox(fen1, yscrollcommand = scrollbar.set )
    mylist.grid(row =2, column =2 )
    scrollbar.config( command = mylist.yview )
    
    
    fen1.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo