Advertisement
steve-shambles-2109

Python Hearing frequency sound tester v1.0

Nov 17th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. """Hearing frequency sound tester v1.0
  2.  
  3. By Steve Shambles 2018. Updated Nov 2019.
  4.  
  5. Requirements: Windows only. No dependencies.
  6.  
  7. More code atrocities at:
  8. stevepython.wordpress.com
  9.  
  10. """
  11. from tkinter import Button, IntVar, Label, LabelFrame, Menu, messagebox
  12. from tkinter import Spinbox, TclError, Tk
  13. import webbrowser
  14. import winsound
  15.  
  16. def help_info():
  17.     """Help and offer info on hearing ranges at a website"""
  18.     ask_yn = messagebox.askyesno('Help', 'A typical healthy adults hearing'
  19.     ' is from 15hz to 20,000hz.\n\nI have Tinnitus and can only hear up to'
  20.     ' 6000hz.\n\n Conversley, my healthy younger sister can hear 21000hz.\n\n'
  21.     'For more info: https://hypertextbook.com/facts/2003/ChrisDAmbrose.shtml'
  22.     '\n\nClick YES to visit site, or NO to continue.')
  23.  
  24.     if ask_yn is True:
  25.         webbrowser.open('https://hypertextbook.com/facts/2003/ChrisDAmbrose.shtml')
  26.     else:
  27.         return
  28.  
  29. def about_menu():
  30.     """Display program info message box"""
  31.     messagebox.showinfo('About', 'Hearing Frequency Sound Tester V1.00'
  32.                         '\nFreeware by Steve Shambles 2018/2019')
  33.  
  34. def visit_blog():
  35.     """Visit my blog, you know it makes sense."""
  36.     webbrowser.open('https://stevepython.wordpress.com/2018/08/29/hearing-frequency-tester')
  37.  
  38. def hear_sound():
  39.     """Play the sound at hz frequency for duration."""
  40.     # If input is invalid, eg.non-numeric, then return
  41.     try:
  42.         dur = VAR.get()
  43.         freq = VAR2.get()
  44.     except TclError:
  45.         messagebox.showerror('Error', 'Bad input error, Numbers only please.')
  46.         return
  47.  
  48.     winsound.Beep(freq, dur)
  49.  
  50. def hfst_info():
  51.     """Reset spinboxes to default settings."""
  52.     VAR.set(3000)
  53.     VAR2.set(10000)
  54.  
  55. # The GUI.
  56. root = Tk()
  57. root.title('HFST V1.00')
  58. root.geometry('266x165')
  59. root.resizable(False, False)
  60.  
  61. main_frame = LabelFrame(root, text="Choose help from menu.")
  62. main_frame.grid(padx=20, column=0, row=0)
  63.  
  64. # Create the drop down menu.
  65. menu_bar = Menu(root)
  66. file_menu = Menu(menu_bar, tearoff=0)
  67. menu_bar.add_cascade(label='Menu', menu=file_menu)
  68. file_menu.add_command(label='Help', command=help_info)
  69. file_menu.add_command(label='About', command=about_menu)
  70. file_menu.add_separator()
  71. file_menu.add_command(label='Visit my blog', command=visit_blog)
  72. file_menu.add_command(label='Exit', command=root.destroy)
  73. root.config(menu=menu_bar)
  74.  
  75. # Create frame for duration label.
  76. drtn_frame = Label(main_frame)
  77. drtn_frame.grid(column=0, row=0)
  78.  
  79. # Create drtn_label.
  80. drtn_label = Label(drtn_frame, fg='blue',
  81.                    text='Duration Msecs', font=('tahoma', '10', 'bold'))
  82. drtn_label.grid(padx=8, pady=8) # label wont appear without this line
  83.  
  84. # frqncy_frame for frqncy_label.
  85. frqncy_frame = Label(main_frame)
  86. frqncy_frame.grid(column=0, row=1)
  87.  
  88. # frqncy_label.
  89. frqncy_label = Label(frqncy_frame, fg='blue', text='Frequency Hz',
  90.                      font=('tahoma', '10', 'bold'))
  91. frqncy_label.grid()
  92.  
  93. drtn_spinbox = Label(main_frame)
  94. drtn_spinbox.grid(column=1, row=0)
  95.  
  96. VAR = IntVar()
  97. VAR.set(3000) # Default start value.
  98. sbox_drtn_widget = Spinbox(drtn_spinbox, from_=500, to=23000,
  99.                            width=4, increment=1000, textvariable=VAR)
  100. sbox_drtn_widget.grid()
  101.  
  102. frq_spinbox = Label(main_frame)
  103. frq_spinbox.grid(column=1, row=1)
  104.  
  105. VAR2 = IntVar()
  106. VAR2.set(10000) # Default start value.
  107. sbox_frq_widget = Spinbox(frq_spinbox, from_=37, to=23000, width=5,
  108.                           increment=100, textvariable=VAR2)
  109. sbox_frq_widget.grid()
  110.  
  111. # hear_frame for hear button.
  112. hear_frame = Label(main_frame)
  113. hear_frame.grid(row=2, column=0)
  114.  
  115. # Create the 'Hear' button.
  116. hear_btn = Button(hear_frame, bg='green2', text='Hear', command=hear_sound)
  117. hear_btn.grid(padx=8, pady=8)
  118.  
  119. # Reset_frame for reset button.
  120. reset_frame = Label(main_frame)
  121. reset_frame.grid(row=2, column=1)
  122.  
  123. # Create the reset button.
  124. hear_btn = Button(reset_frame, bg='red', text='Reset', command=hfst_info)
  125. hear_btn.grid(padx=8, pady=8)
  126.  
  127. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement