Keydown Event and Focus Problem

M

mee-shell

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top