1 # Copyright 2008 Free Software Foundation, Inc.
3 # This file is part of GNU Radio
5 # GNU Radio is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3, or (at your option)
10 # GNU Radio is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with GNU Radio; see the file COPYING. If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street,
18 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr
24 from gnuradio.gr.pubsub import pubsub
26 default_gui_size = (200, 100)
28 class top_block_gui(gr.top_block, pubsub):
29 """gr top block with wx gui app and grid sizer."""
31 def __init__(self, title='', size=default_gui_size, icon=None):
33 Initialize the gr top block.
34 Create the wx gui elements.
35 @param title the main window title
36 @param size the main window size tuple in pixels
37 @param icon the file path to an icon or None
40 gr.top_block.__init__(self)
44 if icon and os.path.isfile(icon): self._icon = icon
45 else: self._icon = None
47 self._wx_app = wx.App()
48 self._wx_frame = wx.Frame(None , -1, title)
49 self._wx_grid = wx.GridBagSizer(5, 5)
50 self._wx_vbox = wx.BoxSizer(wx.VERTICAL)
54 Get the window for wx elements to fit within.
61 Add a window to the wx vbox.
62 @param win the wx window
64 self._wx_vbox.Add(win, 0, wx.EXPAND)
66 def GridAdd(self, win, row, col, row_span=1, col_span=1):
68 Add a window to the wx grid at the given position.
69 @param win the wx window
70 @param row the row specification (integer >= 0)
71 @param col the column specification (integer >= 0)
72 @param row_span the row span specification (integer >= 1)
73 @param col_span the column span specification (integer >= 1)
75 self._wx_grid.Add(win, wx.GBPosition(row, col), wx.GBSpan(row_span, col_span), wx.EXPAND)
79 Setup the wx gui elements.
80 Start the gr top block.
81 Block with the wx main loop.
84 if self._icon: self._wx_frame.SetIcon(wx.Icon(self._icon, wx.BITMAP_TYPE_ANY))
85 #set minimal window size
86 self._wx_frame.SetSizeHints(*self._size)
87 #create callback for quit
89 gr.top_block.stop(self)
90 self._wx_frame.Destroy()
92 self._wx_vbox.Add(self._wx_grid, 0, wx.EXPAND)
93 self._wx_frame.Bind(wx.EVT_CLOSE, _quit)
94 self._wx_frame.SetSizerAndFit(self._wx_vbox)
96 self._wx_app.SetTopWindow(self._wx_frame)
98 gr.top_block.start(self)
100 self._wx_app.MainLoop()