Tkinter中的窗口销毁错误

时间:2018-04-07 21:40:56

标签: python loops tkinter window exit

我正在尝试使用Tkinter创建一个Hangman游戏GUI。除了几件事,我还有完整的代码。我想创建一个循环,通过按下按钮重新启动游戏,或者按" no"退出游戏。

我还希望这些按钮出现在新窗口中并关闭窗口。

我已删除了不需要的代码部分,因此您只能看到主要问题区域和主要想法。为了使它工作,你只需输入" b"然后" o":

discord.js-commando

如果我按" no",我收到以下错误:

from Tkinter import *
item = "BOO"
oldString = "-" * (len(item))
blank = "-" * (len(item))
guesses = 10

def start():
    winMain.destroy()
    mainMenu()


def theWinner():
    def end():
        root.destroy()

    def replay():
        root.destroy()
        mainMenu()

    root = Tk()
    root.title("HANGMAN GAME DEMO")
    answer = Entry(root)
    answer.insert(END, "You Won!!! Play again?")
    answer.grid(row = 0, column = 0)
    yesB=Button(root, text = "Yes", command = replay)
    yesB.grid(row = 1, column = 0)
    noB = Button(root, text = "Quit", command = end)
    noB.grid(row = 1, column = 1)
    root.mainloop()


def mainMenu():
    def gameOn():
        global guesses
        global oldString
        newString = ""
        i = 0
        x = len(item)
        hanged = ""
        readString = answerIn.get().upper()
        winner = 1
        if readString not in item:
            guesses -= 1
        elif readString in item:
            while x > 0:
                if item[i] != readString:
                    newString = newString + oldString[i]
                elif item[i] == readString:
                    newString = newString + readString
                i +=1   
                x -=1

            oldString = newString
            out.config(state=NORMAL)
            out.delete(0,END)
            out.insert(0,oldString);
            out.config(state=DISABLED)
            answerIn.delete(0,END)
            if oldString == item:
                win.destroy()
                theWinner()


        newString = ""
        i=0
        x=len(item)

        answerIn.delete(0,END)

    win = Tk()
    win.geometry ("665x480")
    win.title("HANGMAN GAME DEMO")
    win.configure(background='LightBlue2')

   #Display of the word user is trying to guess
    guessWord = Entry(win, disabledbackground = "mint cream", disabledforeground = "black", font="helvetica 11 bold", width = 12, border = 2)
    guessWord.grid(row=1, column=0, pady = 10, padx = 6, sticky = W)
    guessWord.insert(END, "The word is: ")
    guessWord.config(state = DISABLED)
    #guessWord = Label(win, bg = "mint cream", font="helvetica 10 ", text = "The word you have to guess is: ")
    guessWord.grid(row=1, column=0, pady = 10, padx = 9, sticky = W)
    out = Entry(win, border = 2,  font="helvetica 15 bold", width = 12, disabledforeground = "black", disabledbackground = "gold")
    out.insert(0,blank);
    out.config(state=DISABLED)
    out.grid(row=1, column=0, pady = 10, padx = 200, sticky = W)


    #Type in Window
    answer = Entry(win, disabledbackground = "mint cream", disabledforeground = "black", font="helvetica 10", width = 21, border = 2)
    answer.grid(row=2, column=0, pady = 10, padx = 8, sticky = W)
    answer.insert(END,"Please type in the letter: ")
    answer.config(state=DISABLED)
    answerIn = Entry(win,width = 3, border = 2)
    answerIn.focus_set()
    answerIn.grid(row=2, column=0, pady = 10, padx = 200, sticky = W)

    b = Button(win, text = "Enter",command = gameOn, bg = "chartreuse2", font="helvetica 10 bold",)
    b.grid(row=2, column=0, pady = 10, padx = 200)


    win.mainloop()

winMain = Tk()
winMain.title("HANGMAN GAME DEMO")
imageLabel = Label(winMain, text = "HELLO!")
imageLabel.pack()
winMain.after(1000, start)
winMain.mainloop()

如果我按"是",它似乎从一开始就没有重启游戏。它只允许我在再次启用Win窗口之前输入一个字符。

我哪里出错,是否有快速解决方法?

3 个答案:

答案 0 :(得分:1)

错误是因为在销毁窗口后您要删除entry widget中的内容。见这部分

def mainMenu():
    .......
    .......
    ......
    if oldString == item:
           win.destroy()
           theWinner()


   # newString = ""
   # i=0
   # x=len(item)

 #   answerIn.delete(0,END)

你可以看到我评论说你在关闭窗口之后不需要它的部分,当你这样做时,错误不会再次弹出。关于游戏没有启动它的事情你应该创建另一个功能相同命令为它,因为

def replay():
    root.destroy()
    mainMenu()
当你打电话时,它不起作用。我还建议您使用Toplevel窗口,而不是多调用Tkmainloop

检查这个链接[Toplevel documentation][1]。每个人都很难调试你的代码,因为你在另一个函数下创建了几个函数。

答案 1 :(得分:0)

结束游戏时需要重置oldString

替换

if oldString == item:
    win.destroy()
    theWinner()

使用

if oldString == item:
    win.destroy()
    oldString='---'
    theWinner()

它应该有效(我已经测试过)。

答案 2 :(得分:-1)

如果不尝试在代码库中解决您的问题,我建议将代码切换为基于类的,这样您就可以更好地控制并且可以更容易地保留引用以使某些gui-stuff按预期工作。奖励是你可以抛弃你所拥有的globals

有一个优秀的py2 tkinter doc,其中复制这个最小的例子只是为了给出基于类的编码风格的概念,这通常是首选的方式。你似乎是在py2上,但是如果你在py3上运行文档也是如此,你只需要跟踪导入中的细微差别。

import Tkinter as tk
class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.quitButton = tk.Button(self, text='Quit', command=self.quit)
        self.quitButton.grid()

app = Application()
app.master.title('Sample application')
app.mainloop()