2016-04-06 64 views
0

私はwxPythonを使って簡単なマップエディタを実装しています。 StaticBitmapイメージコントロールはScrolledPanelでホストされています。画像のサイズがパネルのサイズよりも大きいため、スクロールバーが表示されます。マウス移動イベントハンドラでは、関数event.GetPosition()は、パネルの左上に関連するマウスの位置を取得できます。しかし、画像の左上に関連するマウスの位置を取得したいです。どうすればそれを達成できますか?ありがとう。wxPythonでは、ScrolledPanelで画像に関連するマウスの位置を取得する方法は?

たとえば、最初のスクリーンショット(上にスクロール)で、マウスがパネルの左上にある場合、画像に関連する位置は(0、0)でなければなりません。 2番目のスクリーンショット(下にスクロール)で、マウスがパネルの左上にある場合、画像に関連する位置は(0、image_height-panel_height)です。トップへ

#!/usr/bin/python 

import wx 
import wx.lib.scrolledpanel 

class SimpleFrame(wx.Frame): 
    def __init__(self, parent): 
     super(SimpleFrame, self).__init__(parent) 

     # add a panel so it looks the correct on all platforms 
     self.frame_panel = wx.Panel(self) 
     frame_panel = self.frame_panel 
     # image panel 
     self.image_panel = wx.lib.scrolledpanel.ScrolledPanel(frame_panel, style=wx.SIMPLE_BORDER) 
     image_panel = self.image_panel 
     image_panel.SetAutoLayout(True) 
     image_panel.SetupScrolling() 
     # image panel - image control 
     self.image_ctrl = wx.StaticBitmap(image_panel) 
     self.image_ctrl.Bind(wx.EVT_MOTION, self.ImageCtrl_OnMouseMove) 
     img = wx.Image("image.jpg", wx.BITMAP_TYPE_ANY) 
     self.image_ctrl.SetBitmap(wx.BitmapFromImage(img)) 
     image_panel.Layout() 
     image_sizer = wx.BoxSizer(wx.VERTICAL) 
     image_sizer.Add(self.image_ctrl) 
     image_panel.SetSizer(image_sizer) 
     # frame sizer 
     frame_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     frame_sizer.Add(image_panel, proportion=1, flag=wx.EXPAND | wx.ALL) 
     frame_panel.SetSizer(frame_sizer) 
     return 

    def ImageCtrl_OnMouseMove(self, event): 
     # position in control 
     ctrl_pos = event.GetPosition() 
     print("ctrl_pos: " + str(ctrl_pos.x) + ", " + str(ctrl_pos.y)) 
     # position in image 
     #image_pos = ??? convert control position to image position 
     #print("image_pos: " + str(image_pos.x) + ", " + str(image_pos.y)) 


app = wx.PySimpleApp() 
frame = SimpleFrame(None) 
frame.Show() 
app.MainLoop() 

スクロール: Scrolled to top

スクロール下へ: Scrolled to bottom

+0

。あなたは画像自体に 'EVT_MOTION'を聞いているので、取得する位置は画像を基準にしています。 –

+0

@SelçukCihanいいえ、それはありません!位置はパネル内の位置になります。 –

答えて

1

あなたはのクライアント座標を取得するには、ウィンドウが現在ある場所の座標とScreenToClient()を取得するためにGetScreenPosition()を使用する必要があります画像。一緒に追加すると、画像上の相対的な位置を取得します。
このようにすることの美しさは、ユーザーがウィンドウの位置を変更したり、サイズを変更したりして一貫した位置を得ることができることです。

は、ここにあなたのコードが修正されています:あなたが期待するように、これはすでに働いている

#!/usr/bin/python 

import wx 
import wx.lib.scrolledpanel 

class SimpleFrame(wx.Frame): 
    def __init__(self, parent): 
     super(SimpleFrame, self).__init__(parent) 

     # add a panel so it looks the correct on all platforms 
     self.frame_panel = wx.Panel(self) 
     frame_panel = self.frame_panel 
     # image panel 
     self.image_panel = wx.lib.scrolledpanel.ScrolledPanel(frame_panel, style=wx.SIMPLE_BORDER) 
     image_panel = self.image_panel 
     image_panel.SetAutoLayout(True) 
     image_panel.SetupScrolling() 
     # image panel - image control 
     self.image_ctrl = wx.StaticBitmap(image_panel) 
     self.image_ctrl.Bind(wx.EVT_MOTION, self.ImageCtrl_OnMouseMove) 
     self.img = wx.Image("image.jpg", wx.BITMAP_TYPE_ANY) 
     self.image_ctrl.SetBitmap(wx.BitmapFromImage(self.img)) 
     image_panel.Layout() 
     image_sizer = wx.BoxSizer(wx.VERTICAL) 
     image_sizer.Add(self.image_ctrl) 
     image_panel.SetSizer(image_sizer) 
     # frame sizer 
     frame_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     frame_sizer.Add(image_panel, proportion=1, flag=wx.EXPAND | wx.ALL) 
     frame_panel.SetSizer(frame_sizer) 
     return 

    def ImageCtrl_OnMouseMove(self, event): 
     # position in control 
     ctrl_pos = event.GetPosition() 
     print("ctrl_pos: " + str(ctrl_pos.x) + ", " + str(ctrl_pos.y)) 
     pos = self.image_ctrl.ScreenToClient(ctrl_pos) 
     print "pos relative to screen top left = ", pos 
     screen_pos = self.frame_panel.GetScreenPosition() 
     relative_pos_x = pos[0] + screen_pos[0] 
     relative_pos_y = pos[1] + screen_pos[1] 
     print "pos relative to image top left = ", relative_pos_x, relative_pos_y 


app = wx.PySimpleApp() 
frame = SimpleFrame(None) 
frame.Show() 
app.MainLoop() 
+0

ありがとう!これは私が欲しいものです。 –

関連する問題