1+ from tkinter import *
2+ from Array .widget import *
3+ import time
4+
5+ # Globals
6+ canvas = None
7+ inpUpdateElement = None
8+ inpElement = None
9+ inpDelIndex = None
10+ inpUpdateIndex = None
11+ error = None
12+ program = None
13+ window = None
14+
15+ rect_empty_box = []
16+ rect_data = []
17+ txt_index = []
18+ txt_data = []
19+
20+ index = - 1
21+ arr_elements = []
22+
23+ x1 = 100
24+ y1 = 80
25+
26+ arr_x1 = []
27+
28+ def initDynamicArray ():
29+ global canvas ,inpUpdateElement ,inpElement ,inpDelIndex ,inpUpdateIndex ,error ,program ,window
30+
31+ window = Tk ()
32+ window .title ("Visual Static Array" )
33+ w , h = window .winfo_screenwidth (), window .winfo_screenheight () - 20
34+ window .geometry ("%dx%d+0+0" % (w , h ))
35+
36+ try :
37+ window .state ("zoomed" )
38+ except TclError :
39+ print ("Linux Error" )
40+
41+ lbl_heading (window , "Dynamic Array : " )
42+ lbl_text (window , "A dynamic array has memory allocated at run time." )
43+ lbl_text (window , "Run time is when the program is actually running." )
44+ lbl_text (window , "The elements of the dynamic array are stored contiguously at the start of the underlying array," )
45+ lbl_text (window , "and the remaining positions towards the end of the underlying array are reserved, or unused." )
46+
47+ #----- Frame 1 Start -----#
48+ frame1 = Frame (window )
49+
50+ #------- Section InsertAtEnd -------#
51+ entry_text (frame1 , "• Element :" )
52+ inpElement = entry (frame1 , 10 )
53+ btn_in_frame (frame1 , "Insert" ,insert )
54+
55+ #------- Section Update ------------#
56+ entry_text (frame1 , "Index :" )
57+ inpUpdateIndex = entry (frame1 , 10 )
58+ entry_text (frame1 , "Element :" )
59+ inpUpdateElement = entry (frame1 , 10 )
60+ btn_in_frame (frame1 , "Update" ,update )
61+
62+ entry_text (frame1 , " | " )
63+
64+ #------ Section Delete --------#
65+ entry_text (frame1 , "Index :" )
66+ inpDelIndex = entry (frame1 , 10 )
67+ btn_in_frame (frame1 , "Delete" ,delete )
68+
69+ entry_text (frame1 , " | " )
70+
71+ btn_in_frame (frame1 , "Reset" ,reset )
72+
73+ frame1 .pack (anchor = W , pady = 15 )
74+ #----- Frame 1 End -------#
75+
76+ error = lbl_error (window , "" )
77+ program = Label (window , text = "• Program : " ,font = ("Courier New" , 10 , 'bold' ))
78+ program .pack (anchor = W , padx = 15 , pady = 10 )
79+
80+ #----- Canvas Start ------#
81+ canvas = Canvas (window , width = 500 , height = 800 , bg = "#FFF" )
82+ canvas .pack (fill = BOTH , expand = True , anchor = W , padx = 15 , pady = 15 )
83+
84+ scrollbar = Scrollbar (canvas , orient = "horizontal" , command = canvas .xview )
85+ scrollbar .pack (side = BOTTOM , fill = X )
86+ canvas .configure (xscrollcommand = scrollbar .set )
87+
88+ #----- Canvas End -------#
89+ window .protocol ("WM_DELETE_WINDOW" , on_closing )
90+ window .mainloop ()
91+
92+ def element (text ,index ):
93+ global rect_data ,rect_empty_box ,txt_data ,txt_index
94+
95+ id_rect_empty_box = canvas .create_rectangle (x1 , y1 , x1 + 90 , y1 + 80 , width = 2 )
96+ id_txt_index = canvas .create_text (x1 + 40 , y1 + 100 , text = str (index ))
97+ id_rect_data = canvas .create_rectangle (x1 + 10 , y1 + 10 , x1 + 80 , y1 + 70 , fill = "#ffd28c" )
98+ id_txt_data = canvas .create_text (x1 + 45 , y1 + 40 , text = str (text ))
99+
100+ rect_empty_box .append (id_rect_empty_box )
101+ rect_data .append (id_rect_data )
102+ txt_index .append (id_txt_index )
103+ txt_data .append (id_txt_data )
104+
105+ return id_rect_data , id_txt_data , id_rect_empty_box , id_txt_index
106+
107+ def incX1 ():
108+ global x1
109+ x1 += 90
110+
111+ def incIndex ():
112+ global index
113+ index += 1
114+
115+ def getDelIndex ():
116+ try :
117+ inp = int (inpDelIndex .get ())
118+ return str (inp )
119+ except ValueError :
120+ return False
121+
122+ def getInsertText ():
123+ try :
124+ inp = str (int (inpElement .get ()))
125+ if len (inp ) > 4 :
126+ inp = inp [0 :4 ]
127+ inp += ".."
128+ return inp
129+ else :
130+ return inp
131+ except ValueError :
132+ return False
133+
134+ def getUpdateText ():
135+ try :
136+ inp = str (int (inpUpdateElement .get ()))
137+ if len (inp ) > 4 :
138+ inp = inp [0 :4 ]
139+ inp += ".."
140+ return inp
141+ else :
142+ return inp
143+ except ValueError :
144+ return False
145+
146+ def insert ():
147+ updateProgram ("" )
148+ if getInsertText ():
149+ setError ("" )
150+ incIndex ()
151+ updateProgram ("array[" + str (index )+ "] = " + str (inpElement .get ())+ ";" )
152+ element (getInsertText (),index )
153+ arr_elements .append (getInsertText ())
154+ arr_x1 .append (x1 )
155+ incX1 ()
156+ canvas .configure (scrollregion = canvas .bbox ("all" ))
157+ else :
158+ setError ("Only integer value is accepted." )
159+
160+ def getUpdateIndex ():
161+ try :
162+ inp = int (inpUpdateIndex .get ())
163+ return str (inp )
164+ except ValueError :
165+ return False
166+
167+ def update ():
168+ updateProgram ("" )
169+ if getUpdateIndex ():
170+ setError ("" )
171+ if getUpdateText ():
172+ try :
173+ i = int (getUpdateIndex ())
174+ txt = getUpdateText ()
175+ updateProgram ("array[" + str (i )+ "] = " + str (inpUpdateElement .get ())+ ";" )
176+ arr_elements [i ] = txt
177+ canvas .itemconfig (txt_data [i ], text = str (txt ))
178+ except IndexError :
179+ if index == - 1 :
180+ setError ("Array is empty." )
181+ updateProgram ("Java Exception : IndexOutOfBoundsException | Python Exception : IndexError" )
182+ else :
183+ setError ("Enter value between 0 to " + str (index )+ "." )
184+ updateProgram ("Java Exception : IndexOutOfBoundsException | Python Exception : IndexError" )
185+ else :
186+ setError ("Element value must be Numeric." )
187+ else :
188+ setError ("Index must be Numeric." )
189+
190+ def setError (err ):
191+ global error
192+ error .config (text = str ("• Error : " + err ))
193+
194+ def reCreateElement (i ):
195+ try :
196+ element = str (arr_elements [i ])
197+ x = arr_x1 [i ]
198+ id_rect_data = canvas .create_rectangle (x + 10 , y1 + 10 , x + 80 , y1 + 70 , fill = "#ffd28c" )
199+ id_txt_data = canvas .create_text (x + 45 , y1 + 40 , text = element )
200+
201+ rect_data [i ] = id_rect_data
202+ txt_data [i ] = id_txt_data
203+ except IndexError :
204+ arr_x1 .pop (i )
205+
206+ def delete ():
207+ setError ("" )
208+ updateProgram ("" )
209+ if getDelIndex ():
210+ inpIndex = int (getDelIndex ())
211+ if index == - 1 :
212+ setError ("Array is empty." )
213+ updateProgram ("Java Exception : IndexOutOfBoundsException | Python Exception : IndexError" )
214+ elif inpIndex < 0 or inpIndex > index :
215+ try :
216+ data = rect_empty_box [inpIndex ]
217+ updateProgram ("array[" + str (inpIndex )+ "] = null; srinkArray();" )
218+ except IndexError :
219+ setError ("Enter value between 0 to " + str (index )+ "." )
220+ updateProgram ("Java Exception : IndexOutOfBoundsException | Python Exception : IndexError" )
221+ else :
222+ arr_elements .pop (inpIndex )
223+
224+ updateProgram ("array[" + str (inpIndex )+ "] = null; srinkArray();" )
225+
226+ for i in range (inpIndex ,index + 1 ):
227+ deleteDataBox (i )
228+ reCreateElement (i )
229+
230+ rect_data .pop (i )
231+ txt_data .pop (i )
232+
233+ decX1 ()
234+ decIndex ()
235+
236+ else :
237+ setError ("Index must be Numeric." )
238+
239+ def deleteDataBox (i ):
240+ canvas .delete (txt_data [i ])
241+ canvas .delete (rect_data [i ])
242+
243+ def decX1 ():
244+ global x1
245+ x1 -= 90
246+
247+ def decIndex ():
248+ global index
249+ index -= 1
250+
251+ def updateProgram (data ):
252+ program .config (text = "• Program : " + str (data ))
253+
254+ def reset ():
255+ window .destroy ()
256+ resetValues ()
257+ initDynamicArray ()
258+
259+ def resetValues ():
260+ g = globals ()
261+ g ['canvas' ] = None
262+ g ['inpUpdateElement' ] = None
263+ g ['inpElement' ] = None
264+ g ['inpDelIndex' ] = None
265+ g ['inpUpdateIndex' ] = None
266+ g ['error' ] = None
267+ g ['program' ] = None
268+ g ['window' ] = None
269+
270+ g ['rect_empty_box' ] = []
271+ g ['rect_data' ] = []
272+ g ['txt_index' ] = []
273+ g ['txt_data' ] = []
274+
275+ g ['index' ] = - 1
276+ g ['arr_elements' ] = []
277+
278+ g ['x1' ] = 100
279+ g ['y1' ] = 80
280+
281+ g ['arr_x1' ] = []
282+
283+ def on_closing ():
284+ window .destroy ()
285+ resetValues ()
0 commit comments