Advertisement
Guest User

Weeble

a guest
Nov 27th, 2008
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | None | 0 0
  1. from __future__ import division
  2. from multiprocessing import Process, Queue, freeze_support
  3. from Queue import Empty
  4. import sys
  5. import pygame
  6. from pygame.locals import *
  7. from Tkinter import Tk, Text, END, BOTH, DISABLED
  8. import traceback
  9. import threading
  10.  
  11. def showerrorprocess(title,text):
  12.     """Pop up a window with the given title and text. The
  13.       text will be selectable (so you can copy it to the
  14.       clipboard) but not editable. Returns when the
  15.       window is closed."""
  16.     root = Tk()
  17.     root.title(title)
  18.     text_box = Text(root,width=80,height=15)
  19.     text_box.pack(fill=BOTH)
  20.     text_box.insert(END,text)
  21.     text_box.config(state=DISABLED)
  22.     def quit():
  23.         root.destroy()
  24.         root.quit()
  25.     root.protocol("WM_DELETE_WINDOW", quit)
  26.     root.mainloop()
  27.    
  28. def showerror(title,text):
  29.     """Pop up a window with the given title and text. The
  30.       text will be selectable (so you can copy it to the
  31.       clipboard) but not editable. Runs asynchronously in
  32.       a new child process."""
  33.     process = Process(target=showerrorprocess,args=(title,text))
  34.     process.start()
  35.  
  36. def exceptionreport(e,tb):
  37.     """Get the name and formatted stack trace to report
  38.       an exception. e is the exception, tb is the
  39.       traceback object."""
  40.     return (type(e).__name__,
  41.             "".join(
  42.                 traceback.format_tb(tb)+
  43.                 [type(e).__name__,":",str(e)]))
  44.  
  45. def displayexceptionreport(e,tb):
  46.     """Pop up a window describing an exception e with
  47.       a traceback tb. Asynchronous."""
  48.     name,text=exceptionreport(e,tb)
  49.     showerror(name,text)
  50.  
  51. def pygame_queue_thread(q):
  52.     """Read string items from queue q and post an event
  53.       for each on the pygame event queue."""
  54.     while True:
  55.         data=q.get()
  56.         if type(data)!=str:
  57.             showerror("Expected string data","Expected a string\nGot type %s instead."%type(data).__name__)
  58.         else:            
  59.             pygameEvent=pygame.event.Event(USEREVENT,dict(text=data))
  60.             pygame.event.post(pygameEvent)
  61.            
  62. def start_queue_thread(q):
  63.     """Start up the queue reading thread."""
  64.     queue_thread=threading.Thread(target=pygame_queue_thread,args=(q,))
  65.     queue_thread.setDaemon(True)
  66.     queue_thread.start()
  67.  
  68. def pygame_process(q):
  69.     """Run an example pygame process. Reads strings from
  70.       q and displays them on the screen. Also displays a
  71.       count of the number of redraws performed."""
  72.     try:
  73.         pygame.init()
  74.         w,h=640,480
  75.         mainscreen = pygame.display.set_mode(
  76.             (w,h),
  77.             pygame.RESIZABLE)
  78.         doublebuffer = pygame.Surface((w,h))
  79.         running=True
  80.         redraws = 0
  81.         dirty=True
  82.         start_queue_thread(q)
  83.         text="Started"
  84.         font=pygame.font.SysFont("Arial",64)
  85.         while running:
  86.             if dirty:
  87.                 doublebuffer.fill((0,0,0))
  88.                 redrawCounterSurface=font.render(str(redraws),True,(255,255,255))
  89.                 textSurface=font.render(text,True,(255,255,255))
  90.                 cx=doublebuffer.get_width()//2
  91.                 cy=doublebuffer.get_height()//2
  92.                 doublebuffer.blit(redrawCounterSurface,redrawCounterSurface.get_rect(center=(cx,cy-100)))
  93.                 doublebuffer.blit(textSurface,textSurface.get_rect(center=(cx,cy+100)))
  94.                 mainscreen.blit(doublebuffer,(0,0))
  95.                 pygame.display.flip()
  96.                 redraws+=1
  97.                 dirty=False
  98.             firstevent = pygame.event.wait()
  99.             allevents = [firstevent] + list(pygame.event.get())
  100.             for event in allevents:
  101.                 if event.type in (QUIT,):
  102.                     running=False
  103.                     break
  104.                 elif event.type==VIDEORESIZE:
  105.                     w,h=event.size
  106.                     mainscreen=pygame.display.set_mode(
  107.                         (w,h),
  108.                         pygame.RESIZABLE)
  109.                     doublebuffer=pygame.Surface((w,h))
  110.                     dirty=True
  111.                 elif event.type==VIDEOEXPOSE:
  112.                     dirty=True
  113.                 elif event.type==USEREVENT:
  114.                     dirty=True
  115.                     text=event.text
  116.         pygame.quit()
  117.     except Exception as e:
  118.         displayexceptionreport(e,sys.exc_info()[2])
  119.  
  120. def start_pygame_process():
  121.     q=Queue()
  122.     p=Process(target=pygame_process,args=(q,))
  123.     p.start()
  124.     return p,q
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement