我正在创建一个 GUI,在这个 GUI 中我有一个嵌入在窗格窗口中的笔记本。 在这个笔记本中,我创建了 2 个选项卡,并为每个选项卡创建了一个带有工具栏的 matplotlib 图形。 但是,对于第二个选项卡,工具栏位于窗格窗口之外,我需要调整它的大小才能看到工具栏。相比之下,当我调整第一个选项卡的大小时,无论窗格窗口的大小如何,我总是拥有图形及其工具栏。
我是 tkinter 和 GUI 创建方面的初学者,我非常感谢您的帮助!
这是我创建 GUI 的类:
os.chdir(absolute_path+toolbox_path_graph)
from graph_window import graph_window
os.chdir(absolute_path)
class Project(tk.Tk):
def __init__(self):
super().__init__()
#Set-up Window
self.title("Title")
self.state('zoomed')
#Default dimensions of the paned windows
self.dim_paned_windows=({"height":700},{"width":600})
#Create the paned windows
self.paned_windows()
self.graph_window_instance=graph_window(np ,tk, ttk, self.graph)
def paned_windows(self):
#1st Paned windows
panel_1=tk.PanedWindow(bd=2, relief="groove", bg="gray")
panel_1.pack(fill=tk.BOTH, expand=1)
#1st window
self.paned_window1=tk.Label(panel_1,takefocus=0,highlightthickness=0)
panel_1.add(self.paned_window1, width=600)
#2nd Paned window
self.panel_2=tk.PanedWindow(panel_1,bd=0, orient=tk.VERTICAL,relief="groove", bg="gray")
panel_1.add(self.panel_2)
#Graph window
self.graph=tk.Canvas(self.panel_2, bg="white", borderwidth=0, highlightthickness=0, takefocus=0)
self.panel_2.add(self.graph,height=700)
#3rd window
self.paned_window3=tk.Label(self.panel_2)
self.panel_2.add(self.paned_window3)
if __name__ == "__main__":
app = Project()
app.mainloop()
这是我创建图形的课程:
class graph_window:
def __init__(self, np, tk, ttk, master_frame): #master_frame=paned window
#Add modules
self.np=np
self.tk=tk
self.ttk=ttk
#Define list for different graph
self.graph=[]
self.fig=[]
self.ax=[]
self.toolbar=[]
#Main
self.main(master_frame)
def main(self, master_frame):
#Create the tabs
self.tab1, self.tab2=self.setup_tab(master_frame)
self.setup_graph(self.tab2)
self.setup_graph(self.tab1)
def setup_tab(self, master_frame):
#Define style to position the tab at the bottom left
style=self.ttk.Style(master_frame)
style.configure('lefttab.TNotebook', tabposition='sw')
#Creation of the tab control
tabControl=self.ttk.Notebook(master_frame, style='lefttab.TNotebook')
tabControl.pack(expand=1, fill=self.tk.BOTH)
#Creation of the tab
tab1=self.ttk.Frame(tabControl)
tab2=self.ttk.Frame(tabControl)
#Set the name of the tab
tabControl.add(tab1, text='tab1')
tabControl.add(tab2, text='tab2')
return tab1, tab2
def setup_graph(self, tab_frame):
fig, ax=plt.subplots()
self.fig.append(fig)
self.ax.append(ax)
#Create Matplotlib graph
self.graph.append(FigureCanvasTkAgg(self.fig[-1], master=tab_frame))
self.graph[-1].get_tk_widget().pack(expand=True, fill=self.tk.BOTH)
#Create toolbar
self.toolbar.append(NavigationToolbar2Tk(self.graph[-1], tab_frame, pack_toolbar=False))
self.toolbar[-1].update()
self.toolbar[-1].pack()
#Define axis system and title
self.ax[-1].set_xlabel('a')
self.ax[-1].set_ylabel('b')
self.ax[-1].set_title('title')
#Define default scale
self.ax[-1].set_xlim(0,160)
self.ax[-1].set_ylim(0,100)
这是调整窗格窗口大小后获得的结果: 如果窗口足够大,工具栏只会出现在 tab2 中,这与 tab1 的行为不同。
我尝试切换到网格方法,但没有获得与 tab1 相同的结果。