I am very new to Python (and programming for the most part) and I am working on a project to beacon morse code out and listen for morse code between beacons.
I have beacon working, probably not the best code but it does work, however when the loop starts, the Tkinter screen freezes and the stop beacon button does not work until all beacons are complete.
I would like to run the beacon infinitely and just use the stop beacon button to stop it but so far I can't seem to figure out how to stop the loop.
#!usr/bin/env python
import sys
import re
import tkMessageBox
from Tkinter import *
import pygame
import time
CODE = {'A': '.-', 'B': '-...', 'C': '-.-.',
'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..',
'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-',
'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.'
}
ONE_UNIT = 0.5
THREE_UNITS = 3 * ONE_UNIT
SEVEN_UNITS = 7 * ONE_UNIT
PATH = 'morse_sound_files/'
def verify(string):
keys = CODE.keys()
for char in string:
if char.upper() not in keys and char != ' ':
sys.exit('Error the charcter ' + char + ' cannot be translated to Morse Code')
beaconout = ''
beaconTEXT = 'this is text that is default'
def ask_quit():
if tkMessageBox.askokcancel("Quit", "are you sure you want to quit?"):
root.destroy()
def getinput():
incomingTEXT = incoming.get()
outboundTEXT = outbound.get()
beaconTEXT = beaconmessage.get(1.0,"end")
beaconout = outboundTEXT+" "+outboundTEXT+" "+outboundTEXT+" "+beaconTEXT+""+incomingTEXT
print beaconout
beaconout = beaconout.replace('\n', ' ')
print beaconout
print 'Welcome to Alphabet to Morse Code Translator v.01'
msg = beaconout
#verify(msg)
print
pygame.init()
for char in msg:
if char == ' ':
print ' '*7,
time.sleep(SEVEN_UNITS)
else:
print CODE[char.upper()],
pygame.mixer.music.load(PATH + char.upper() + '_morse_code.ogg')
pygame.mixer.music.play()
time.sleep(THREE_UNITS)
root = Tk()
root.geometry("800x600+300+300")
frame = Frame(root, width=1000, height=600)
label1 = Label(root, text="To: Call Sign:")
label2 = Label(root, text="Your Call Sign:")
label3 = Label(root, text="Enter your message:")
outbound = StringVar()
outboundcallsign = Entry(root, textvariable=outbound)
incoming = StringVar()
inboundcallsign = Entry(root, textvariable=incoming)
beacon = StringVar()
beaconmessage = Text(root, height=1, width=30)
label1.grid(row=1, sticky=E)
label2.grid(row=2, sticky=E)
label3.grid(row=3, sticky=E)
outboundcallsign.grid(row=1, column=1)
inboundcallsign.grid(row=2, column=1)
beaconmessage.grid(row=4, columnspan=4)
cbox = Checkbutton(root, text="message is ready to beacon?")
cbox.grid(columnspan=2)
submitbut = Button(root,text="Start Beacon", command = getinput)
submitbut.grid(row=14,column=1)
submitbut.bind("<Button-1>")
cancelbut = Button(root,text="Stop Beacon", command=ask_quit)
cancelbut.grid(row=14, column=3)
root.mainloop()
getinputinto a thread, or rewrite yourfor char in msgloop usingroot.afterinstead ofsleep