- Tkinter GUI Programming by Example
- David Love
- 283字
- 2021-08-27 18:49:09
Getting feedback from the user
Should you require something back from the user, Tkinter has four more message boxes for you:
- askquestion
- askyesno
- askokcancel
- askretrycancel
askquestion will allow any question to be passed in and provides Yes and No answers. These are returned to the program as the string literals "yes" and "no".
askyesno does the same, but will return 1 on Yes and nothing on No.
askokcancel provides OK and Cancel buttons to the user. OK returns 1 and Cancel nothing.
askretrycancel provides Retry and Cancel buttons. Retry returns 1 and Cancel nothing.
Despite the seemingly large number of choices, these all do pretty much the same thing. There doesn't seem to be much of a use case for askquestion over askyesno since they provide the same button choices, but askquestion will produce cleaner code thanks to the return values.
Let's see askyesno in action within our Hello World application.
Change the say_goodbye method to the following:
def say_goodbye(self):
if msgbox.askyesno("Close Window?", "Would you like to
close this window?"):
self.label_text.set("Window will close in 2 seconds")
self.after(2000, self.destroy)
else:
msgbox.showinfo("Not Closing", "Great! This window
will stay open.")
Run this application and try clicking the Say Goodbye button. You will now be asked whether you want to close the window. Give both No and Yes a try:

From the code for this function, you should see that the askyesno method can be treated like a Boolean statement. If you don't like doing this in one go, you could always use a variable such as the following:
close = msgbox.askyesno("Close Window?", "Would you like to close this window?")
if close:
self.close()
- JavaScript+DHTML語法與范例詳解詞典
- Java游戲服務器架構實戰
- 數據結構(Python語言描述)(第2版)
- Python GUI Programming Cookbook
- Web程序設計(第二版)
- Python編程與幾何圖形
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- ExtJS高級程序設計
- 新一代SDN:VMware NSX 網絡原理與實踐
- C++反匯編與逆向分析技術揭秘(第2版)
- Serverless Web Applications with React and Firebase
- Java 9 Programming By Example
- Vue.js 3應用開發與核心源碼解析
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- Python物理建模初學者指南(第2版)