滑鼠懸停在 Tkinter Python 中某個內容上方時顯示訊息


假設我們想要建立一個應用程式,希望在其中為 Tkinter 小元件新增一些描述,以便在懸停在按鈕小元件上時顯示工具提示文字。可以透過新增工具提示或彈出框來實現。

工具提示對於需要使用者互動的應用程式很有用。我們可以透過例項化 Balloon(win) 的建構函式來定義工具提示。之後,我們可以將按鈕繫結到應用於該小元件的工具提示訊息。

示例

#Import the tkinter library
from tkinter import *
from tkinter.tix import *

#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("400x200")

#Create a tooltip
tip= Balloon(win)

#Create a Button widget
my_button=Button(win, text= "Python", font=('Helvetica bold', 20))
my_button.pack(pady=20)

#Bind the tooltip with button
tip.bind_widget(my_button,balloonmsg="Python is an interpreted, high-level
and general-purpose programming language")

win.mainloop()

輸出

執行上述程式碼將顯示一個帶按鈕的視窗。現在,將滑鼠懸停在 “Python” 按鈕上,它將顯示一個工具提示文字。

更新於: 2021-03-27

4K+ 次瀏覽

開啟您的 事業

完成課程獲得認證

開始
廣告