threadedgui.py :  » Windows » pyExcelerator » pywin32-214 » pythonwin » pywin » Demos » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » pythonwin » pywin » Demos » threadedgui.py
# Demo of using just windows, without documents and views.

# Also demo of a GUI thread, pretty much direct from the MFC C++ sample MTMDI.

import win32ui
import win32con
import win32api
import timer

from pywin.mfc import window,docview,thread
from pywin.mfc.thread import WinThread


WM_USER_PREPARE_TO_CLOSE = win32con.WM_USER + 32

# font is a dictionary in which the following elements matter:
# (the best matching font to supplied parameters is returned)
#   name    string name of the font as known by Windows
#   size    point size of font in logical units
#   weight    weight of font (win32con.FW_NORMAL, win32con.FW_BOLD)
#   italic    boolean; true if set to anything but None
#   underline  boolean; true if set to anything but None

# This window is a child window of a frame.  It is not the frame window itself.
class FontWindow(window.Wnd):
  def __init__(self,  text = 'Python Rules!'):
    window.Wnd.__init__(self)
    self.text = text
    self.index = 0
    self.incr = 1
    self.width = self.height = 0
    self.ChangeAttributes()
    # set up message handlers
    
  def Create(self, title, style, rect, parent):
    classStyle = win32con.CS_HREDRAW | win32con.CS_VREDRAW
    className = win32ui.RegisterWndClass(classStyle, 0, win32con.COLOR_WINDOW+1, 0)
    self._obj_ = win32ui.CreateWnd()
    self._obj_.AttachObject(self)
    self._obj_.CreateWindow(className, title, style, rect, parent, win32ui.AFX_IDW_PANE_FIRST)
    self.HookMessage (self.OnSize, win32con.WM_SIZE)
    self.HookMessage (self.OnPrepareToClose, WM_USER_PREPARE_TO_CLOSE)
    self.HookMessage (self.OnDestroy, win32con.WM_DESTROY)
    self.timerid = timer.set_timer (100, self.OnTimer)
    self.InvalidateRect()

  def OnDestroy (self, msg):
    timer.kill_timer(self.timerid)

  def OnTimer(self, id, timeVal):
    self.index = self.index + self.incr
    if self.index > len(self.text):
      self.incr = -1
      self.index = len(self.text)
    elif self.index < 0:
      self.incr = 1
      self.index = 0
    self.InvalidateRect()

  def OnPaint (self):
#    print "Paint message from thread", win32api.GetCurrentThreadId()
    dc, paintStruct = self.BeginPaint()
    self.OnPrepareDC(dc, None)
    
    if (self.width == 0 and self.height == 0):
      left, top, right, bottom = self.GetClientRect()
      self.width = right - left
      self.height = bottom - top
    x, y = self.width // 2, self.height // 2
    dc.TextOut (x, y, self.text[:self.index])
    self.EndPaint(paintStruct)

  def ChangeAttributes(self):
    font_spec = {'name':'Arial', 'height':42}
    self.font = win32ui.CreateFont (font_spec)
    
  def OnPrepareToClose(self, params):
    self.DestroyWindow()

  def OnSize (self, params):
    lParam = params[3]
    self.width = win32api.LOWORD(lParam)
    self.height = win32api.HIWORD(lParam)

  def OnPrepareDC (self, dc, printinfo):
    # Set up the DC for forthcoming OnDraw call
    dc.SetTextColor (win32api.RGB(0,0,255))
    dc.SetBkColor (win32api.GetSysColor (win32con.COLOR_WINDOW))
    dc.SelectObject (self.font)
    dc.SetTextAlign (win32con.TA_CENTER | win32con.TA_BASELINE)

class FontFrame(window.MDIChildWnd):
  def __init__(self):
    pass # Dont call base class doc/view version...
  def Create(self, title, rect = None, parent = None):
    style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
    self._obj_ = win32ui.CreateMDIChild()
    self._obj_.AttachObject(self)

    self._obj_.CreateWindow(None, title, style, rect, parent)
    rect = self.GetClientRect()
    rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
    self.child = FontWindow("Not threaded")
    self.child.Create("FontDemo", win32con.WS_CHILD | win32con.WS_VISIBLE, rect, self)


class TestThread(WinThread):
  def __init__(self, parentWindow):
    self.parentWindow = parentWindow
    self.child = None
    WinThread.__init__(self)
  def InitInstance(self):
    rect = self.parentWindow.GetClientRect()
    rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])

    self.child = FontWindow()
    self.child.Create("FontDemo", win32con.WS_CHILD | win32con.WS_VISIBLE, rect, self.parentWindow)
    self.SetMainFrame(self.child)
    return WinThread.InitInstance(self)

  def ExitInstance(self):
    return 0

class ThreadedFontFrame(window.MDIChildWnd):
  def __init__(self):
    pass # Dont call base class doc/view version...
    self.thread = None
  def Create(self, title, rect = None, parent = None):
    style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
    self._obj_ = win32ui.CreateMDIChild()
    self._obj_.CreateWindow(None, title, style, rect, parent)
    self._obj_.HookMessage(self.OnDestroy, win32con.WM_DESTROY)
    self._obj_.HookMessage (self.OnSize, win32con.WM_SIZE)
    
    self.thread = TestThread(self)
    self.thread.CreateThread()
    
  def OnSize(self, msg):
    pass

  def OnDestroy(self, msg):
    win32ui.OutputDebugString("OnDestroy\n")
    if self.thread and self.thread.child:
      child = self.thread.child
      child.SendMessage(WM_USER_PREPARE_TO_CLOSE, 0, 0)
      win32ui.OutputDebugString("Destroyed\n")

  
def Demo():
  f = FontFrame()
  f.Create("Font Demo")

def ThreadedDemo():
  rect = win32ui.GetMainFrame().GetMDIClient().GetClientRect()
  rect = rect[0], int(rect[3]*3/4), int(rect[2]/4), rect[3]
  incr = rect[2]
  for i in range(4):
    if i==0:
      f = FontFrame()
      title = "Not threaded"
    else:
      f = ThreadedFontFrame()
      title = "Threaded GUI Demo"
    f.Create(title, rect)
    rect = rect[0] + incr, rect[1], rect[2]+incr, rect[3]
  # Givem a chance to start
  win32api.Sleep(100)
  win32ui.PumpWaitingMessages()

if __name__=='__main__':
  import demoutils
  if demoutils.NeedGoodGUI():
    ThreadedDemo()
#    Demo()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.