试图按下主页上的应答按钮但窗口冻结:Tkinter

Posted

技术标签:

【中文标题】试图按下主页上的应答按钮但窗口冻结:Tkinter【英文标题】:Trying to press answer button on main page but window freezes: Tkinter 【发布时间】:2017-05-26 23:18:33 【问题描述】:
    #Learning how to create a GUI
    from tkinter import *

    from random import randint

    #Naming the Tk window
    master = Tk()

    #Setting the size of Tk Window
    master.geometry('800x750')


    #Dictionary for the questions
    easy3 = ["Who is CEO of Apple?", "Who is the founder of Facebook?", "Who is the CEO of Google?"]
    easy3_A = ["Tim Cook", "Mark Zuckerburg", "Sundar Pichai"]

    #Defining some variables
    question_number = randint(0,2)

    global score

    question_index = 0

#Procedure
    def callback():
        #A loading... symbol to ease a consumer's concern
        top = Toplevel(master)
        top.geometry('800x750')
        def question_label():
            question_label = Label(top, text = ('Your Question'), bg = 'light blue', font=('Times New Roman',20), highlightbackground='black', width = 25, height = 3)
            question_label.place(x=215,y=125)
            question_label.pack
        def question_text():
            question_text = Label(top, text = easy3[question_number], bg = 'light blue', font=('Times New Roman',16), highlightbackground='black', width = 25, height = 3)
            question_text.place(x=250,y=300)
            question_text.pack
        def answer():
            score = 0
            answer = input('Answer: ')
            if answer == easy3_A[question_number]:
                print('Correct!')
                score = score `enter code here`+ 1
                print('Score = ',score)

            else:
                print('Wrong!')
                print('GAME OVER!')        
        top.configure(background = 'light blue')
        image = PhotoImage(file="STEM_1_final.gif")
        w = Label(top, image=image)
        w.place(x=1,y=1)
        image_2 = PhotoImage(file="STEM_2_final.gif")
        c = Label(top, image=image_2)
        c.place(x=750,y=1)
        image_3 = PhotoImage(file="STEM_3_final.gif")
        a = Label(top, image=image_3)
        a.place(x=1,y=700)
        image_4 = PhotoImage(file="STEM_4_final.gif")
        d = Label(top, image=image_4)
        d.place(x=750,y=700)
        top.title('Question #1')
        b = Button(text="Answer", command=answer)
        b.pack()
        b.place(relx=.5, rely=.9, anchor=CENTER)
        question_text()                                          
        question_label()
        mainloop()



    def main_content():
        #A procedure to create and display the title

        title_label = Label(master, text = 'Can You Beat the Compu-tition?', bg = 'light blue', font=('Times New Roman',40), highlightbackground='black', width = 25, height = 3)
        title_label.pack()

    #Making the background of window light blue
    master.configure(background = 'light blue')

    #Titling the window, "Can you beat the Compu-tition?"
    master.title('Can You Beat the Compu-tition?')

    #Creating the button, and positioning it
    b = Button(text="Next Page", command=callback,)
    b.pack()
    b.place(relx=.5, rely=.75, anchor=CENTER)

    #Invoking the procedure to display the title
    main_content()

    #Uploading all photos for the user interface
    image = PhotoImage(file="STEM_1_final.gif")
    w = Label(master, image=image)
    w.place(x=1,y=1)
    image_2 = PhotoImage(file="STEM_2_final.gif")
    c = Label(master, image=image_2)
    c.place(x=750,y=1)
    image_3 = PhotoImage(file="STEM_3_final.gif")
    a = Label(master, image=image_3)
    a.place(x=1,y=700)
    image_4 = PhotoImage(file="STEM_4_final.gif")
    d = Label(master, image=image_4)
    d.place(x=750,y=700)
    image_5 = PhotoImage(file="STEM_5_final.gif")
    e = Label(master, image=image_5)
    e.place(x=400,y=350, anchor=CENTER)

    #Creating, displaying, positioning, and centering the slogan label
    slogan = Label(master, text = 'A Trivia Game In The STEM Field', bg = 'light blue',font=('Times New Roman',15))
    slogan.place(x=400,y=160, anchor=CENTER)




    #Making this whole piece of code specific to the Tkinter window run
    mainloop()

如果您按下主页上的应答按钮,它将不起作用。窗口只是说“没有响应”。我不知道用户使用什么模式来输入他们对问题的答案。如果您想输入问题的答案,请转到 Python Shell。

【问题讨论】:

你需要减少你发布的代码,直到只剩下一个minimal reproducible example。 我需要帮助,项目明天结束 这对除了你之外的任何人都没有关系。本网站是面向专业和爱好者程序员的 QA 资源,而不是最后一分钟的家庭作业完成服务。 您是否有意将函数放入callback 中?此外,如果您立即致电place,则致电pack() 毫无意义。使用其中之一,而不是两者。只有最后一个调用才会有任何效果。最后,您绝对不能多次致电mainloop 【参考方案1】:

当您单击Answer 时,您在控制台中执行input('Answer: '),这会阻止所有程序,mainloop() 可以完成它的工作 - 接收键/鼠标事件,将其发送到小部件,重绘小部件等 - 它看起来像程序冻结。在控制台中回答并回车后mainloop()可以再次工作。

使用Tkinter 元素,如Entry(或使用Entry 构建窗口)来获得答案。

另请参阅:SimpleDialog

import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()

answer = simpledialog.askstring("Answer", "Input answer")
print('Answer:', answer)

root.destroy()

【讨论】:

以上是关于试图按下主页上的应答按钮但窗口冻结:Tkinter的主要内容,如果未能解决你的问题,请参考以下文章

如何在python tkinter中按下按钮之前使窗口状态空闲?

当用户按下主页按钮但在应用程序进入后台之前,如何从应用程序窗口的 rootViewController 呈现 viewController? [复制]

在 Python Tkinter 中按下按钮后如何清除窗口?

按下“停止”按钮时如何停止动画并冻结图像

在 Tkinter 中执行函数期间程序冻结

按钮点击后,Python tkinter在框架中显示文本