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

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.

主站蜘蛛池模板: 肃北| 罗源县| 综艺| 长丰县| 汽车| 德兴市| 河池市| 林州市| 卫辉市| 万全县| 峨眉山市| 项城市| 扎兰屯市| 花莲市| 汝城县| 云和县| 浑源县| 南溪县| 本溪市| 惠水县| 广平县| 辽阳市| 石台县| 怀化市| 龙门县| 贞丰县| 班戈县| 高陵县| 隆德县| 宁武县| 沧源| 双桥区| 漳平市| 虎林市| 汉沽区| 秦安县| 长顺县| 扶沟县| 赤城县| 新和县| 攀枝花市|