如何使用导航栏排列框架的布局

时间:2014-07-11 13:00:09

标签: python layout matplotlib tkinter

我想要的只是简单但让我疯狂。出于某种原因,我无法为未附加到导航工具栏的按钮和树结构创建框架。我所拥有的是:

enter image description here

我想要的是

enter image description here

以下是代码段:

import os, ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

from matplotlib.figure import Figure
from Tkinter import * 
import Tkinter, tkFileDialog, tkMessageBox
from numpy import arange, sin, pi, genfromtxt 

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

root = Tk.Tk()
root.wm_title("Particle Counter")
fig= Figure(figsize = (10,7), dpi =100) # window size
fig.suptitle('Nanobiz Particle Counter', fontsize = 14, fontweight = 'bold')

ax1=fig.add_subplot(211) # figure adds this. later figure is added to canvas.
ax1.set_title('0.5u Particle',fontsize = 10)
ax1.set_xlabel('Time',fontsize = 10)
ax1.set_ylabel('Particle Number',fontsize = 10)   
ax1.set_ylim([0,6000])
ax1.xaxis.set_label_coords(1,-0.09)
ax1.plot(array1)

ax2=fig.add_subplot(212)
ax2.set_title('5u Particle',fontsize = 10)
ax2.set_xlabel('Time',fontsize = 10)
ax2.xaxis.set_label_coords(1,-0.09)
ax2.set_ylim([0,6000])
ax2.set_ylabel('Particle Number',fontsize = 10)
ax2.plot(array2)
ax2.grid()

mainFrame = Frame(root)
mainFrame.pack()

canvas = FigureCanvasTkAgg(fig, mainFrame)


#canvas.mpl_connect('key_press_event', on_key_event)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
toolbar.pack()
canvas.show()
canvas._tkcanvas.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)
canvas.get_tk_widget().pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)

menu = Menu(root)
root.config(menu=menu)
aboutMenu = Menu(menu)
menu.add_cascade(label="About",menu=aboutMenu, command = about_)
#aboutMenu.pack(side=Tk.RIGHT)


buttonFrame = Frame(root)
buttonFrame.pack(side = Tk.BOTTOM)


closeButton = Tk.Button(buttonFrame, text='Close', width = 6, command=_quit)
closeButton.pack(side= RIGHT)

由于

1 个答案:

答案 0 :(得分:2)

canvas中有mainFrame但你NavigationToolbar添加root

使用mainFrame代替

中的root
toolbar = NavigationToolbar2TkAgg(canvas, mainFrame)

您可以在Close

下面找到NavigationToolbar按钮

NavigationToolbar以上canvas使用

toolbar.pack(side=Tk.TOP)

编辑:与树一样 - 您是否需要在NavigationToolbarbuttonFrameNavigationToolbar以下buttonFrame以上的左侧?< / p>

至于我 - 使用.grid(row=...,column=...)然后使用.pack()

更容易做到这一切