pyqt 仅在 x 轴上移动小部件
pyqt move widget in x axis only
我正在使用 PyQt5
并尝试将标签水平居中。该标签已使用 Qt Designer 从 gui 文件加载。我想知道如何使用 move
方法只更新 x 轴,而不更改 y 轴。
class App(QMainWindow):
def __init__(self):
loadUi('gui.ui', self)
self.h_center(self.label)
...
def h_center(widget):
screen_width = self.frameGeometry().width()
widget_width = widget.width()
widget.move((screen_width - widget_width) / 2, 20)
如您所见,我必须为 y 轴指定 20 的值,这会覆盖我使用设计器设置的默认值。如果我忽略它,我会收到以下错误:
>>> widget.move((screen_width - widget_width) / 2)
>>> move(self, QPoint): argument 1 has unexpected type 'float'
>>> move(self, int, int): not enough arguments
如果您想保留 y 轴的值,则必须设置之前的值:
y = widget.pos().y()
widget.move((screen_width - widget_width) / 2, y)
或
widget.move((screen_width - widget_width) / 2, widget.y())
我正在使用 PyQt5
并尝试将标签水平居中。该标签已使用 Qt Designer 从 gui 文件加载。我想知道如何使用 move
方法只更新 x 轴,而不更改 y 轴。
class App(QMainWindow):
def __init__(self):
loadUi('gui.ui', self)
self.h_center(self.label)
...
def h_center(widget):
screen_width = self.frameGeometry().width()
widget_width = widget.width()
widget.move((screen_width - widget_width) / 2, 20)
如您所见,我必须为 y 轴指定 20 的值,这会覆盖我使用设计器设置的默认值。如果我忽略它,我会收到以下错误:
>>> widget.move((screen_width - widget_width) / 2)
>>> move(self, QPoint): argument 1 has unexpected type 'float'
>>> move(self, int, int): not enough arguments
如果您想保留 y 轴的值,则必须设置之前的值:
y = widget.pos().y()
widget.move((screen_width - widget_width) / 2, y)
或
widget.move((screen_width - widget_width) / 2, widget.y())