Tooltip in Tkinter || How to make Balloon in tkinter || Python
Source code 👉🏻
➖ ➖ ➖ ➖
tooltip.py
import tkinter as tk class CreateToolTip(object): """ create a tooltip for a given widget """ def __init__(self, widget, text='widget info'): self.waittime = 500 #miliseconds self.wraplength = 180 #pixels self.widget = widget self.text = text self.widget.bind("<Enter>", self.enter) self.widget.bind("<Leave>", self.leave) self.widget.bind("<ButtonPress>", self.leave) self.id = None self.tw = None def enter(self, event=None): self.schedule() def leave(self, event=None): self.unschedule() self.hidetip() def schedule(self): self.unschedule() self.id = self.widget.after(self.waittime, self.showtip) def unschedule(self): id = self.id self.id = None if id: self.widget.after_cancel(id) def showtip(self, event=None): x = y = 0 x, y, cx, cy = self.widget.bbox("insert") x += self.widget.winfo_rootx() + 25 y += self.widget.winfo_rooty() + 20 # creates a toplevel window self.tw = tk.Toplevel(self.widget) # Leaves only the label and removes the app window self.tw.wm_overrideredirect(True) self.tw.wm_geometry("+%d+%d" % (x, y)) label = tk.Label(self.tw, text=self.text, justify='left', background="sky blue", relief='solid', borderwidth=1, wraplength = self.wraplength) label.pack(ipadx=1) def hidetip(self): tw = self.tw self.tw= None if tw: tw.destroy()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from tkinter import * from tkinter.tix import * from tkinter.ttk import Combobox from tooltips import CreateToolTip root = Tk() root.title("Tooltip Example2") root.state('zoomed') # A Label here name = Label(root,text="Dynamic Balloon / tooltip Example ",font=20).pack(fill=X) # An entry box here entry1 = Entry(root,width=30) entry1.pack(fill=X) # A Combo box here combo1 = Combobox(root,values=[1,2,3,4,5],width=30) combo1.pack() # A button here btn1 = Button(root,text="Click me!",font=15,fg='white',bg='green',width=15) btn1.pack() # an instance of balloon b1 = Balloon(root) # bind any widget to balloon with custom msg b1.bind_widget(combo1,balloonmsg='Select any of the option') CreateToolTip(btn1,'Click to Submit!') root.mainloop() |
Comments
Post a Comment