@@ -58,83 +58,95 @@ def showAboutTurtle():
5858
5959class DemoWindow (object ):
6060
61- def __init__ (self , filename = None ): #, root=None):
61+ def __init__ (self , filename = None ):
6262 self .root = root = turtle ._root = Tk ()
63+ root .title ('Python turtle-graphics examples' )
6364 root .wm_protocol ("WM_DELETE_WINDOW" , self ._destroy )
6465
65- #################
66- self .mBar = Frame (root , relief = RAISED , borderwidth = 2 )
67- self .mBar .pack (fill = X )
66+ root .grid_rowconfigure (1 , weight = 1 )
67+ root .grid_columnconfigure (0 , weight = 1 )
68+ root .grid_columnconfigure (1 , minsize = 90 , weight = 1 )
69+ root .grid_columnconfigure (2 , minsize = 90 , weight = 1 )
70+ root .grid_columnconfigure (3 , minsize = 90 , weight = 1 )
6871
72+ self .mBar = Frame (root , relief = RAISED , borderwidth = 2 )
6973 self .ExamplesBtn = self .makeLoadDemoMenu ()
7074 self .OptionsBtn = self .makeHelpMenu ()
71- self .mBar .tk_menuBar (self .ExamplesBtn , self .OptionsBtn ) #, QuitBtn)
75+ self .mBar .tk_menuBar (self .ExamplesBtn , self .OptionsBtn )
76+ self .mBar .grid (row = 0 , columnspan = 4 , sticky = 'news' )
77+
78+ pane = PanedWindow (orient = HORIZONTAL , sashwidth = 5 ,
79+ sashrelief = SOLID , bg = '#ddd' )
80+ pane .add (self .makeTextFrame (pane ))
81+ pane .add (self .makeGraphFrame (pane ))
82+ pane .grid (row = 1 , columnspan = 4 , sticky = 'news' )
83+
84+ self .output_lbl = Label (root , height = 1 , text = " --- " , bg = "#ddf" ,
85+ font = ("Arial" , 16 , 'normal' ), borderwidth = 2 ,
86+ relief = RIDGE )
87+ self .start_btn = Button (root , text = " START " , font = btnfont ,
88+ fg = "white" , disabledforeground = "#fed" ,
89+ command = self .startDemo )
90+ self .stop_btn = Button (root , text = " STOP " , font = btnfont ,
91+ fg = "white" , disabledforeground = "#fed" ,
92+ command = self .stopIt )
93+ self .clear_btn = Button (root , text = " CLEAR " , font = btnfont ,
94+ fg = "white" , disabledforeground = "#fed" ,
95+ command = self .clearCanvas )
96+ self .output_lbl .grid (row = 2 , column = 0 , sticky = 'news' , padx = (0 ,5 ))
97+ self .start_btn .grid (row = 2 , column = 1 , sticky = 'ew' )
98+ self .stop_btn .grid (row = 2 , column = 2 , sticky = 'ew' )
99+ self .clear_btn .grid (row = 2 , column = 3 , sticky = 'ew' )
100+
101+ Percolator (self .text ).insertfilter (ColorDelegator ())
102+ self .dirty = False
103+ self .exitflag = False
104+ if filename :
105+ self .loadfile (filename )
106+ self .configGUI (NORMAL , DISABLED , DISABLED , DISABLED ,
107+ "Choose example from menu" , "black" )
108+ self .state = STARTUP
72109
73- root .title ('Python turtle-graphics examples' )
74- #################
75- self .left_frame = left_frame = Frame (root )
76- self .text_frame = text_frame = Frame (left_frame )
77- self .vbar = vbar = Scrollbar (text_frame , name = 'vbar' )
78- self .text = text = Text (text_frame ,
79- name = 'text' , padx = 5 , wrap = 'none' ,
80- width = 45 )
110+
111+ def onResize (self , event ):
112+ cwidth = self ._canvas .winfo_width ()
113+ cheight = self ._canvas .winfo_height ()
114+ self ._canvas .xview_moveto (0.5 * (self .canvwidth - cwidth )/ self .canvwidth )
115+ self ._canvas .yview_moveto (0.5 * (self .canvheight - cheight )/ self .canvheight )
116+
117+ def makeTextFrame (self , root ):
118+ self .text_frame = text_frame = Frame (root )
119+ self .text = text = Text (text_frame , name = 'text' , padx = 5 ,
120+ wrap = 'none' , width = 45 )
121+
122+ self .vbar = vbar = Scrollbar (text_frame , name = 'vbar' )
81123 vbar ['command' ] = text .yview
82124 vbar .pack (side = LEFT , fill = Y )
83- #####################
84- self .hbar = hbar = Scrollbar (text_frame , name = 'hbar' , orient = HORIZONTAL )
125+ self .hbar = hbar = Scrollbar (text_frame , name = 'hbar' , orient = HORIZONTAL )
85126 hbar ['command' ] = text .xview
86127 hbar .pack (side = BOTTOM , fill = X )
87- #####################
128+
129+ text ['font' ] = txtfont
88130 text ['yscrollcommand' ] = vbar .set
89- text .config (font = txtfont )
90- text .config (xscrollcommand = hbar .set )
91- text .pack (side = LEFT , fill = Y , expand = 1 )
92- #####################
93- self .output_lbl = Label (left_frame , height = 1 ,text = " --- " , bg = "#ddf" ,
94- font = ("Arial" , 16 , 'normal' ))
95- self .output_lbl .pack (side = BOTTOM , expand = 0 , fill = X )
96- #####################
97- text_frame .pack (side = LEFT , fill = BOTH , expand = 0 )
98- left_frame .pack (side = LEFT , fill = BOTH , expand = 0 )
99- self .graph_frame = g_frame = Frame (root )
100-
101- turtle ._Screen ._root = g_frame
102- turtle ._Screen ._canvas = turtle .ScrolledCanvas (g_frame , 800 , 600 , 1000 , 800 )
103- #xturtle.Screen._canvas.pack(expand=1, fill="both")
131+ text ['xscrollcommand' ] = hbar .set
132+ text .pack (side = LEFT , fill = BOTH , expand = 1 )
133+ return text_frame
134+
135+ def makeGraphFrame (self , root ):
136+ turtle ._Screen ._root = root
137+ self .canvwidth = 1000
138+ self .canvheight = 800
139+ turtle ._Screen ._canvas = self ._canvas = canvas = turtle .ScrolledCanvas (
140+ root , 800 , 600 , self .canvwidth , self .canvheight )
141+ canvas .adjustScrolls ()
142+ canvas ._rootwindow .bind ('<Configure>' , self .onResize )
143+ canvas ._canvas ['borderwidth' ] = 0
144+
104145 self .screen = _s_ = turtle .Screen ()
105146 turtle .TurtleScreen .__init__ (_s_ , _s_ ._canvas )
106147 self .scanvas = _s_ ._canvas
107- #xturtle.RawTurtle.canvases = [self.scanvas]
108148 turtle .RawTurtle .screens = [_s_ ]
109-
110- self .scanvas .pack (side = TOP , fill = BOTH , expand = 1 )
111-
112- self .btn_frame = btn_frame = Frame (g_frame , height = 100 )
113- self .start_btn = Button (btn_frame , text = " START " , font = btnfont , fg = "white" ,
114- disabledforeground = "#fed" , command = self .startDemo )
115- self .start_btn .pack (side = LEFT , fill = X , expand = 1 )
116- self .stop_btn = Button (btn_frame , text = " STOP " , font = btnfont , fg = "white" ,
117- disabledforeground = "#fed" , command = self .stopIt )
118- self .stop_btn .pack (side = LEFT , fill = X , expand = 1 )
119- self .clear_btn = Button (btn_frame , text = " CLEAR " , font = btnfont , fg = "white" ,
120- disabledforeground = "#fed" , command = self .clearCanvas )
121- self .clear_btn .pack (side = LEFT , fill = X , expand = 1 )
122-
123- self .btn_frame .pack (side = TOP , fill = BOTH , expand = 0 )
124- self .graph_frame .pack (side = TOP , fill = BOTH , expand = 1 )
125-
126- Percolator (text ).insertfilter (ColorDelegator ())
127- self .dirty = False
128- self .exitflag = False
129- if filename :
130- self .loadfile (filename )
131- self .configGUI (NORMAL , DISABLED , DISABLED , DISABLED ,
132- "Choose example from menu" , "black" )
133- self .state = STARTUP
134-
135- def _destroy (self ):
136- self .root .destroy ()
137- sys .exit ()
149+ return canvas
138150
139151 def configGUI (self , menu , start , stop , clear , txt = "" , color = "blue" ):
140152 self .ExamplesBtn .config (state = menu )
@@ -160,9 +172,9 @@ def configGUI(self, menu, start, stop, clear, txt="", color="blue"):
160172
161173 self .output_lbl .config (text = txt , fg = color )
162174
163-
164175 def makeLoadDemoMenu (self ):
165- CmdBtn = Menubutton (self .mBar , text = 'Examples' , underline = 0 , font = menufont )
176+ CmdBtn = Menubutton (self .mBar , text = 'Examples' ,
177+ underline = 0 , font = menufont )
166178 CmdBtn .pack (side = LEFT , padx = "2m" )
167179 CmdBtn .menu = Menu (CmdBtn )
168180
@@ -172,17 +184,18 @@ def emit():
172184 self .loadfile (x )
173185 return emit
174186 if isinstance (entry ,str ):
175- CmdBtn .menu .add_command (label = entry [6 :- 3 ], underline = 0 , font = menufont ,
187+ CmdBtn .menu .add_command (label = entry [6 :- 3 ], underline = 0 ,
188+ font = menufont ,
176189 command = loadexample (entry ))
177190 else :
178191 _dir , entries = entry [0 ], entry [1 :]
179192 CmdBtn .menu .choices = Menu (CmdBtn .menu )
180193 for e in entries :
181- CmdBtn .menu .choices .add_command (label = e [ 6 : - 3 ], underline = 0 , font = menufont ,
182- command = loadexample ( os . path . join ( _dir , e )))
183-
184- CmdBtn .menu .add_cascade (label = _dir [ 6 :],
185- menu = CmdBtn .menu .choices , font = menufont )
194+ CmdBtn .menu .choices .add_command (
195+ label = e [ 6 : - 3 ], underline = 0 , font = menufont ,
196+ command = loadexample ( os . path . join ( _dir , e )))
197+ CmdBtn .menu .add_cascade (
198+ label = _dir [ 6 :], menu = CmdBtn .menu .choices , font = menufont )
186199
187200 CmdBtn ['menu' ] = CmdBtn .menu
188201 return CmdBtn
@@ -193,17 +206,19 @@ def makeHelpMenu(self):
193206 CmdBtn .pack (side = LEFT , padx = '2m' )
194207 CmdBtn .menu = Menu (CmdBtn )
195208
196- CmdBtn .menu .add_command (label = 'About turtle.py' , font = menufont , command = showAboutTurtle )
197- CmdBtn .menu .add_command (label = 'turtleDemo - Help' , font = menufont , command = showDemoHelp )
198- CmdBtn .menu .add_command (label = 'About turtleDemo' , font = menufont , command = showAboutDemo )
209+ CmdBtn .menu .add_command (label = 'About turtle.py' , font = menufont ,
210+ command = showAboutTurtle )
211+ CmdBtn .menu .add_command (label = 'turtleDemo - Help' , font = menufont ,
212+ command = showDemoHelp )
213+ CmdBtn .menu .add_command (label = 'About turtleDemo' , font = menufont ,
214+ command = showAboutDemo )
199215
200216 CmdBtn ['menu' ] = CmdBtn .menu
201217 return CmdBtn
202218
203219 def refreshCanvas (self ):
204220 if not self .dirty : return
205221 self .screen .clear ()
206- #self.screen.mode("standard")
207222 self .dirty = False
208223
209224 def loadfile (self ,filename ):
@@ -262,10 +277,12 @@ def stopIt(self):
262277 self .configGUI (NORMAL , NORMAL , DISABLED , DISABLED ,
263278 "STOPPED!" , "red" )
264279 turtle .TurtleScreen ._RUNNING = False
265- #print "stopIT: exitflag = True"
266280 else :
267281 turtle .TurtleScreen ._RUNNING = False
268- #print "stopIt: exitflag = False"
282+
283+ def _destroy (self ):
284+ self .root .destroy ()
285+ sys .exit ()
269286
270287if __name__ == '__main__' :
271288 demo = DemoWindow ()
0 commit comments