Logo ROOT   6.10/09
Reference Guide
gui_ex.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_pyroot
3 ## A Simple GUI Example
4 ##
5 ## \macro_code
6 ##
7 ## \author Wim Lavrijsen
8 
9 import os, sys, ROOT
10 
11 def pygaus( x, par ):
12  import math
13  if (par[2] != 0.0):
14  arg1 = (x[0]-par[1])/par[2]
15  arg2 = (0.01*0.39894228)/par[2]
16  arg3 = par[0]/(1+par[3])
17 
18  gauss = arg3*arg2*math.exp(-0.5*arg1*arg1)
19  else:
20  print 'returning 0'
21  gauss = 0.
22  return gauss
23 
24 tpygaus = ROOT.TF1( 'pygaus', pygaus, -4, 4, 4 )
25 tpygaus.SetParameters( 1., 0., 1. )
26 
27 def MyDraw():
28  btn = ROOT.BindObject( ROOT.gTQSender, ROOT.TGTextButton )
29  if btn.WidgetId() == 10:
30  global tpygaus, window
31  tpygaus.Draw()
32  ROOT.gPad.Update()
33 
34 m = ROOT.TPyDispatcher( MyDraw )
35 
36 
37 class pMainFrame( ROOT.TGMainFrame ):
38  def __init__( self, parent, width, height ):
39  ROOT.TGMainFrame.__init__( self, parent, width, height )
40 
41  self.Canvas = ROOT.TRootEmbeddedCanvas( 'Canvas', self, 200, 200 )
42  self.AddFrame( self.Canvas, ROOT.TGLayoutHints() )
43  self.ButtonsFrame = ROOT.TGHorizontalFrame( self, 200, 40 )
44 
45  self.DrawButton = ROOT.TGTextButton( self.ButtonsFrame, '&Draw', 10 )
46  self.DrawButton.Connect( 'Clicked()', "TPyDispatcher", m, 'Dispatch()' )
47  self.ButtonsFrame.AddFrame( self.DrawButton, ROOT.TGLayoutHints() )
48 
49  self.ExitButton = ROOT.TGTextButton( self.ButtonsFrame, '&Exit', 20 )
50  self.ExitButton.SetCommand( 'TPython::Exec( "raise SystemExit" )' )
51  self.ButtonsFrame.AddFrame( self.ExitButton, ROOT.TGLayoutHints() )
52 
53  self.AddFrame( self.ButtonsFrame, ROOT.TGLayoutHints() )
54 
55  self.SetWindowName( 'My first GUI' )
56  self.MapSubwindows()
57  self.Resize( self.GetDefaultSize() )
58  self.MapWindow()
59 
60  def __del__(self):
61  self.Cleanup()
62 
63 
64 if __name__ == '__main__':
65  window = pMainFrame( ROOT.gClient.GetRoot(), 200, 200 )