如何从 NavigationToolbar2Tk/FigureCanvasTkAgg 中删除工具栏按钮 [英] How to remove toolbar button from NavigationToolbar2Tk/FigureCanvasTkAgg
问题描述
我将matplotlib与tk/tkinter一起使用,遵循"
如何删除工具栏按钮?
嵌入在 tkinter
中的 Canvas
和 NavigationToolbar2Tk
似乎具有不同的结构,并且不同的功能.标准matplotlib
窗口使用Qt
作为后端.
toolbar = NavigationToolbar2Tk(self._canvas, frame)
列出按钮信息
print(toolbar.toolitems)
要删除 Pan
按钮-这是第四个按钮
toolbar.children['!button4'].pack_forget()
要将新功能分配给现有按钮-即首页
def my_function():打印(按下回家")工具栏.children ['!button1'].config(command = my_function)
添加新按钮
button = tkinter.Button(master=toolbar, text="Quit", command=_quit)#button.pack()button.pack(side="left")
以相同的方式添加其他 tkinter
小部件- Label
, Entry
等
正如评论中提到的ImportanceOfBeingErnest一样,它可以做得更优雅.
NavigationToolbar2Tk.toolitems = [如果在NavigationToolbar2Tk.toolitems中为t,则为tt[0] 不在 ('Pan',)]
<小时>
完整的工作示例
导入tkinter从 matplotlib.backends.backend_tkagg 导入(FigureCanvasTkAgg, NavigationToolbar2Tk)# 实现默认的 Matplotlib 键绑定.从 matplotlib.backend_bases 导入 key_press_handlerfrom matplotlib.figure 导入图将numpy导入为np根 = tkinter.Tk()root.wm_title(嵌入Tk")无花果=图(figsize =(5,4),dpi = 100)t = np.arange(0, 3, .01)fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))canvas = FigureCanvasTkAgg(fig, master=root) # 一个 tk.DrawingArea.canvas.draw()canvas.get_tk_widget().pack(side = tkinter.TOP,fill = tkinter.BOTH,expand = 1)# ---NavigationToolbar2Tk.toolitems = [t for t in NavigationToolbar2Tk.toolitems ift [0]不在('Pan',)]中# ---工具栏= NavigationToolbar2Tk(画布,根目录)工具栏.update()canvas.get_tk_widget().pack(side = tkinter.TOP,fill = tkinter.BOTH,expand = 1)打印(工具栏.toolitems)def on_key_press(事件):print(您按下了{}".format(event.key))key_press_handler(事件,画布,工具栏)canvas.mpl_connect("key_press_event", on_key_press)定义_退出():root.quit()#停止mainlooproot.destroy() # 这在 Windows 上是必要的,以防止# 致命的 Python 错误:PyEval_RestoreThread: NULL tstatebutton = tkinter.Button(master = root,text ="Quit",command = _quit)button.pack(side=tkinter.BOTTOM)tkinter.mainloop()# 如果你把root.destroy()放在这里,如果窗口是会导致错误#用窗口管理器关闭.
I am using matplotlib with tk/tkinter, following the "Embedding in Tk"
self._figure = Figure()
self._axes: matplotlib.axes.Axes = self._figure.add_subplot(111)
self._canvas = FigureCanvasTkAgg(self._figure, master=frame)
toolbar = NavigationToolbar2Tk(self._canvas, frame)
toolbar.update()
According to Tool Manager
self._figure.canvas.manager.toolmanager.remove_tool("pan")
should work, but it does not.
# self._figure.canvas does not have attribute 'manager'
As one can see in the screenshot from vs-code, FigureCanvasTkAgg does not have attribute 'manager':
How to remove the toolbar button?
It seems Canvas
and NavigationToolbar2Tk
embedded in tkinter
have different constructions and differen functions. Standard matplotlib
window uses Qt
as backend.
toolbar = NavigationToolbar2Tk(self._canvas, frame)
To list information about buttons
print(toolbar.toolitems)
To remove Pan
button - it is 4th button
toolbar.children['!button4'].pack_forget()
To assign new function to existing button - ie. Home
def my_function():
print("Pressed Home")
toolbar.children['!button1'].config(command=my_function)
To add new button
button = tkinter.Button(master=toolbar, text="Quit", command=_quit)
#button.pack()
button.pack(side="left")
The same way you could add other tkinter
widgets - Label
, Entry
, etc.
EDIT: as ImportanceOfBeingErnest mentioned in comment it can be done more elegant way.
NavigationToolbar2Tk.toolitems = [t for t in NavigationToolbar2Tk.toolitems if
t[0] not in ('Pan',)]
Full working example
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
# ---
NavigationToolbar2Tk.toolitems = [t for t in NavigationToolbar2Tk.toolitems if
t[0] not in ('Pan',)]
# ---
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
print(toolbar.toolitems)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect("key_press_event", on_key_press)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)
tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.
这篇关于如何从 NavigationToolbar2Tk/FigureCanvasTkAgg 中删除工具栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!