Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyqt仅在x轴上移动小部件_Python_Pyqt5_Widget - Fatal编程技术网

Python pyqt仅在x轴上移动小部件

Python pyqt仅在x轴上移动小部件,python,pyqt5,widget,Python,Pyqt5,Widget,我正在使用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

我正在使用
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((屏幕宽度-widget宽度)/2,y)

widget.move((屏幕宽度-widget宽度)/2,widget.y())

可以肯定的是,您这样做是因为您在UI中使用固定的几何图形,而您不能/不想使用布局管理器?如果是这样的话,我可以问你为什么吗?这是我第一次使用PyQt,我对基于代码的gui没有任何背景知识。所以在我的例子中,使用设计器是有帮助的。这不是我所要求的。如果您想要小部件自动适应其父级,则必须使用布局管理器,它会自动处理所有问题,包括大小限制、最小大小要求等。您可以阅读更多关于它们的信息以及如何使用它们。使用固定的几何图形在99%的情况下是一个错误的选择,因为你不能仅仅依靠你在屏幕上看到的东西,对于复杂的界面,它很容易使你的UI无法使用。甚至
widget.move((screen\u width-widget\u width)/2,widget.y())