I have a tkinter window
that displays a tooltip, when i am over a button with the mouse. Below is the code for that:
import tkinter as tk
from tkmacosx import Button
class ToolTip(object):
def __init__(self, widget, text, x, y, dx, dy):
self.widget = widget
self.text = text
self.x = x #root window coordinates
self.y = y #root window coordinates
self.dx = dx #differnce to root window
self.dy = dy #differnce to root window
def enter(event):
self.showTooltip()
def leave(event):
self.hideTooltip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
widget.bind("<ButtonRelease-1>", leave)
def showTooltip(self):
self.tooltipwindow = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(1) # window without border and no normal means of closing
tw.wm_geometry("+{}+{}".format(self.widget.winfo_rootx(), self.widget.winfo_rooty()))
label = tk.Label(tw, text = self.text, background = "#ffffe0", relief = 'solid', borderwidth = 1)
label.pack()
tw.geometry("+%d+%d" % (self.x + self.dx, self.y + self.dy))
def hideTooltip(self):
try:
tw = self.tooltipwindow
tw.destroy()
self.tooltipwindow = None
except:
pass
root = tk.Tk()
root.title('Start')
button = Button(root, text="Quit", command=root.quit)
button.pack()
ToolTip(widget=button, text="Quits programm", x=root.winfo_x(), y=root.winfo_y(), dx=80, dy=55)
root.mainloop()
As long as I run the whole thing in the Pycharm environment
it works fine. But when I convert the code to a standalone program using py2app
, the tooltip is no longer displayed. Below is the code for the setup
file:
from setuptools import setup
APP = ['test.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)