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

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.

主站蜘蛛池模板: 秭归县| 五大连池市| 廉江市| 阿克苏市| 泸西县| 页游| 秦皇岛市| 乌兰浩特市| 贵州省| 筠连县| 措勤县| 鸡西市| 永济市| 阳泉市| 土默特左旗| 墨竹工卡县| 达日县| 右玉县| 荆州市| 广宗县| 鹤峰县| 桦川县| 景德镇市| 无棣县| 鄂托克前旗| 凤庆县| 会昌县| 怀柔区| 德州市| 青河县| 宜城市| 贵港市| 饶阳县| 乐昌市| 宁武县| 兴和县| 云和县| 青冈县| 烟台市| 桑植县| 洛隆县|