Python get_tk_widget()框架周围的边框
我在tkinter中嵌入了两个matplotlib绘图的画布小部件。我想使用Python get_tk_widget()框架周围的边框,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我在tkinter中嵌入了两个matplotlib绘图的画布小部件。我想使用fig1\u canvas.get\u tk\u widget().configure(highlightthickness=3)和 fig2\u canvas.get\u tk\u widget().configure(highlightthickness=3) 然而,这只适用于其中一种 我该如何解决 将tkinter作为tk导入 将matplotlib.pyplot作为plt导入 从matplotlib.backe
fig1\u canvas.get\u tk\u widget().configure(highlightthickness=3)
和
fig2\u canvas.get\u tk\u widget().configure(highlightthickness=3)
然而,这只适用于其中一种
我该如何解决
将tkinter作为tk导入
将matplotlib.pyplot作为plt导入
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG
将numpy作为np导入
root=tk.tk()
图1,ax1=plt.子批次(图大小=(2,2))
图2,ax2=plt.子批次(图大小=(2,2))
t=np.arange(0,2*np.pi,0.1)
ax1.绘图(t,np.cos(t))
图1.紧凑的布局图()
图1_canvas=FigureCanvasTkAgg(图1,master=root)
图1_canvas.get_tk_widget().configure(highlightthickness=3)
ax2.绘图(t,np.sin(t))
图2.紧凑型布局图()
图2_canvas=FigureCanvasTkAgg(图2,master=root)
图2_canvas.get_tk_widget().configure(highlightthickness=3)
frame1=tk.Frame()
frame2=tk.Frame()
标签(frame1,text='hello').pack()
标签(frame2,text='world').pack()
frame1.grid(行=0,列=0,行跨度=2)
图1_canvas.get_tk_widget().grid(行=0,列=1)
图2_canvas.get_tk_widget().grid(行=1,列=1)
frame2.grid(行=2,列=0,列span=2)
tk.mainloop()
如果您想在图形周围放置一个不同颜色的平面边框,您可以将它们嵌入到所需颜色的框架中,使用padx
和pady
选项留出空白:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
root = tk.Tk()
# frames to create the black border:
fig_frame1 = tk.Frame(root, background='black', padx=2, pady=2)
fig_frame2 = tk.Frame(root, background='black', padx=2, pady=2)
fig1, ax1 = plt.subplots(figsize=(2, 2))
fig2, ax2 = plt.subplots(figsize=(2, 2))
t = np.arange(0, 2*np.pi, 0.1)
ax1.plot(t, np.cos(t))
fig1.tight_layout()
fig1_canvas = FigureCanvasTkAgg(fig1, master=fig_frame1) # set master of fig1_canvas to the border frame
fig1_canvas.get_tk_widget().pack(padx=1, pady=1) # change padx and pady to choose the thickness of the border
ax2.plot(t, np.sin(t))
fig2.tight_layout()
fig2_canvas = FigureCanvasTkAgg(fig2, master=fig_frame2)
fig2_canvas.get_tk_widget().pack(padx=1, pady=1)
frame1 = tk.Frame()
frame2 = tk.Frame()
tk.Label(frame1, text='hello').pack()
tk.Label(frame2, text='world').pack()
frame1.grid(row=0, column=0, rowspan=2)
# put the border frames in the root window
fig_frame1.grid(row=0, column=1)
fig_frame2.grid(row=1, column=1)
frame2.grid(row=2, column=0, columnspan=2)
一次只能突出显示一个元素。如果你按TAB键,它会突出显示另一个。啊,好的(我想这也在名称中),那么我如何在它们周围加上边框呢?