如何修改Tkinter的CreateToolTip类实现可变wraplength?
自定义CreateToolTip类的wraplength参数实现方案
刚好我对这个经典的Tkinter ToolTip类很熟悉,要实现每次创建时自定义换行宽度,只需要修改几个关键地方,我给你拆解一下:
需要修改的核心部分
- 调整
__init__方法的参数顺序与默认值:把wraplength作为第二个参数加入,同时保留默认值180(这样不指定宽度的旧代码依然能正常运行),参数顺序改成widget, wraplength=180, text='widget info',完美匹配你想要的CreateToolTip(widget_name, tooltip_width, "提示内容")调用格式。 - 更新实例变量赋值:把原来固定写死的
self.wraplength = 180改成self.wraplength = wraplength,这样就会使用你传入的自定义宽度值。
修改后的完整代码
""" tk_ToolTip_class101.py gives a Tkinter widget a tooltip as the mouse is above the widget tested with Python27 and Python34 by vegaseat 09sep2014 Modified to include a delay time by Victor Zaccardo, 25mar16 Modified to support custom wraplength by adjusting __init__ parameters """ try: # for Python2 import Tkinter as tk except ImportError: # for Python3 import tkinter as tk class CreateToolTip(object): """ create a tooltip for a given widget """ def __init__(self, widget, wraplength=180, text='widget info'): self.waittime = 500 #miliseconds self.wraplength = wraplength # 使用传入的自定义宽度,默认180像素 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="#ffffff", relief='solid', borderwidth=1, wraplength = self.wraplength) label.pack(ipadx=1) def hidetip(self): tw = self.tw self.tw= None if tw: tw.destroy() # testing ... if __name__ == '__main__': root = tk.Tk() btn1 = tk.Button(root, text="button 1 (宽250像素)") btn1.pack(padx=10, pady=5) # 自定义wraplength为250像素 button1_ttp = CreateToolTip(btn1, 250, \ 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, ' 'consectetur, adipisci velit. Neque porro quisquam est qui dolorem ipsum ' 'quia dolor sit amet, consectetur, adipisci velit. Neque porro quisquam ' 'est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.') btn2 = tk.Button(root, text="button 2 (默认宽180像素)") btn2.pack(padx=10, pady=5) # 使用默认的180像素宽度 button2_ttp = CreateToolTip(btn2, text=\ "First thing's first, I'm the realest. Drop this and let the whole world " "feel it. And I'm still in the Murda Bizness. I could hold you down, like " "I'm givin' lessons in physics. You should want a bad Vic like this.") root.mainloop()
使用说明
- 自定义宽度:直接按
CreateToolTip(目标控件, 自定义宽度, "提示文本")的格式调用,比如CreateToolTip(btn1, 250, "这是宽250的提示") - 保留默认宽度:可以省略宽度参数,用关键字指定文本,比如
CreateToolTip(btn2, text="默认宽度的提示"),或者直接传180作为第二个参数。
内容的提问来源于stack exchange,提问作者user7722318
相关产品推荐
相关产品推荐

