-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewqueueworking.py
More file actions
225 lines (151 loc) · 6.02 KB
/
newqueueworking.py
File metadata and controls
225 lines (151 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from tkinter import *
from time import sleep
from tkinter import messagebox
class Queue(Frame):
def __init__(self,master):
self.queue=list()
self.rect=list()
self.text=list()
self.color=["light green","green","yellow","orange","red"]
self.limit=5
self.front=None
self.rear=None
super().__init__(master)
self.pack()
self.CreateWidget()
def isfull(self):
return len(self.queue) == self.limit
def isempty(self):
return len(self.queue) == 0
def enqueuerect(self,rect,text,val):
if self.enqueue(val):
self.rect.append(rect)
self.text.append(text)
def enqueue(self,val):
if self.isfull():
messagebox.showwarning("Warning","Queue Overflow")
return False
else:
if self.front==None and self.rear==None:
self.front=self.rear=0
else:
self.rear+=1
self.queue.append(val)
return True
def dequeuerect(self):
if self.dequeue():
self.rect.pop(0)
self.text.pop(0)
def dequeue(self):
if self.isempty():
messagebox.showwarning("Warning","Queue Underflow")
return False
else:
if self.front == self.rear :
self.front=self.rear=None
else:
self.front+=1
self.queue.pop(0)
return True
def display(self):
if self.isempty():
messagebox.showwarning("Warning","Queue Underflow")
else:
print(self.queue)
def CreateWidget(self):
Label(self,text="Working of Queue",font=("Times",20,"bold")).pack()
self.topframe=Frame(self,bg="Light Green",height=150,width=500)
self.topframe.pack(side=TOP)
self.bottomframe=Frame(self,height=350,width=500)
self.bottomframe.pack(side=BOTTOM,fill=BOTH,expand=1)
self.create=Button(self.topframe,text="Create Queue",command=self.createqueue)
self.create.grid(row=0,column=0,sticky=NW)
self.push=Label(self.topframe,text="Enqueue")
self.push.grid(row=1,column=0,sticky=NE)
self.entry1=Entry(self.topframe,width=8)
self.entry1.grid(row=1,column=1)
self.accept=Button(self.topframe,text="accept",command=self.pushvalue)
self.accept.grid(row=1,column=3)
self.pop = Button(self.topframe,text="dequeue",command=self.popvalue)
self.pop.grid(row=2,column=0)
self.display=Button(self.topframe,text="Display",command=self.display)
self.display.grid(row=2,column=5)
self.c = Canvas(self.bottomframe,height=350,width=400,bg="pink")
self.c.pack()
def createqueue(self):
self.create.config(text="Delete Queue",command=self.deletequeue)
self.c.create_line(50,100,300,100)
self.c.create_line(50,250,300,250)
self.x1,self.y1,self.x2,self.y2 = 50,100,100,250
self.c_index=-1
def deletequeue(self):
self.c.delete("all")
self.create.config(text="Create Queue",command=self.createqueue)
self.queue=[]
def switchAllButtons(self):
self.switchAcceptButton()
self.switchPopButton()
def switchAcceptButton(self):
if self.accept["state"] == "normal":
self.accept["state"] = "disabled"
else:
self.accept["state"] = "normal"
def switchPopButton(self):
if self.pop["state"] == "normal":
self.pop["state"] = "disabled"
else:
self.pop["state"] = "normal"
def pushvalue(self):
self.switchAllButtons()
val=self.entry1.get()
if val== "":
return
elif not self.isfull():
if self.c_index == len(self.color)-1 :
self.c_index=0
else:
self.c_index+=1
rect=self.c.create_rectangle(400,100,450,250,fill=self.color[self.c_index])
text=self.c.create_text(430,150,text=val,justify=CENTER,font=("Times",20,'bold'))
self.enqueuerect(rect,text,val)
self.moveelements()
self.x1,self.x2 = self.x1+50,self.x2+50 #changing the coordinates
else:
messagebox.showwarning("Warning","Queue OverFlow")
self.entry1.delete(0,END)
self.switchAllButtons()
def popvalue(self):
self.switchAllButtons()
self.x1,self.x2 = self.x1-50,self.x2-50
xend=0
for i in range(len(self.rect)):
while True :
self.c.move(self.rect[i],-5,0)
self.c.move(self.text[i],-5,0)
pos=self.c.coords(self.rect[i])
sleep(0.02)
self.c.update()
if pos[2]== xend :
# the second block need to be moved to the first position
if i!=0 :
xend+=50
else:
xend+=100
sleep(0.1)
break
self.dequeuerect()
self.switchAllButtons()
def moveelements(self):
while True :
self.c.move(self.rect[len(self.queue)-1],-5,0)
self.c.move(self.text[len(self.queue)-1],-5,0)
pos=self.c.coords(self.rect[len(self.queue)-1])
sleep(0.005)
self.c.update()
if pos[0]==self.x1:
break
root=Tk()
root.title("Working of Queue")
root.geometry("500x500")
app=Queue(root)
root.mainloop()