用Tkinter打造GUI开发工具(17)tix.Balloon气球窗口小部件

用Tkinter打造GUI开发工具(17)tix.Balloon气球窗口小部件
Tix.Balloon气球窗口小部件一个部件弹出提供帮助。当用户将光标移动到已绑定了气球窗口小部件的窗口小部件中时,屏幕上将显示一个带有描述性消息的小型弹出式窗口。
使用tix.Balloon气球窗口小部件的构造语法如下:

balloon = tix.Balloon (master, statusbar=None)

参数master 这代表了父部件。statusbar是这个部件的绑定的状态条部件。
tix.Balloon件除了上面的属性外,还有一些方法可以使用。
1)绑定小部件。

balloon .bind_widget(widget, balloonmsg=’’, statusmsg=None)

参数widget是要绑定的小部件,也就是说鼠标移动到小部件上方会出现需要提示信息。
参数balloonmsg是需要提示的信息。
参数statusmsg是需要在状态栏显示的信息。

2)解绑小部件。

balloon .unbind_widget(widget)

参数widget是要解绑的小部件。

下面看一个tix.Balloon气球窗口小部件的演示程序。

# -*- coding: utf-8 -*-
# https://blog.csdn.net/hepu8
# 独狼荷蒲 QQ:2775205

import tkinter.tix as Tix   #导入Tkinter.tix
from tkinter.constants import *

TCL_ALL_EVENTS		= 0

def RunSample (root):
    balloon = DemoBalloon(root)
    balloon.mainloop()
    balloon.destroy()

class DemoBalloon:
    def __init__(self, w):
        self.root = w
        self.exit = -1

        z = w.winfo_toplevel()
        z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
        z.title('Tix.Balloon演示')  #Tkinter中设置窗口标题方法

        status = Tix.Label(w, width=40, relief=Tix.SUNKEN, bd=1)
        status.pack(side=Tix.BOTTOM, fill=Tix.Y, padx=2, pady=1)

        # 建立2个 tix按钮
        button1 = Tix.Button(w, text='关闭窗口',
                             command=self.quitcmd)
        button2 = Tix.Button(w, text='按钮自毁')
        button2['command'] = lambda w=button2: w.destroy()
        button1.pack(side=Tix.TOP, expand=1)
        button2.pack(side=Tix.TOP, expand=1)

        # 建立 tixballoon
        b = Tix.Balloon(w, statusbar=status)

        b.bind_widget(button1, balloonmsg='关闭这个窗口',
                      statusmsg='按下这个按钮,关闭窗口。')
        
        b.bind_widget(button2, balloonmsg='删除这个按钮',
                      statusmsg='按下这个按钮,删除这个按钮。')

    def quitcmd (self):
        self.exit = 0

    def mainloop(self):
        foundEvent = 1
        while self.exit < 0 and foundEvent > 0:
            foundEvent = self.root.tk.dooneevent(TCL_ALL_EVENTS)

    def destroy (self):
        self.root.destroy()

if __name__ == '__main__':
    root = Tix.Tk()
    RunSample(root)

程序运行结果如下图.
在这里插入图片描述
我们这节介绍了Tkinter扩展小部件Tix的tix.Balloon气球窗口小部件使用。Tix的小部件有40多个,极大丰富了Tkinter的开发。

发布了56 篇原创文章 · 获赞 67 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/hepu8/article/details/89964749