官术网_书友最值得收藏!

Showing messages

Often, a GUI application will need to tell the user something. Using what we have learned at the moment, we could make several Label widgets which update depending on the results of some other functions. This would get tedious and take up a lot of space within the application's window.

A much better way to achieve this is to use a pop-up window. These can be created manually, but Tkinter also comes with a few pre-built pop-ups which are already laid out and ready to display any message the programmer passes to them.

Let's adjust our Hello World application to utilize these windows to display the chosen message to the user.

Import the messagebox module with the following statement:

import tkinter.messagebox as msgbox

Now update the non-init methods to utilize this module:

def say_hello(self):
msgbox.showinfo("Hello", "Hello World!")

def say_goodbye(self):
self.label_text.set("Window will close in 2 seconds")
msgbox.showinfo("Goodbye!", "Goodbye, it's been fun!")
self.after(2000, self.destroy)

Run this version of our application and try out both buttons.

主站蜘蛛池模板: 贵阳市| 开封市| 章丘市| 玉屏| 玛曲县| 龙山县| 抚州市| 商城县| 奉化市| 康马县| 施秉县| 邵武市| 报价| 永兴县| 沾化县| 赤壁市| 隆化县| 年辖:市辖区| 家居| 深圳市| 民权县| 隆子县| 阳高县| 漠河县| 饶平县| 麟游县| 咸丰县| 牟定县| 康保县| 绥棱县| 鹿邑县| 兰西县| 太湖县| 霍山县| 大埔县| 罗甸县| 镇巴县| 喀什市| 宕昌县| 丹巴县| 饶平县|