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

coolnick 2013-08-05 05:20:56
代码如下。基本可以正常运行,当鼠标到了某个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()

...全文
102 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
panghuhu250 2013-08-05
  • 打赏
  • 举报
回复

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

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧