Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've created window with button named "First", the window shows me next button named "Second" after clicking on the button "First", but button "Second" isn't moved by:

self.b2.move(50,50)

Whats the problem?

import sys
from PyQt5 import QtWidgets
class Window(QtWidgets.QMainWindow):
   def __init__(self):
       super().__init__()
       self.init_UI()
   def init_UI(self):
       self.Centr= QtWidgets.QWidget()
       self.setCentralWidget(self.Centr)
       self.window = QtWidgets.QStackedWidget(self.Centr)
       self.b1 = self.addButton()
       self.window.addWidget(self.b1)
       self.b2 = self.addButton_2()
       self.b2.move(50,50)
       self.window.addWidget(self.b2)
       self.b1.clicked.connect(self.b1_clk)
       self.currentStack(0)
       self.show()
   def addButton(self): 
       b1 = QtWidgets.QPushButton("First")
       return b1
   def addButton_2(self):  
       b2 = QtWidgets.QPushButton("Second")
       return b2
   def b1_clk(self):
       self.currentStack(1)
   def currentStack(self, index):
      self.window.setCurrentIndex(index)
app = QtWidgets.QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())

Sorry for my English. And thank you for your attention!

Why do you want to place a button in the QStackedWidget? It is normal to place it inside the pages managed by the QStackedWidget. – eyllanesc Apr 5, 2018 at 8:17 Thank you! But how can I move button in window? I'm trying by self.window.b2.move(50,50) after adding button into window widget and catch: 'QStackedWidget' object has no attribute 'b2' – Artem Getmanskiy Apr 5, 2018 at 8:28 Will it better tactic to add all buttons in different CentralWidget, then add all CentralWidget in QStackedWidget and managing its by QStackedWidget methods? – Artem Getmanskiy Apr 5, 2018 at 8:37 @ArtemGetmanskiy You generally shouldn't be using the move command. Very few widgets are hard positioned. People generally use QLayouts and spacers to position their widgets. You should probably add a QWidget to the stacked widget and position the button inside that with a layout and some spacer items. – Brendan Abel Apr 5, 2018 at 17:08

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.