Keydown Event and Focus Problem

mee-shell jadethacker at hotmail.com
Fri Oct 22 21:07:12 EDT 2004


Hi, I am a python newbie.  I have created a tool for capturing the
screen shot.  I want to be able to do the screen capture with the
keydown event as well.  The tool worked fine when it first opened. 
However, if I keep the tool open, and open another window to do the
screen capture, the tool will lose its focus, and the keydown event
will not work anymore.  I wonder if any of you can help me to solve
this problem of how to get the focus back to the tool after opening a
new window?  Here is my code, and thank you in advance.


from wxPython.wx import *
import Image
import ImageGrab
import time, string, os
TITLE    = "wxPython test"

class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
                         wxDefaultPosition,
			 wxSize(400, 25), style = wxSIMPLE_BORDER|wxSTAY_ON_TOP)

        #------------------------------------------------------------------
        # add a panel
        self.panelCNB = wxPanel(self, -1, wxPoint(15,0),
wxSize(200,25))
        self.panelCNB.SetOwnBackgroundColour(wxColor(99,148,214))
        screenButton = wxButton(self.panelCNB, 1000, 'screen shot')
        exitButton = wxButton(self.panelCNB, 5000, 'exit')

        buttonSizer = wxBoxSizer(wxHORIZONTAL)    
        buttonSizer.Add(screenButton, 0)
        buttonSizer.Add(exitButton, 0)
        self.panelCNB.SetSizer(buttonSizer)
        self.panelCNB.Layout()

        # Set the focus so that the panel gets key events
        self.SetFocus()

        # button event and key down
        EVT_BUTTON(screenButton, 1000, self.ScreenShot)
        EVT_BUTTON(exitButton, 5000, self.Exit)
        EVT_KEY_DOWN(self, self.OnKeyDown)
        
    def Exit(self, evt):
        self.Close(true)

    def ScreenShot(self, evt):
        filename = time.strftime("%Y%m%d%H%M%S", time.localtime()) 
        temp = filename + 'temp.jpg'
        thumbnail = filename + 'thumb.jpg'
        file = filename + '.jpg'
        
        ImageGrab.grab().save(temp)
        testimage=Image.open(temp)
        testimage.thumbnail((800,600),Image.ANTIALIAS)
        testimage.save(file)
        testimage.thumbnail((100,75),Image.ANTIALIAS)
        testimage.save(thumbnail)

    def OnKeyDown(self, evt):
        if evt.ControlDown() and evt.GetKeyCode() == ord('R'):
            self.SetFocus()
            self.ScreenShot(evt)

app = wxPySimpleApp()
frame = MyFrame(None, -1, TITLE)
frame.SetBackgroundColour(wxColor(99,148,214))
frame.Show(true)
app.MainLoop()



More information about the Python-list mailing list