如何删除Tkinter画布中matplotlib图周围的灰色边框

时间:2015-04-18 03:15:34

标签: python python-2.7 matplotlib tkinter tkinter-canvas

如何删除每个画布周围出现的灰色边框?我已经将facecolor设置为黑色,我认为应该照顾它。

import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, cos, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.configure(background='black')
x = Figure(figsize=(3,3), dpi=100, facecolor='black')

b = x.add_subplot(111,  axisbg='#1a1a1a')
c = arange(0.0,3.0,0.01)
s = cos(2*pi*c)
b.plot(c,s)

f = Figure(figsize=(5,3), dpi=100, facecolor='black')
a = f.add_subplot(111,  axisbg='#1a1a1a')
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a.plot(t,s)


canvas = FigureCanvasTkAgg(f, master=root)
canvas1 = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().grid(row=0,column=0)
canvas1.show()
canvas1.get_tk_widget().grid(row=1,column=0)

Tk.mainloop()

1 个答案:

答案 0 :(得分:2)

您看到的画布周围的灰色边框是FigureCanvasTkAgg小部件的背景。因此,除了更改根和图背景的背景之外,您还必须更改画布背景。

要更改FigureCanvasTkAgg的背景,您必须获取基础Tk画布窗口小部件并进行适当配置。记录了所需的方法here

因此,您必须调整您的代码,如

...
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().configure(background='black',  highlightcolor='black', highlightbackground='black')
canvas1 = FigureCanvasTkAgg(f, master=root)
canvas1.get_tk_widget().configure(background='black', highlightcolor='black', highlightbackground='black')
canvas.show()
canvas.get_tk_widget().grid(row=0,column=0)
canvas1.show()
canvas1.get_tk_widget().grid(row=1,column=0)
...

更新

如果仍有一个小的灰色边框,这可能是由于其中一幅画布的当前焦点。在我的测试中,上部画布仍然有一个小的灰色边框,表示另一个画布处于焦点上(用黑色边框突出显示)。因此,为了摆脱这个额外的边框,必须改变未聚焦画布的高亮颜色。这可以通过两个参数highlightbackgroundhighlightcolor来实现。我已更新上面发布的代码以解决此问题。有了它,窗口就像我的机器一样 enter image description here