【问题标题】:Is there a way to know the X and Y coordinates of an element in a QGridLayout?有没有办法知道 QGridLayout 中元素的 X 和 Y 坐标?
【发布时间】:2019-10-03 19:50:27
【问题描述】:

我有一个 QGridLayout,我要在其中添加不同的元素,我需要知道这些元素,并且我需要知道特定元素的坐标。有没有办法做到这一点? 我阅读了文档并尝试使用 pos() 和 geometry() 属性,但我无法得到我想要的。 例如,给定以下代码:

import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,QPushButton, QApplication)

class basicWindow(QWidget):
    def __init__(self):
        super().__init__()
        grid_layout = QGridLayout()
        self.setLayout(grid_layout)

        for x in range(3):
            for y in range(3):
                button = QPushButton(str(str(3*x+y)))
                grid_layout.addWidget(button, x, y)

        self.setWindowTitle('Basic Grid Layout')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windowExample = basicWindow()
    windowExample.show()
    sys.exit(app.exec_())

有没有办法知道每个按钮的坐标?

【问题讨论】:

标签: python pyqt pyqt5 qgridlayout


【解决方案1】:

几何图形仅在显示小部件时更新,因此您可能在显示坐标之前打印坐标。在下面的示例中,如果您按下任何按钮,它将打印相对于窗口的坐标。

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication


class BasicWindow(QWidget):
    def __init__(self):
        super().__init__()
        grid_layout = QGridLayout(self)

        for x in range(3):
            for y in range(3):
                button = QPushButton(str(3 * x + y))
                button.clicked.connect(self.on_clicked)
                grid_layout.addWidget(button, x, y)

        self.setWindowTitle("Basic Grid Layout")

    @pyqtSlot()
    def on_clicked(self):
        button = self.sender()
        print(button.text(), ":", button.pos(), button.geometry())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    windowExample = BasicWindow()
    windowExample.show()
    sys.exit(app.exec_())

输出:

0 : PyQt5.QtCore.QPoint(10, 10) PyQt5.QtCore.QRect(10, 10, 84, 34)
4 : PyQt5.QtCore.QPoint(100, 50) PyQt5.QtCore.QRect(100, 50, 84, 34)
8 : PyQt5.QtCore.QPoint(190, 90) PyQt5.QtCore.QRect(190, 90, 84, 34)

更新:

如果要在给定行和列的情况下获取小部件的位置,首先要获取 QLayoutItem,并且必须从该 QLayoutItem 中获取小部件。在下面的例子中,按钮的位置在窗口显示后的一瞬间被打印出来:

import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication


class basicWindow(QWidget):
    def __init__(self):
        super().__init__()
        grid_layout = QGridLayout(self)

        for x in range(3):
            for y in range(3):
                button = QPushButton(str(3 * x + y))
                grid_layout.addWidget(button, x, y)

        self.setWindowTitle("Basic Grid Layout")

        QTimer.singleShot(0, lambda: self.print_coordinates(1, 1))

    def print_coordinates(self, x, y):
        grid_layout = self.layout()
        it = grid_layout.itemAtPosition(x, y)
        w = it.widget()
        print(w.pos())



if __name__ == "__main__":
    app = QApplication(sys.argv)
    windowExample = basicWindow()
    windowExample.show()
    sys.exit(app.exec_())

【讨论】:

  • 谢谢!这正是我所想的。还有一件事,如果没有 on_clicked 方法,什么是获取坐标的好方法?我想可能在第 16 行使用grid_layout.itemAtPosition(x,y).pos()(就在您将按钮添加到小部件之后)但这不起作用。
  • 谢谢,但有没有办法将坐标“保存”在变量中?类似'coordinates = (x,y)'的东西,其中x和y是对应的坐标。我试图这样做,但坐标最终是一个 Nonetype 对象。我想将每个按钮的坐标保存在不同的元组中。我对 QTimer 不太了解,所以这可能是我无法做到这一点的原因。
  • @Juan add coordinates = w.pos().x(), w.pos().y() on print_coordinates 但无论如何,将其存储在变量中与 GUI 无关,而是与 OOP 无关。虽然我觉得你想获取一个小部件的坐标很奇怪,但你可能有一个XY problem
  • 我会解释整个问题。我正在创建一个迷你游戏,其中我有一个角色,每按下一个键(WASD)移动 10 个像素,并且有角色无法跨越的墙壁。我正在考虑保存墙壁的坐标,并使其为程序的“行走”部分创建的类不会让角色越过这些坐标。地图本身(显示墙壁的地图)是一个 QGridLayout,并且角色在与网格大小完全相同的 QLabel 上移动。这可能是一种复杂的创建方式,但我只是从 Pyqt 开始。
  • @Juan mmm,因为我以为你走错路了。如果你想用Qt制作游戏,那么你必须使用Qt图形框架:doc.qt.io/qt-5/graphicsview.html如果你想看例子你必须下载源代码:riverbankcomputing.com/software/pyqt/download5,解压到examples/graphicsview文件夹
猜你喜欢
  • 2011-08-27
  • 2014-09-13
  • 2017-06-28
  • 2018-10-31
  • 2011-02-14
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多