快速运行我的程序,它是一个应用程序(或盒子),可以帮助您学习摩尔斯电码。 因此,我目前遇到的问题是,我想在创建一个新窗口之前删除(破坏)TopLevel窗口,而我的尝试是在命令= lambda:newwin.destroy()代码中进行。 newword函数。对不起,由于缺乏组织和错误的变量/函数名,我花了几个月的时间编写代码。
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()