Search code examples
pythonpython-2.7pyside2

How to move a button Pyside2


I don't know why but i can't move a button using Pyside2.

I named a button Lina and i tried to move it using :

self.__ButtonLina.move(400,400)

but it doesn't work, maybe i place this line in the wrong place in the code ?

Here is the code: (Lina's button is in the last tab)

# -*- coding: utf-8 -*-

import sys
from PySide2 import QtCore, QtGui
from PySide2.QtWidgets import *
from PySide2.QtGui import *

# orange Continental rgb(255, 128, 0)

class Dialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        # Les champs
        self.__champTexteNomAuteur = QLineEdit("")
        self.__champTextePrenomAuteur = QLineEdit("")
        self.__champDateNaissanceAuteur = QDateEdit()
        self.__champDateNaissanceAuteur.setCalendarPopup(True)
        self.__champTexteTitreLivre = QLineEdit("")
        self.__champDatePublication = QDateEdit()
        self.__champDatePublication.setCalendarPopup(True)
        self.__ButtonLina = QPushButton("Lina")
        self.__ButtonLina.setMaximumWidth(145)
        self.__ButtonLina.move(400,400)#------<----here is the problem
        self.__ButtonLina.setStyleSheet("background-color: rgb(255, 128, 0);")
        # Les widgets
        self.__widgetAuteur = QWidget()
        self.__widgetLivre = QWidget()
        self.__widget1 = QWidget()
        self.__widget2 = QWidget()
        self.__Tools = QWidget()
        self.__widget1.setStyleSheet("background-color: black;");
        # Les layouts des onglets

        self.__layoutTools = QFormLayout()
        self.__layoutTools.addRow(self.__ButtonLina)
        self.__Tools.setLayout(self.__layoutTools)

        self.__layoutAuteur = QFormLayout()
        self.__widgetAuteur.setLayout(self.__layoutAuteur)
        # La boîte à onglets
        self.__tabWidget = QTabWidget()
        self.__tabWidget.addTab(self.__widgetAuteur, "                  Single Simulation                   ")
        self.__tabWidget.addTab(self.__widgetLivre, "                  Batch Simulation                   ")
        self.__tabWidget.addTab(self.__widget1, "            Vehicule Simulation Tool                ")
        self.__tabWidget.addTab(self.__widget2, "                     Simulation                     ")
        self.__tabWidget.addTab(self.__Tools, "                       Tools                        ")
        # Le layout final
        self.__mainLayout = QVBoxLayout()
        self.__mainLayout.addWidget(self.__tabWidget)
        self.setLayout(self.__mainLayout)
        self.resize(1200,800)
        self.setWindowTitle('VSS Vehicule Simulation Suite')
        self.setStyleSheet("color: black;"
                        "background-color: black");
app = QApplication(sys.argv)
dialog = Dialog()
dialog.exec_()

if you have any idea it could be cool !


Solution

  • The layouts are used to manage the position and size of the widgets that it handles, in your case the position of the __ButtonLina is handled by __layoutTools, therefore the position you set manually is not applied. So if you want to establish a fixed position you should not use a layout.

    In this case the solution is to remove the layout and set __ButtonLina to __Tools as parent of __ButtonLina to __Tools

    # ...
    self.__widget1.setStyleSheet("background-color: black;");
    
    # Set as parent of__ButtonLina to __Tools 
    # so that the position of __ButtonLina is relative to __Tools
    self.__ButtonLina.setParent(self.__Tools)
    
    # remove layout
    # Les layouts des onglets
    # self.__layoutTools = QFormLayout()
    # self.__layoutTools.addRow(self.__ButtonLina)
    # self.__ButtonLina.clicked.connect(Lina)
    # self.__Tools.setLayout(self.__layoutTools)
    
    self.__layoutAuteur = QFormLayout()
    # ...