Search code examples
python-3.xclasstkintertooltip

Python: call a class in another class


I am new to Python class and tkinter. I am trying to use python GUI tooltip class - Class CreateToolTip developed by @crxguy52 and implement to a GUI class. The GUI class is simple: Label, Entry and Button.

import tkinter as tk

class CreateToolTip(object):
    """
    create a tooltip for a given widget
    """
    def __init__(self, widget=None, text=None):
        self.waittime = 500
        self.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()

class GUI(tk.Frame):

    def __init__(self, master=None, Frame=None):
        tk.Frame.__init__(self, master)
        super(GUI,self).__init__()
        self.createWidgets()
        self.button_close = tk.Button(text='Close', command=self.quitButton)\
                .grid(row=9, column=0, sticky='W', padx=5, pady=5)
        self.button_send = tk.Button(text='Send', command=self.sendButton)\
                .grid(row=9, column=3, sticky='E', padx=5, pady=5)
    def quitButton(self):
        self.master.destroy()
        import sys
        sys.exit()

    def sendButton(self):
        global alt
        alt = self.alt.get()
        self.master.destroy()

    def createWidgets(self):
        import os
        # alt
        lbl = tk.Label(text='Start at ').grid(row=1, sticky='W')
        lbl_ttp = CreateToolTip(lbl,'hello world')
        self.alt = tk.Entry(width=12)
        self.alt.grid(row=1, column=1)
        self.alt.focus()
        self.alt.insert(0,'5000')


app = GUI()
app.mainloop()
print (alt)

To make the code tidy, I keep original Tooltip class and create a GUI. Then it runs into the error:

Traceback (most recent call last):
  File "D:/User/test3.py", line 88, in <module>
    app = GUI()
  File "D:/User/test3.py", line 62, in __init__
    self.createWidgets()
  File "D:/User/test3.py", line 81, in createWidgets
    lbl_ttp = CreateToolTip(lbl,'hello world')
  File "D:/User/test3.py", line 13, in __init__
    self.widget.bind("<Enter>", self.enter)
AttributeError: 'NoneType' object has no attribute 'bind'

It causes by the bind in the __init__ of class CreateToolTip. How should I change the statement: lbl_ttp = CreateToolTip(lbl,'hello world') to make it works? Thank you all.


Solution

  • The bug is on the "tk.Label(text='Start at ').grid(row=1, sticky='W')" line.

    The grid method of a Label/Button etc. object returns None,so the "master" in your __init__ is also None,and that is why the bind call failes.

    You can fix it by changing the lines to

        lbl = tk.Label(text='Start at ')
        lbl.grid(row=1, sticky='W')
        lbl_ttp = CreateToolTip(lbl,'hello world')