【问题标题】:How do I configure a MatPlotLib Toolbar so that the buttons in the toolbars are ttk buttons instead of old Tk buttons如何配置 MatPlotLib 工具栏,以便工具栏中的按钮是 ttk 按钮而不是旧的 Tk 按钮
【发布时间】:2020-04-23 16:11:17
【问题描述】:

我一直在考虑在我的 tkinter GUI 应用程序中嵌入 matplotlib 图形,但是在使用工具栏时,我不喜欢它的外观。谁能告诉我如何制作自己的按钮,制作 ttk 按钮,并将它们放在工具栏中?

【问题讨论】:

  • 我曾经创建NavigationToolbar2Tk的子类,然后对相关方法/参数一一修改。虽然很痛苦。
  • 你会怎么做?我对如何更改按钮的参数感到困惑,因为工具栏在 matplotlib 模块中是硬编码的
  • 这正是您需要对其进行子类化的原因——覆盖您需要自己修改的部分。我会给你一个小样本。

标签: python windows matplotlib tkinter


【解决方案1】:

您可以覆盖NavigationToolbar2Tk 中的方法并为图标提供您自己的图像,如下所示:

from tkinter import *
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk, FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.backends._backend_tk import ToolTip

root = Tk()

class Navigator(NavigationToolbar2Tk):
    """
    Customized Navigator object
    - Removed mouse_move event.x and event.y display
    - Changed buttons layout
    """
    def mouse_move(self,event):
        pass

    def _Button(self, text, file, command, extension='.gif'):
        im = PhotoImage(master=self, file=file)
        b = Button(master=self, text=text, padx=2,image=im, command=command,bg="DeepSkyBlue4",relief="flat")
        b._ntimage = im
        b.pack(side=LEFT)
        self.tool_buttons.append(b)
        return b

    def _init_toolbar(self):
        self.tool_buttons = []
        self.toolbar_icons = ["icons/home.png", #provide paths of your own icons
                              "icons/backward.png",
                              "icons/forward.png",
                              None,
                              "icons/pan.png",
                              "icons/zoom.png",
                              "icons/config.png",
                              None,
                              "icons/save.png"]
        xmin, xmax = self.canvas.figure.bbox.intervalx
        height, width = 50, xmax-xmin
        Frame.__init__(self, master=self.window,
                          width=500, height=int(height))
        self.update()
        num = 0
        for text, tooltip_text, image_file, callback in self.toolitems:
            if text is None:
                self._Spacer()
            else:
                try:
                    button = self._Button(text=text, file=self.toolbar_icons[num],
                                          command=getattr(self, callback))
                    if tooltip_text is not None:
                        ToolTip.createToolTip(button, tooltip_text)
                except IndexError:
                    pass
            num+=1
        self.pack(side=BOTTOM, fill=X)

    def destroy(self, *args):
        Frame.destroy(self)


f = Figure()

canvas = FigureCanvasTkAgg(f, root)
canvas.get_tk_widget().pack(fill=BOTH, expand=True)

toolbar = NavigationToolbar2Tk(canvas, root)

custom_toolbar  = Navigator(canvas, root)
custom_toolbar.config(background="DeepSkyBlue4")
root.mainloop()

结果(顶部自定义工具栏,底部原始工具栏):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 2011-07-15
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2016-07-16
    相关资源
    最近更新 更多