免责声明:也许我在这方面做得不正确,但我在wxpython的GUI开发中只花了大约24小时。因此,我们很感激大家的建议。在
目标:我想通过制作一个简单的屏幕保护程序来熟悉wxpython。在
问题:Im试图绑定鼠标移动和键盘移动(也就是试图捕获任何用户输入),以便在事件发生时销毁()应用程序(典型的屏幕保护程序行为)。到目前为止,我的鼠标事件被捕捉到并被正确地销毁,但是我的键盘事件没有(我可能会在键盘上重击!)。我尝试过EVT_CHAR和CHAR_HOOK,就像其他SO帖子推荐的那样。我甚至去看我的重点在哪里-因为我认为这是我的问题(注意线有self.set焦点()-如果删除此项并移动鼠标,则返回的“自我发现焦点()“由鼠标移动事件触发的事件没有返回。。。使用SetFocus()它现在返回我的SpaceFrame类)。在
问题:为什么我不能捕捉按键并激活我的keyboardMovement()方法?有趣的是,来自here的示例对于键盘事件down/up工作得很好。所以我百分之百的认为这是用户错误。在
import wx
import random
MAX_INVADERS = 10
INVADERS_COLORS = ["yellow_invader",
"green_invader",
"blue_invader",
"red_invader"]
class SpaceFrame(wx.Frame):
def __init__(self):
"""
The generic subclassed Frame/"space" window. All of the invaders fall
into this frame. All animation happens here as the parent window
as well.
"""
wx.Frame.__init__(self, None, wx.ID_ANY, "Space Invaders", pos=(0, 0))
self.SetFocus()
self.Bind(wx.EVT_MOTION, self.mouseMovement)
self.Bind(wx.EVT_CHAR_HOOK, self.keyboardMovement)
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour('black')
self.SetBackgroundColour('black')
self.monitorSize = wx.GetDisplaySize()
for invader in range(0, MAX_INVADERS, 1):
randX = random.randint(0, self.monitorSize[0])
self.showInvader(coords=(randX, 0),
invader=random.choice(INVADERS_COLORS),
scale=(random.randint(2, 10)/100.0))
def mouseMovement(self, event, *args):
print self.FindFocus()
def keyboardMovement(self, event, *args):
self.Destroy()
def showInvader(self, coords=(0, 0), invader="green_invader", scale=.05):
"""
Displays an invader on the screen
"""
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.dropInvader, self.timer)
self.timer.Start(1000)
self.invader = wx.Bitmap("{0}.png".format(invader))
self.invader = wx.ImageFromBitmap(self.invader)
self.invader = self.invader.Scale((self.invader.GetWidth()*scale),
(self.invader.GetHeight()*scale),
wx.IMAGE_QUALITY_HIGH)
self.result = wx.BitmapFromImage(self.invader)
self.control = wx.StaticBitmap(self, -1, self.result)
self.control.SetPosition(coords)
self.panel.Show(True)
def moveInvader(self, coords):
self.control.SetPosition(coords)
def dropInvader(self, *args):
# print "this hit"
self.control.SetPosition((100, 600))
if __name__ == "__main__":
application = wx.PySimpleApp()
window = SpaceFrame()
window.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)
application.MainLoop()
到目前为止所做的研究:也许我遗漏了一些东西,但在这里没有什么是我突出的。在
目前没有回答
相关问题 更多 >
编程相关推荐