首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

在Grid的Cell上自动弹出PopupWindow遇到的有关问题

2013-08-09 
在Grid的Cell上自动弹出PopupWindow遇到的问题代码如下。基本可以正常运行,当鼠标到了某个Cell上时,自动在

在Grid的Cell上自动弹出PopupWindow遇到的问题
代码如下。基本可以正常运行,当鼠标到了某个Cell上时,自动在该Cell内显示一个PopupWindow。
现在的问题是,当滚动Grid的滚动条之后,PopupWindow就显示错位了。有经验的指点一下,先谢了。


import wx
import wx.grid as grd

class TestPopup(wx.PopupTransientWindow):
    def __init__(self, parent, style):
        wx.PopupTransientWindow.__init__(self, parent, style)

        btn = wx.Button(self, -1, "Test", (0, 0))
        
        sz = btn.GetBestSize()
        self.SetSize( (sz.width, sz.height) )
        wx.CallAfter(self.Refresh)

class MyGrid(grd.Grid):
    def __init__(self, parent):
        grd.Grid.__init__(self, parent, -1, pos=(10,40), size=(350,110))

        self.CreateGrid(30,1)
       
        self.SetRowLabelSize(50)
        self.SetColLabelSize(20)
        
        self.SetDefaultRowSize(30)
        self.SetColSize(0, 300)

class TestFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Test Frame", size=(800,600))
        self.grid = MyGrid(self)
        self.grid.SetFocus()
        self.CentreOnScreen()
        
        self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMouseOver)
        
    def OnMouseOver(self, evt):
        x,y = self.grid.CalcUnscrolledPosition(evt.GetX(), evt.GetY())

        coords = self.grid.XYToCell(x, y)
        row = coords[0]
        col = coords[1]
        
        try:
            self.win.Show(False)


            self.win.Destroy()
        except:
            pass
            
        if 0 == col:
            self.win = TestPopup(self, wx.SIMPLE_BORDER)
    
            rowsize = self.grid.GetRowSize(row)
            colsize = self.grid.GetColSize(col)
            
            w, h = self.win.GetSize()
            
            posx = colsize * (col+1) - w
            posy = rowsize * row + (rowsize - h)/2
        
            pos = evt.GetEventObject().ClientToScreen( (0, 0) )
            self.win.Position(pos, (posx, posy))
            
            self.win.Show(True) 
      
if __name__ == '__main__':
    app = wx.App(False) 
    frame = TestFrame(None)

    frame.Show()
    app.MainLoop()   


[解决办法]


            posx = colsize * (col+1) - w
            posy = rowsize * row + (rowsize - h)/2


计算posy的时候,你算的是该行相对于第一行的位置,应该是该行相对于目前显示的第一行的位置。改成:

            posx = colsize * (col+1) - w
            posy = rowsize * row + (rowsize - h)/2 - (y - evt.GetY())

热点排行