python - Tkinter 窗口不会在按钮按下时破坏
问题描述
快速运行我的程序,它是一个可以帮助您学习摩尔斯电码的应用程序(或盒子)。所以我现在遇到的问题是我想在创建新窗口之前删除(销毁)一个 TopLevel 窗口,我尝试这样做是在 command=lambda: newwin.destroy() 代码中新词功能。对于缺乏组织和错误的变量/函数名称,我很抱歉我已经编码了几个月。
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()
解决方案
推荐阅读
- c++ - 在 Visual C++ 中与 Stockfish(外部可执行文件)通信的最佳方式
- kubernetes - Connection 拒绝从 pod 到服务的通信
- java - 如何使用 Omnifaces 1.14.1 修复 javax.xml.xpath.XPathFactoryConfigurationException
- javascript - 生产错误但不在预生产中
- android - 将数据从 Native Activity 传递给 Ionic
- django - 使用 django 将数据插入数据库时如何发送 whatsapp 消息
- pandas - 如何将 Pandas 日期时间列从没有夏令时调整的 EST 转换为有夏令时调整的 CET
- c++ - How to protect MSI against modification
- android - 无法使用查找键获取所有联系人
- sql-server - SQL Server 2012 / 2018 - Invalid object name