Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- from multiprocessing import Process, Queue, freeze_support
- from Queue import Empty
- import sys
- import pygame
- from pygame.locals import *
- from Tkinter import Tk, Text, END, BOTH, DISABLED
- import traceback
- import threading
- def showerrorprocess(title,text):
- """Pop up a window with the given title and text. The
- text will be selectable (so you can copy it to the
- clipboard) but not editable. Returns when the
- window is closed."""
- root = Tk()
- root.title(title)
- text_box = Text(root,width=80,height=15)
- text_box.pack(fill=BOTH)
- text_box.insert(END,text)
- text_box.config(state=DISABLED)
- def quit():
- root.destroy()
- root.quit()
- root.protocol("WM_DELETE_WINDOW", quit)
- root.mainloop()
- def showerror(title,text):
- """Pop up a window with the given title and text. The
- text will be selectable (so you can copy it to the
- clipboard) but not editable. Runs asynchronously in
- a new child process."""
- process = Process(target=showerrorprocess,args=(title,text))
- process.start()
- def exceptionreport(e,tb):
- """Get the name and formatted stack trace to report
- an exception. e is the exception, tb is the
- traceback object."""
- return (type(e).__name__,
- "".join(
- traceback.format_tb(tb)+
- [type(e).__name__,":",str(e)]))
- def displayexceptionreport(e,tb):
- """Pop up a window describing an exception e with
- a traceback tb. Asynchronous."""
- name,text=exceptionreport(e,tb)
- showerror(name,text)
- def pygame_queue_thread(q):
- """Read string items from queue q and post an event
- for each on the pygame event queue."""
- while True:
- data=q.get()
- if type(data)!=str:
- showerror("Expected string data","Expected a string\nGot type %s instead."%type(data).__name__)
- else:
- pygameEvent=pygame.event.Event(USEREVENT,dict(text=data))
- pygame.event.post(pygameEvent)
- def start_queue_thread(q):
- """Start up the queue reading thread."""
- queue_thread=threading.Thread(target=pygame_queue_thread,args=(q,))
- queue_thread.setDaemon(True)
- queue_thread.start()
- def pygame_process(q):
- """Run an example pygame process. Reads strings from
- q and displays them on the screen. Also displays a
- count of the number of redraws performed."""
- try:
- pygame.init()
- w,h=640,480
- mainscreen = pygame.display.set_mode(
- (w,h),
- pygame.RESIZABLE)
- doublebuffer = pygame.Surface((w,h))
- running=True
- redraws = 0
- dirty=True
- start_queue_thread(q)
- text="Started"
- font=pygame.font.SysFont("Arial",64)
- while running:
- if dirty:
- doublebuffer.fill((0,0,0))
- redrawCounterSurface=font.render(str(redraws),True,(255,255,255))
- textSurface=font.render(text,True,(255,255,255))
- cx=doublebuffer.get_width()//2
- cy=doublebuffer.get_height()//2
- doublebuffer.blit(redrawCounterSurface,redrawCounterSurface.get_rect(center=(cx,cy-100)))
- doublebuffer.blit(textSurface,textSurface.get_rect(center=(cx,cy+100)))
- mainscreen.blit(doublebuffer,(0,0))
- pygame.display.flip()
- redraws+=1
- dirty=False
- firstevent = pygame.event.wait()
- allevents = [firstevent] + list(pygame.event.get())
- for event in allevents:
- if event.type in (QUIT,):
- running=False
- break
- elif event.type==VIDEORESIZE:
- w,h=event.size
- mainscreen=pygame.display.set_mode(
- (w,h),
- pygame.RESIZABLE)
- doublebuffer=pygame.Surface((w,h))
- dirty=True
- elif event.type==VIDEOEXPOSE:
- dirty=True
- elif event.type==USEREVENT:
- dirty=True
- text=event.text
- pygame.quit()
- except Exception as e:
- displayexceptionreport(e,sys.exc_info()[2])
- def start_pygame_process():
- q=Queue()
- p=Process(target=pygame_process,args=(q,))
- p.start()
- return p,q
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement