如何在 wxPython 中控制网格的视口? [英] How do I control the viewport of a Grid in wxPython?

查看:36
本文介绍了如何在 wxPython 中控制网格的视口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网格应用程序中实现 Find 功能.我可以使用 SetGridCursor(self, row, col) 移动光标,但我不知道如何将网格的视口"移动到新的光标位置.也就是说,当光标移动到用户要查找的单元格时,用户仍然需要手动滚动,直到找到光标所在的单元格.

I'm trying to implement Find functionality in my grid application. I can move the cursor using SetGridCursor(self, row, col), but I can't figure out how to move the "viewport" of the grid to the new cursor position. In other words, when the cursor is moved to the cell the user is looking for, the user still has to manually scroll around until he finds the cursor cell.

如何以编程方式滚动网格,以便在视口中居中网格光标?

How do I programmatically scroll the grid so I can centre the grid cursor in the viewport?

随机,在写这个问题的时候,我去了Grid wiki页面,搜索滚动"这个词,发现:

By random, while writing this question, I went to the Grid wiki page, searched for the word "scroll" and found:

MakeCellVisible( int row, int col ) — 强制特定单元格可见,有效地滚动给定单元格的网格

MakeCellVisible( int row, int col ) — forces the particular cell to be visible, effectively works to scroll the grid to be given cell

这几乎解决了我的问题.我希望单元格在视口中居中,但此功能只会滚动直到单元格在屏幕边缘可见.我找不到任何只影响网格滚动位置而不影响光标位置的函数.想法?

Which almost solves my problem. I would like the cell to be centred in the viewport, but this function only scrolls until the cell is visible at the edge of the screen. I can't find any functions that only affect the scroll position of the grid without also affecting the cursor position. Thoughts?

推荐答案

在这种情况下,请查看 wxWidgets(核心 C++ 库)公开了哪些方法.wxGrid 子类 wxScrolled.wxScrolled 公开了这些方法:

In situations like this, have a look at which methods are exposed by wxWidgets (the core C++ library). wxGrid subclasses wxScrolled. wxScrolled exposes these methods:

滚动窗口,使视图从给定点开始.

Scrolls a window so the view start is at the given point.

  • GetVirtualSize

    获取可滚动窗口区域的设备单元大小(与客户端大小相反,客户端大小是当前可见的窗口区域).

    Gets the size in device units of the scrollable window area (as opposed to the client size, which is the area of the window currently visible).

  • 我认为你可以使用这些方法来计算可滚动窗口的中心坐标.现在,您需要的是要居中的单元格的坐标.wxGrid 暴露了这个方法:

    I think you can use these methods to calculate the center coordinates of the scrollable window. Now, what you need is the coordinates of the cell you would like to center on. wxGrid exposes this method:

    • CellToRect

      返回与网格单元在逻辑坐标中的大小和位置相对应的矩形.

    • Return the rectangle corresponding to the grid cell's size and position in logical coordinates.

      grid = wx.Grid()
      
      cell_coords = grid.CellToRect(12,12)
      
      # get the virtual size by calling it as unbound method
      virtual_size = wx.Scrolled.GetVirtualSize(grid)
      
      # calculate the upper-left coordinate
      scroll_coords = (cell_coords.x - virtual_size.width / 2,
                       cell_coords.y - virtual_size.height / 2)
      
      # call Scroll as unbound method
      wx.Scrolled.Scroll(grid, scroll_coords)
      

      这篇关于如何在 wxPython 中控制网格的视口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆