简体   繁体   English

Tkinter按钮功能通过MessageBox控制

[英]Tkinter Button Function Control by MessageBox

The code below shows part of my program and the issue im facing. 下面的代码显示了我的程序的一部分以及所面临的问题。

def checkAnswer():
mainAnswer = answer01.get()
if len(mainAnswer) == 0:
    messagebox.showwarning(message='Please answer the question!')
    return
if int(mainAnswer) != answer:
    messagebox.showwarning(message='Incorrect! The correct answer is: ' + str(answer))
else:
    nxtquest.config(state=NORMAL)
    messagebox.showinfo(message='Correct! :)')question01 = Label(easy)
question01.grid(row=2, column=0)
answer01 = Entry(easy)
answer01.grid(row=3, column=2)
answer01.bind('<Return>', func=lambda e:checkAnswer())
start = Button(easy, text = "Start!", command=ask, bg='green', fg='white')
start.grid(row=3, column=3) 
nxtquest = Button(easy, text='Next Question', command=ask)
nxtquest.grid(row=5, column=2)
checkbut = Button(easy, text='Check', command=checkAnswer)
checkbut.grid(row=4, column=2)
#check button and answer01 enabled after start pressed
launch = 1 
if launch == 1:
    answer01.config(state=DISABLED)
    checkbut.config(state=DISABLED)
    nxtquest.config(state=DISABLED)

The issue which im struggling here is that whenever i run the program everything is okay. 我在这里苦苦挣扎的问题是,每当我运行程序时,一切都会好起来的。 When the window is displayed checkbut, nxtquest and label answer01 are greyed out (disabled). 显示该窗口时,选中,但是nxtquest和标签answer01变灰(禁用)。 The start button enables only checkbut and answer01 and then is destroyed. 启动按钮仅启用checkbut和answer01,然后销毁。 (So far so good) So nxtquest will enable once the input is correct as seen in the else: nxtquest.config(state=NORMAL) But when I reach another question the nxtquest button is already enabled, this is the problem! (到目前为止,一切都很好)因此,一旦输入正确就可以启用nxtquest,如else所示:nxtquest.config(state = NORMAL)但是当我遇到另一个问题时,nxtquest按钮已经启用,这就是问题所在!

How could I make it so the button will enable itself only after the warning message box is displayed? 我如何才能使按钮仅在显示警告消息框后才能启用?

Could I ask for some help with this and possibly suggestions if you see any rookie mistakes ? 如果您遇到任何菜鸟错误,请问对此有什么帮助,还可以提出建议吗?

Whilst I don't know of any way you could do this with a messagebox widget (although I'm sure there's an event you could use as the trigger) you can most certainly do this by substituting the messagebox with a Toplevel widget and using .protocol("WM_DELETE_WINDOW", callback()) on the widget. 尽管我不知道您可以使用messagebox小部件执行任何操作(尽管我确定有一个事件可以用作触发器),但是您可以肯定地通过用Toplevel小部件替换messagebox并使用来做到这一点.protocol("WM_DELETE_WINDOW", callback())小部件上的.protocol("WM_DELETE_WINDOW", callback())

This would mean that whenever the Toplevel widget was "closed" we would actually be overwriting the action taken when the event was raised and would manually handle the closing of the widget as well as whatever else we wanted it to do. 这意味着只要“关闭” Toplevel窗口小部件,我们实际上就会覆盖引发事件时采取的操作,并且将手动处理窗口小部件的关闭以及我们希望它执行的其他任何操作。

This would look something like the below: 如下所示:

from tkinter import *

root = Tk()

button = Button(root, text="Ok", state="disabled")
button.pack()

top = Toplevel(root)

def close():
    top.destroy()
    button.configure(state="active")

top.protocol("WM_DELETE_WINDOW", close)

root.mainloop()

If you close the Toplevel widget you will see that the button is now active instead. 如果关闭“ Toplevel窗口小部件,您将看到该按钮现在active This would equally work if we added a Button to the Toplevel widget which called the function close() . 如果我们在Toplevel小部件中添加了一个名为close()函数的Button ,这同样可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM