【问题标题】:Why is my tkinter GUI freezing at the end of my program?为什么我的 tkinter GUI 在我的程序结束时冻结?
【发布时间】:2020-04-19 04:31:37
【问题描述】:

我无法让它工作,请有人帮助我。当它应该允许另一个输入时,我的窗口在我的程序结束时冻结。 整个程序粘贴在下面,我到处查找帮助并卡住了。抱歉代码太粗糙了,我一直在组合不同的sn-ps,希望好运。

使用 python 3.8.1

import time
from tkinter import *
from random import randint
answers = ["It is almost certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"]

queries = []
repeat = "You already asked that question."
think = "thinking..."
sorry = "Sorry, I didn't understand you. Exiting..."
yes = ["YES", "yes", "Yes", "ya", "Ya", "YA", "y", "Y"]
no = ["NO", "no", "No", "nah", "Nope", "n", "N"]


def question():
    query = e1.get()
    print(think)
    time.sleep(2)
    if query not in queries:
        queries.append(query)
    else:
        print(repeat)
    print(answers[randint(0,19)])
    restart()

def restart():
    reply = input("Thanks for playing. Do you want to ask another question? ")
    if reply in yes:
        e1.delete(0, END)
        question()
    elif reply in no:
        exit()
    else:
        print(sorry)
    exit()

root = Tk()
root.title("Magic 8 Ball")
logo = PhotoImage(file="Blank.png")

w1 = Label(root, image=logo).pack(side="top")

w2 = Label(root,justify=CENTER,pady=10,padx=20,font="Verdana 12 bold",text="Hello! Welcome to the Magic 8 Ball.").pack(side="top")

w3 = Label(root,justify=LEFT,font="Verdana 10",text="Enter your question here: ").pack(side="top")


e1 = Entry(root,width=50)
e1.pack(side="top")



b1 = Button(root,width=10,text="Submit",command=question).pack(side="top")

w4 = Label(root,justify=LEFT,pady=20,font="Verdana 10",text="Answer:").pack(side="top")

        
root.mainloop()

【问题讨论】:

    标签: python-3.x tkinter tkinter-entry


    【解决方案1】:

    您不应在 GUI 应用程序中混用控制台 input。请改用tkinter.messagebox.askquestion():

        from tkinter.messagebox import askquestion
    
        ...
    
        def restart():
            reply = askquestion("Confirm", "Thanks for playing.\nDo you want to ask another question?")
            if reply in yes:
                e1.delete(0, END)
                #question()
            elif reply in no:
                exit()
            else:
                print(sorry)
                exit()
    

    【讨论】:

    • 非常感谢!你是救生员!
    猜你喜欢
    • 1970-01-01
    • 2014-09-14
    • 2020-02-23
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多