1414STATE_START = 1
1515STATE_END = 2
1616
17-
1817# GLOBALS
1918obstacles = []
2019current_state = None
@@ -45,10 +44,11 @@ def refresh_map():
4544
4645
4746def create_path ():
48- global CELL_SIZE , SIZE , start , end
47+ global CELL_SIZE , SIZE , current_state , start , end
4948
50- startButton .config (state = DISABLED )
51- customButton .config (state = DISABLED )
49+ current_state = None
50+ start_button .config (state = DISABLED )
51+ custom_button .config (state = DISABLED )
5252 refresh_map ()
5353
5454 if not start and not end :
@@ -81,8 +81,8 @@ def create_path():
8181 print (" length : no path" )
8282 percentage = 0
8383 print (" {0} cells analyzed ({1}%)\n " .format (len (debug ), percentage ))
84- startButton .config (state = NORMAL )
85- customButton .config (state = NORMAL )
84+ start_button .config (state = NORMAL )
85+ custom_button .config (state = NORMAL )
8686 start = None
8787 end = None
8888
@@ -91,18 +91,18 @@ def custom(state):
9191 global obstacles , current_state
9292
9393 if state == STATE_OBSTACLE :
94- startButton .config (state = DISABLED )
94+ start_button .config (state = DISABLED )
9595 current_state = STATE_OBSTACLE
96- customButton .config (text = "place start" , command = lambda :custom (STATE_START ))
96+ custom_button .config (text = "place start" , command = lambda :custom (STATE_START ))
9797 refresh_map ()
9898
9999 elif state == STATE_START :
100100 current_state = STATE_START
101- customButton .config (text = "place end" , command = lambda :custom (STATE_END ))
101+ custom_button .config (text = "place end" , command = lambda :custom (STATE_END ))
102102
103103 elif state == STATE_END :
104104 current_state = STATE_END
105- customButton .config (text = "place obstacles" , command = lambda :custom (STATE_OBSTACLE ))
105+ custom_button .config (text = "place obstacles" , command = lambda :custom (STATE_OBSTACLE ))
106106
107107
108108def click (event ):
@@ -126,11 +126,12 @@ def click(event):
126126 if (x , y ) not in obstacles and (x , y ) != start :
127127 end = (x , y )
128128 refresh_map ()
129- startButton .config (state = NORMAL )
129+ start_button .config (state = NORMAL )
130130
131131
132132def get_random_positions ():
133133 global start , end
134+
134135 start = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
135136 while start in obstacles :
136137 start = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
@@ -140,7 +141,14 @@ def get_random_positions():
140141
141142
142143root = Tk ()
143- grid = Canvas (root , width = SIZE * CELL_SIZE , height = SIZE * CELL_SIZE , bg = 'white' )
144+
145+ grid_frame = Frame (root )
146+ grid_frame .grid (row = 0 , column = 0 )
147+
148+ button_frame = Frame (root )
149+ button_frame .grid (row = 0 , column = 1 )
150+
151+ grid = Canvas (grid_frame , width = SIZE * CELL_SIZE , height = SIZE * CELL_SIZE , bg = 'white' )
144152grid .bind ("<Button-1>" , click )
145153grid .pack ()
146154
@@ -153,10 +161,10 @@ def get_random_positions():
153161 obstacles .append ((x , y ))
154162 grid .create_rectangle (x * CELL_SIZE , y * CELL_SIZE , (x + 1 )* CELL_SIZE , (y + 1 )* CELL_SIZE , fill = 'black' )
155163
156- startButton = Button (root , text = 'start' , command = create_path )
157- startButton .pack ()
164+ start_button = Button (button_frame , width = 15 , height = 5 , text = 'start' , command = create_path )
165+ start_button .pack ()
158166
159- customButton = Button (root , text = 'place obstacles' , command = lambda :custom (STATE_OBSTACLE ))
160- customButton .pack ()
167+ custom_button = Button (button_frame , width = 15 , height = 5 , text = 'place obstacles' , command = lambda :custom (STATE_OBSTACLE ))
168+ custom_button .pack ()
161169
162170root .mainloop ()
0 commit comments