Quick run down of my program, it is an app (or box) that is supposed to help you learn Morse code. So the issue I'm having at the moment is I'm wanting to remove (destroy) a TopLevel window before a new one is created and my attempt at doing this is at the command=lambda: newwin.destroy() code in the newword function. Sorry for the lack of organisation and bad variable/function names I'm a few months into coding.
from tkinter import *
import winsound
import random
window = Tk()
window.resizable(0, 0)
window.title("Morse Code")
frequency = 860
longbeep = 750
shortbeep = 350
window.geometry("270x130+0+0")
translate = list("")
samplewords = [""]
morsecodelist = [""] # My word lists are 1000 words long each so this post would be over the limit
def firstbuttoncmd():
firstbutton.grid_forget()
secondbutton.grid_forget()
window.geometry("640x350")
short = Button(text="Short Beep\n.", height=16, width=36, command=lambda: fpshortcmd())
long = Button(text="Long Beep\n___", height=16, width=36, command=lambda: fplongcmd())
short.grid(row=10, column=1)
long.grid(row=10, column=10)
cheatsheet = Label(
text="A .__ U ..__\nB __... V ...__\nC __.__. W .__ __\nD__.. X__..__\nE . Y __.__ __\nF ..__. Z __ __ ..\nG __ __. \nH .... \nI .. \nJ.__ __ __ \nK __.__ 1 .__ __ __ __\nL .__.. 3 ..__ __ __\nM __ __ 3 ...__ __\nN __. 4 ....__\no __ __ __ 5 .....\nP.__ __. 6 __....\nQ __ __.__ 7 __ __...\nR .__. 8 __ __ __..\nS ... 9 __ __ __ __.\nT __ 0 __ __ __ __ __")
cheatsheet.grid(row=10, column=12)
def wordgenerator():
newwin = Toplevel(window)
newwin.resizable(0, 0)
global newwin
newwin.geometry("140x100+640+0")
wordchoice = (random.randint(0, len(samplewords)))
randomword = Label(newwin, text="Your Word is '" + samplewords[wordchoice - 1] + "'")
randomword.grid()
resetword = Button(newwin, text=" Check ", command=lambda: newword())
resetword.grid()
def newword():
stuff = "".join(translate)
if stuff in morsecodelist:
newwin.geometry("740x100+640+0")
correct = Label(newwin, text="You Spelt " + "'" + samplewords[wordchoice - 1] + "'" + " Correct")
randomword.grid_forget()
resetword.grid_forget()
correct.grid()
next = Button(newwin, text="Next", command=lambda: newwin.destroy(), wordgenerator())
next.grid()
else:
incorrect = Label(newwin, text="Incorrect")
incorrect.grid()
translate.clear()
next = Button(newwin, text="Next", command=lambda: newwin.destroy(), wordgenerator())
next.grid()
wordgenerator()
def secondbuttoncmd():
window.geometry("640x350+0+0")
firstbutton.grid_forget()
secondbutton.grid_forget()
short = Button(text="Short Beep\n.", height=16, width=36, command=lambda: shortcmd())
long = Button(text="Long Beep\n___", height=16, width=36, command=lambda: longcmd())
short.grid(row=10, column=1)
long.grid(row=10, column=10)
cheatsheet = Label(
text="A .__ U ..__\nB __... V ...__\nC __.__. W .__ __\nD__.. X__..__\nE . Y __.__ __\nF ..__. Z __ __ ..\nG __ __. \nH .... \nI .. \nJ.__ __ __ \nK __.__ 1 .__ __ __ __\nL .__.. 3 ..__ __ __\nM __ __ 3 ...__ __\nN __. 4 ....__\no __ __ __ 5 .....\nP.__ __. 6 __....\nQ __ __.__ 7 __ __...\nR .__. 8 __ __ __..\nS ... 9 __ __ __ __.\nT __ 0 __ __ __ __ __")
cheatsheet.grid(row=10, column=11)
def shortcmd():
winsound.Beep(frequency, shortbeep)
def longcmd():
winsound.Beep(frequency, longbeep)
def fpshortcmd():
translate.append(".")
winsound.Beep(frequency, shortbeep)
def fplongcmd():
translate.append("_")
winsound.Beep(frequency, longbeep)
firstbutton = Button(text="One Player", fg="Red", height=8, width=18, command=firstbuttoncmd)
firstbutton.grid(row=10, column=1)
secondbutton = Button(text="Multiplayer", fg="Red", height=8, width=18, command=secondbuttoncmd)
secondbutton.grid(row=10, column=2)
window.mainloop()