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
25 default_gui_size = (200, 100)
27 class top_block_gui(gr.top_block):
28 """gr top block with wx gui app and grid sizer."""
30 def __init__(self, title='', size=default_gui_size, icon=None):
32 Initialize the gr top block.
33 Create the wx gui elements.
34 @param title the main window title
35 @param size the main window size tuple in pixels
36 @param icon the file path to an icon or None
39 gr.top_block.__init__(self)
42 if icon and os.path.isfile(icon): self._icon = icon
43 else: self._icon = None
45 self._wx_app = wx.App()
46 self._wx_frame = wx.Frame(None , -1, title)
47 self._wx_grid = wx.GridBagSizer(5, 5)
48 self._wx_vbox = wx.BoxSizer(wx.VERTICAL)
52 Get the window for wx elements to fit within.
59 Add a window to the wx vbox.
60 @param win the wx window
62 self._wx_vbox.Add(win, 0, wx.EXPAND)
64 def GridAdd(self, win, row, col, row_span=1, col_span=1):
66 Add a window to the wx grid at the given position.
67 @param win the wx window
68 @param row the row specification (integer >= 0)
69 @param col the column specification (integer >= 0)
70 @param row_span the row span specification (integer >= 1)
71 @param col_span the column span specification (integer >= 1)
73 self._wx_grid.Add(win, wx.GBPosition(row, col), wx.GBSpan(row_span, col_span), wx.EXPAND)
75 def start(self, start=True):
77 gr.top_block.start(self)
79 gr.top_block.stop(self)
80 gr.top_block.wait(self)
82 def Run(self, autostart=True):
84 Setup the wx gui elements.
85 Start the gr top block.
86 Block with the wx main loop.
89 if self._icon: self._wx_frame.SetIcon(wx.Icon(self._icon, wx.BITMAP_TYPE_ANY))
90 #set minimal window size
91 self._wx_frame.SetSizeHints(*self._size)
92 #create callback for quit
94 gr.top_block.stop(self)
95 self._wx_frame.Destroy()
97 self._wx_vbox.Add(self._wx_grid, 0, wx.EXPAND)
98 self._wx_frame.Bind(wx.EVT_CLOSE, _quit)
99 self._wx_frame.SetSizerAndFit(self._wx_vbox)
100 self._wx_frame.Show()
101 self._wx_app.SetTopWindow(self._wx_frame)
103 self.start(autostart)
105 self._wx_app.MainLoop()