Advertisement
gregwa

QueueDemo.py

Sep 6th, 2011
555
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import sys
  3. from sqlite3 import *
  4. import datetime
  5. from Tkinter import *
  6. import ttk
  7. import tkMessageBox
  8. import Queue
  9.    
  10. class QueueTest:
  11.     def __init__(self,master = None):
  12.         self.DefineVars()
  13.         f = self.BuildWidgets(master)
  14.         self.PlaceWidgets(f)
  15.         self.ShowStatus()
  16.         self.LastQueue = 'fifo'
  17.    
  18.     def DefineVars(self):
  19.         self.QueueType = ''
  20.         self.LastQueue = ''
  21.         self.FullStatus = StringVar()
  22.         self.EmptyStatus = StringVar()
  23.         self.Item = StringVar()
  24.         self.Output = StringVar()
  25.         # Define the queues
  26.         self.fifo = Queue.Queue(10)
  27.         self.lifo = Queue.LifoQueue(10)
  28.         self.pq = Queue.PriorityQueue(10)
  29.         self.obj = self.fifo
  30.        
  31.     def BuildWidgets(self,master):
  32.         # Define our widgets
  33.         frame = Frame(master)
  34.         self.f1 = Frame(frame,
  35.             relief = SUNKEN,
  36.             borderwidth=2,
  37.             width = 300,
  38.             padx = 3,
  39.             pady = 3
  40.         )
  41.         self.btnFifo = Button(self.f1,
  42.             text = "FIFO"
  43.         )    
  44.         #
  45.         self.btnFifo.bind('<Button-1>',
  46.             lambda e: self.btnMain(1)
  47.         )        
  48.         self.btnLifo = Button(self.f1,
  49.             text = "LIFO"
  50.         )    
  51.         self.btnLifo.bind('<ButtonRelease-1>',
  52.             lambda e: self.btnMain(2)
  53.         )
  54.         self.btnPriority = Button(self.f1,
  55.             text = "PRIORITY"
  56.         )    
  57.         self.btnPriority.bind('<ButtonRelease-1>',
  58.             lambda e: self.btnMain(3)
  59.         )
  60.         self.f2 = Frame(frame,
  61.             relief = SUNKEN,
  62.             borderwidth=2,
  63.             width = 300,
  64.             padx = 3,
  65.             pady = 3
  66.         )
  67.         self.txtAdd = Entry(self.f2,
  68.             width=5,
  69.             textvar=self.Item
  70.         )
  71.         self.txtAdd.bind('<Return>',self.AddToQueue)
  72.         self.btnAdd = Button(self.f2,
  73.             text='Add to Queue',
  74.             padx = 3,
  75.             pady = 3
  76.         )
  77.         self.btnAdd.bind('<ButtonRelease-1>',self.AddToQueue)
  78.         self.btnGet = Button(self.f2,
  79.             text='Get Next Item',
  80.             padx = 3,
  81.             pady = 3
  82.         )
  83.         self.btnGet.bind('<ButtonRelease-1>',self.GetFromQueue)
  84.         self.lblEmpty = Label(self.f2,
  85.             textvariable=self.EmptyStatus,
  86.             relief=FLAT
  87.         )
  88.         self.lblFull = Label(self.f2,
  89.             textvariable=self.FullStatus,
  90.             relief=FLAT
  91.         )
  92.         self.lblData = Label(self.f2,
  93.             textvariable=self.Output,
  94.             relief = FLAT,
  95.             font=("Helvetica", 16),
  96.             padx = 5
  97.         )
  98.  
  99.         return frame
  100.        
  101.     def PlaceWidgets(self, master):
  102.         frame = master
  103.         # Place the widgets
  104.         frame.grid(column = 0, row = 0)
  105.         l = Label(frame,text='',relief=FLAT,width = 15, anchor = 'e').grid(column = 0, row = 0)
  106.         l = Label(frame,text='',relief=FLAT,width = 15, anchor = 'e').grid(column = 1, row = 0)
  107.         l = Label(frame,text='',relief=FLAT,width = 15, anchor = 'e').grid(column = 2, row = 0)
  108.         l = Label(frame,text='',relief=FLAT,width = 15, anchor = 'e').grid(column = 3, row = 0)
  109.         l = Label(frame,text='',relief=FLAT,width = 15, anchor = 'e').grid(column = 4, row = 0)
  110.                                        
  111.         self.f1.grid(column = 0,row = 1,sticky='nsew',columnspan=5,padx = 5,pady = 5)
  112.         l = Label(self.f1,text='',width = 25,anchor = 'e').grid(column = 0, row = 0)
  113.         self.btnFifo.grid(column = 1,row = 0,padx = 4)
  114.         self.btnLifo.grid(column = 2,row = 0,padx = 4)
  115.         self.btnPriority.grid(column = 3, row = 0, padx = 4)
  116.         self.f2.grid(column = 0,row = 2,sticky='nsew',columnspan=5,padx = 5, pady = 5)
  117.         l = Label(self.f2,text='',width = 15,anchor = 'e').grid(column = 0, row = 0)
  118.         self.txtAdd.grid(column=1,row=0)
  119.         self.btnAdd.grid(column=2,row=0)
  120.         self.btnGet.grid(column=3,row=0)
  121.         self.lblEmpty.grid(column=2,row=1)
  122.         self.lblFull.grid(column=3,row = 1)
  123.         self.lblData.grid(column = 4,row = 0)
  124.  
  125.     def Quit(self):
  126.         sys.exit()
  127.    
  128.     def btnMain(self,p1):
  129.         if p1 == 1:
  130.             self.QueueType = 'FIFO'
  131.             self.obj = self.fifo
  132.             root.title('Queue Tests - FIFO')
  133.             #self.btnFifo.config(relief='sunken')
  134.         elif p1 == 2:
  135.             self.QueueType = 'LIFO'
  136.             self.obj = self.lifo
  137.             root.title('Queue Tests - LIFO')
  138.         elif p1 == 3:
  139.             self.QueueType = 'PRIORITY'
  140.             self.obj = self.pq
  141.             root.title('Queue Tests - Priority')
  142.         elif p1 == 4:
  143.             self.QueueType = 'RING'
  144.         print self.QueueType
  145.         self.ShowStatus()
  146.        
  147.     def ShowStatus(self):
  148.         # Check for Empty
  149.         if self.obj.empty() == True:
  150.             self.EmptyStatus.set('Empty')
  151.         else:
  152.             self.EmptyStatus.set('')
  153.         # Check for Full
  154.         if self.obj.full() == True:
  155.             self.FullStatus.set('FULL')
  156.         else:
  157.             self.FullStatus.set('')
  158.            
  159.     def AddToQueue(self,p1):
  160.         temp = self.Item.get()
  161.         if self.QueueType == 'PRIORITY':
  162.             commapos = temp.find(',')
  163.             if commapos == -1:
  164.                 print "ERROR"
  165.                 tkMessageBox.showerror('Queue Demo',
  166.                     'Priority entry must be in format\r(priority,data)')
  167.             else:
  168.                 self.obj.put(self.Item.get())
  169.         elif not self.obj.full():
  170.             self.obj.put(self.Item.get())
  171.         self.Item.set('')
  172.         self.ShowStatus()
  173.        
  174.     def GetFromQueue(self,p1):
  175.         self.Output.set('')
  176.         if not self.obj.empty():
  177.             temp = self.obj.get()
  178.             self.Output.set("Pulled {0}".format(temp))
  179.         self.ShowStatus()
  180.  
  181.            
  182. if __name__ == '__main__':
  183.     def Center(window):
  184.         # Get the width and height of the screen
  185.         sw = window.winfo_screenwidth()
  186.         sh = window.winfo_screenheight()        
  187.         # Get the width and height of the window
  188.         rw = window.winfo_reqwidth()
  189.         rh = window.winfo_reqheight()
  190.         xc = (sw-rw)/2
  191.         yc = (sh-rh)/2
  192.         window.geometry("%dx%d+%d+%d"%(rw,rh,xc,yc))
  193.         window.deiconify()
  194.    
  195.     root = Tk()
  196.     #root.withdraw()  
  197.     root.title('Queue Tests - FIFO')
  198.     demo = QueueTest(root)  
  199.     root.after(3,Center,root)
  200.     root.mainloop()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement