【问题标题】:How to remove toolbar button from NavigationToolbar2Tk/FigureCanvasTkAgg如何从 NavigationToolbar2Tk/FigureCanvasTkAgg 中删除工具栏按钮
【发布时间】:2020-03-28 01:36:00
【问题描述】:

我正在使用带有 tk/tkinter 的 matplotlib,遵循“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()

根据Tool Manager

self._figure.canvas.manager.toolmanager.remove_tool("pan")

应该可以,但是不行。

# self._figure.canvas does not have attribute 'manager'

从 vs-code 的屏幕截图中可以看出,FigureCanvasTkAgg 没有属性“manager”:

如何去掉工具栏按钮?

【问题讨论】:

  • 如果manager 应该在self._figure.canvas 中,为什么错误说self._figure 没有manager?也许你在self._figure.canvas.manager 中忘记了canvas?
  • 也许可以试试self._canvas.manager.toolmanager.remove_tool("pan")
  • 感谢 cmets,但这只是问题中的一个小错误,我更正了 q 并添加了来自调试器的屏幕截图
  • toolbar.children['!button4'].pack_forget() - 编辑:从'!button5' 更正到'!button4'

标签: python python-3.x matplotlib tkinter


【解决方案1】:

似乎tkinter 中嵌入的Canvas 和NavigationToolbar2Tk 具有不同的结构和不同的功能。标准matplotlib 窗口使用Qt 作为后端。


toolbar = NavigationToolbar2Tk(self._canvas, frame)

列出有关按钮的信息

print(toolbar.toolitems)    

要删除 Pan 按钮 - 这是第 4 个按钮

toolbar.children['!button4'].pack_forget()

将新功能分配给现有按钮 - 即。 Home

def my_function():
    print("Pressed Home")

toolbar.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 = [t for t in NavigationToolbar2Tk.toolitems if
             t[0] not in ('Pan',)]

完整的工作示例

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.

【讨论】:

  • 与其搞乱 GUI 本身,不如在 stackoverflow.com/questions/55779944/… 或 stackoverflow.com/questions/12695678/… 上查看现有答案
  • @ImportanceOfBeingErnest 感谢您提供链接。我怀疑更改toolitems 可能会更改按钮,但我懒得用代码或搜索链接检查它。只是我的混乱写起来更短,测试起来也更快;)
  • 我添加了完整的工作示例以及建议 @ImportanceOfBeingErnest 的解决方案
猜你喜欢
  • 2019-09-10
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2017-05-06
  • 2017-03-08
  • 1970-01-01
  • 2011-07-28
相关资源
最近更新 更多