Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 28th, 2013, 09:17 AM
Henry14 Henry14 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Henry14 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 42 sec
Reputation 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()


Reply With Quote
  #2  
Old February 28th, 2013, 11:39 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 45 m 4 sec
Reputation Power: 2
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.

Reply With Quote
  #3  
Old February 28th, 2013, 12:09 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
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!

Reply With Quote
  #4  
Old February 28th, 2013, 12:30 PM
Henry14 Henry14 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Henry14 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 42 sec
Reputation Power: 0
Got it working, Thanks

Reply With Quote
  #5  
Old February 28th, 2013, 12:42 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
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.

Reply With Quote
  #6  
Old February 28th, 2013, 03:52 PM
Henry14 Henry14 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Henry14 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 42 sec
Reputation Power: 0
Quote:
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()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Stuck with tkinter

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap