“”我正在从“ GUI编程指南”中学习Tkinter。使用运行Mojave,python3.8和包装的Tkinter的MacBook Pro。有一种方法可以将鼠标悬停在鼠标上时显示弹出式工具提示。小部件。在Win 10和linux上工作正常,但弹出窗口未出现在我的Mac上。没有错误。
我放入了一些debug print()语句,并且鼠标坐标和移动正确。
这有效****。窗口文本悬停在小部件上]
这里是Linux上print(sys.version)的输出。
3.7.4(默认值,2019年7月9日,16:48:28)[GCC 8.3.1 20190223(Red Hat 8.3.1-2)]
此操作无效****弹出窗口不出现
在Mac上为print(sys.version)的输出
3.8.0(v3.8.0:fa919fdf25,2019年10月14日,10:23:27)[Clang 6.0(clang-600.0.57)]
'''
Created on May 3, 2019
@author: Burkhard A. Meier
'''
#======================
# imports
#======================
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import messagebox as msg
from tkinter import Spinbox
#====================================================
class ToolTip(object):
def __init__(self, widget, tip_text=None):
self.widget = widget
self.tip_text = tip_text
widget.bind('<Enter>', self.mouse_enter)
# bind mouse events
widget.bind('<Leave>', self.mouse_leave)
def mouse_enter(self, _event):
self.show_tooltip()
print('entered the widget')
def mouse_leave(self, _event):
self.hide_tooltip()
print('entered the widget')
def show_tooltip(self):
if self.tip_text:
x_left = self.widget.winfo_rootx()
# get widget top-left coordinates
y_top = self.widget.winfo_rooty() - 18
#DEBUG
print('self.widget, y_top, x_left')
print(self.widget, y_top, x_left)
# place tooltip above widget or it flickers
self.tip_window = tk.Toplevel(self.widget)
# create Toplevel window; parent=widget
self.tip_window.overrideredirect(True)
# remove surrounding toolbar window
self.tip_window.geometry("+%d+%d" % (x_left, y_top))
# position tooltip
#print('\nTOOLTIP before author code line')
#print(self.tip_text)
#label = tk.Label(self.tip_window, text=self.tip_text,
#justify=tk.LEFT, background="#ffffe0",
# relief=tk.SOLID,
#borderwidth=1, font=("tahoma", "8", "normal"))
#below is my code cus the above line does
#not show up EKK 10/25/19
label = tk.Label(self.tip_window, text=self.tip_text,
justify=tk.LEFT)
label.pack(fill="both")
#label.pack(ipadx=1)
print('TOOLTIP after author code line')
print(self.tip_text)
def hide_tooltip(self):
if self.tip_window:
self.tip_window.destroy()
#=====================================================
# just added all the code.
# the above class is called by the Spinbox and Scroll methods below.
# Create instance
win = tk.Tk()
# Add a title
win.title("Python GUI")
tabControl = ttk.Notebook(win) # Create Tab Control
tab1 = ttk.Frame(tabControl) # Create a tab
tabControl.add(tab1, text='Tab 1') # Add the tab
tab2 = ttk.Frame(tabControl) # Add a second tab
tabControl.add(tab2, text='Tab 2') # Make second tab visible
tabControl.pack(expand=1, fill="both") # Pack to make visible
# LabelFrame using tab1 as the parent
mighty = ttk.LabelFrame(tab1, text=' Mighty Python ')
mighty.grid(column=0, row=0, padx=8, pady=4)
# Modify adding a Label using mighty as the parent instead of win
a_label = ttk.Label(mighty, text="Enter a name:")
a_label.grid(column=0, row=0, sticky='W')
# Modified Button Click Function
def click_me():
action.configure(text='Hello ' + name.get() + ' ' +
number_chosen.get())
# Adding a Textbox Entry widget
name = tk.StringVar()
name_entered = ttk.Entry(mighty, width=12, textvariable=name)
name_entered.grid(column=0, row=1, sticky='W')
# align left/West
# Adding a Button
action = ttk.Button(mighty, text="Click Me!", command=click_me)
action.grid(column=2, row=1)
ttk.Label(mighty, text="Choose a number:").grid(column=1, row=0)
number = tk.StringVar()
number_chosen = ttk.Combobox(mighty, width=12, textvariable=number, state='readonly')
number_chosen['values'] = (1, 2, 4, 42, 100)
number_chosen.grid(column=1, row=1)
number_chosen.current(0)
# Spinbox callback
def _spin():
value = spin.get()
print(value)
scrol.insert(tk.INSERT, value + '\n')
# Adding a Spinbox widget
spin = Spinbox(mighty, values=(1, 2, 4, 42, 100), width=5, bd=9, command=_spin)
# using range
spin.grid(column=0, row=2)
# Add a Tooltip to the Spinbox
ToolTip(spin, 'This is a Spin control')
# Using a scrolled Text control
scrol_w = 30
scrol_h = 3
scrol = scrolledtext.ScrolledText(mighty, width=scrol_w, height=scrol_h, wrap=tk.WORD)
scrol.grid(column=0, row=3, sticky='WE', columnspan=3)
# Add a Tooltip to the ScrolledText widget
ToolTip(scrol, 'This is a ScrolledText widget')
# Bunch of code snipped here
name_entered.focus() # Place cursor into name Entry
#======================
# Start GUI
#======================
win.mainloop()
该类是从其他代码中调用的。第42和46行上的标签不会在我的MacBook上显示,但在Windows和Linux上一切正常。没有错误消息。窗口永远不会出现。有人有建议吗?我要疯了!提前致谢。
“”我正在从“ GUI编程指南”中学习Tkinter。使用运行Mojave,python3.8和打包的Tkinter的MacBook Pro。有一种方法可以在... ...时显示弹出工具提示。
[如果您在Mac OSX上仍然有此问题,我发现了一个指向[d]的reddit thread:
https://github.com/python/cpython/blob/master/Lib/idlelib/tooltip.py