首页 > 解决方案 > 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()

标签: pythonpython-3.xtkinter

解决方案


推荐阅读