How to call Main Windows Function from Widget inside QStackedWidget in PyQt5 [duplicate]












0















This question already has an answer here:




  • call a function when a button is pressed - pyqt

    3 answers




LAYOUT IMAGe



For reference purpose consider the above example.



On the left I have two buttons PAGE1 and PAGE2 and on the right I have a QStackedWidget consisting of two widgets.



I made two seperate UI files with names page1.ui and page2.ui
enter image description here



I used "Promote Widget" to add page1.ui and page2.ui inside my stacked widget.
(See the image for reference)



The following is structure of my files:




├── main.py
├── pages
│ ├── page1.py
│ ├── page2.py
└── ui
├── main.ui
├── page1.ui
├── page2.ui



What I would like to achieve is that I am able to change the Index of Stacked Widget from a Widget inside Stacked Widget.



Currently I am only able to change Index of Stacked Widget by using buttons in main.ui.



CODE:
main.py



import os
from PyQt5 import QtGui, uic, QtWidgets
from functools import partial


current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

class MainWidget(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
buttons = (self.page1Button, self.page2Button)
for i, button in enumerate(buttons):
button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))



if __name__ == '__main__':
import sys
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)

app.setStyle("fusion")
w = MainWidget()
w.show()
sys.exit(app.exec_())


page1.py:



import os
from PyQt5 import QtGui, uic, QtWidgets

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/page1.ui"))

class page1Window(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)


if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = page1Window()
w.show()
sys.exit(app.exec_())


page2.py is similar to page1.py



Suppose I have a button in page1.ui, how do I use this button to change the index of Stacked Widget using this button?



If you feel I am missing anything or have doubt on what I exactly want to do please leave a comment.










share|improve this question













marked as duplicate by eyllanesc pyqt
Users with the  pyqt badge can single-handedly close pyqt questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 13:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • call a function when a button is pressed - pyqt

      3 answers




    LAYOUT IMAGe



    For reference purpose consider the above example.



    On the left I have two buttons PAGE1 and PAGE2 and on the right I have a QStackedWidget consisting of two widgets.



    I made two seperate UI files with names page1.ui and page2.ui
    enter image description here



    I used "Promote Widget" to add page1.ui and page2.ui inside my stacked widget.
    (See the image for reference)



    The following is structure of my files:




    ├── main.py
    ├── pages
    │ ├── page1.py
    │ ├── page2.py
    └── ui
    ├── main.ui
    ├── page1.ui
    ├── page2.ui



    What I would like to achieve is that I am able to change the Index of Stacked Widget from a Widget inside Stacked Widget.



    Currently I am only able to change Index of Stacked Widget by using buttons in main.ui.



    CODE:
    main.py



    import os
    from PyQt5 import QtGui, uic, QtWidgets
    from functools import partial


    current_dir = os.path.dirname(os.path.abspath(__file__))
    Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

    class MainWidget(Base, Form):
    def __init__(self, parent=None):
    super(self.__class__, self).__init__(parent)
    self.setupUi(self)
    buttons = (self.page1Button, self.page2Button)
    for i, button in enumerate(buttons):
    button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))



    if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication.instance()
    if app is None:
    app = QtWidgets.QApplication(sys.argv)

    app.setStyle("fusion")
    w = MainWidget()
    w.show()
    sys.exit(app.exec_())


    page1.py:



    import os
    from PyQt5 import QtGui, uic, QtWidgets

    current_dir = os.path.dirname(os.path.abspath(__file__))
    Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/page1.ui"))

    class page1Window(Base, Form):
    def __init__(self, parent=None):
    super(self.__class__, self).__init__(parent)
    self.setupUi(self)


    if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = page1Window()
    w.show()
    sys.exit(app.exec_())


    page2.py is similar to page1.py



    Suppose I have a button in page1.ui, how do I use this button to change the index of Stacked Widget using this button?



    If you feel I am missing anything or have doubt on what I exactly want to do please leave a comment.










    share|improve this question













    marked as duplicate by eyllanesc pyqt
    Users with the  pyqt badge can single-handedly close pyqt questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 28 '18 at 13:07


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • call a function when a button is pressed - pyqt

        3 answers




      LAYOUT IMAGe



      For reference purpose consider the above example.



      On the left I have two buttons PAGE1 and PAGE2 and on the right I have a QStackedWidget consisting of two widgets.



      I made two seperate UI files with names page1.ui and page2.ui
      enter image description here



      I used "Promote Widget" to add page1.ui and page2.ui inside my stacked widget.
      (See the image for reference)



      The following is structure of my files:




      ├── main.py
      ├── pages
      │ ├── page1.py
      │ ├── page2.py
      └── ui
      ├── main.ui
      ├── page1.ui
      ├── page2.ui



      What I would like to achieve is that I am able to change the Index of Stacked Widget from a Widget inside Stacked Widget.



      Currently I am only able to change Index of Stacked Widget by using buttons in main.ui.



      CODE:
      main.py



      import os
      from PyQt5 import QtGui, uic, QtWidgets
      from functools import partial


      current_dir = os.path.dirname(os.path.abspath(__file__))
      Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

      class MainWidget(Base, Form):
      def __init__(self, parent=None):
      super(self.__class__, self).__init__(parent)
      self.setupUi(self)
      buttons = (self.page1Button, self.page2Button)
      for i, button in enumerate(buttons):
      button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))



      if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication.instance()
      if app is None:
      app = QtWidgets.QApplication(sys.argv)

      app.setStyle("fusion")
      w = MainWidget()
      w.show()
      sys.exit(app.exec_())


      page1.py:



      import os
      from PyQt5 import QtGui, uic, QtWidgets

      current_dir = os.path.dirname(os.path.abspath(__file__))
      Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/page1.ui"))

      class page1Window(Base, Form):
      def __init__(self, parent=None):
      super(self.__class__, self).__init__(parent)
      self.setupUi(self)


      if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication(sys.argv)
      w = page1Window()
      w.show()
      sys.exit(app.exec_())


      page2.py is similar to page1.py



      Suppose I have a button in page1.ui, how do I use this button to change the index of Stacked Widget using this button?



      If you feel I am missing anything or have doubt on what I exactly want to do please leave a comment.










      share|improve this question














      This question already has an answer here:




      • call a function when a button is pressed - pyqt

        3 answers




      LAYOUT IMAGe



      For reference purpose consider the above example.



      On the left I have two buttons PAGE1 and PAGE2 and on the right I have a QStackedWidget consisting of two widgets.



      I made two seperate UI files with names page1.ui and page2.ui
      enter image description here



      I used "Promote Widget" to add page1.ui and page2.ui inside my stacked widget.
      (See the image for reference)



      The following is structure of my files:




      ├── main.py
      ├── pages
      │ ├── page1.py
      │ ├── page2.py
      └── ui
      ├── main.ui
      ├── page1.ui
      ├── page2.ui



      What I would like to achieve is that I am able to change the Index of Stacked Widget from a Widget inside Stacked Widget.



      Currently I am only able to change Index of Stacked Widget by using buttons in main.ui.



      CODE:
      main.py



      import os
      from PyQt5 import QtGui, uic, QtWidgets
      from functools import partial


      current_dir = os.path.dirname(os.path.abspath(__file__))
      Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

      class MainWidget(Base, Form):
      def __init__(self, parent=None):
      super(self.__class__, self).__init__(parent)
      self.setupUi(self)
      buttons = (self.page1Button, self.page2Button)
      for i, button in enumerate(buttons):
      button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))



      if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication.instance()
      if app is None:
      app = QtWidgets.QApplication(sys.argv)

      app.setStyle("fusion")
      w = MainWidget()
      w.show()
      sys.exit(app.exec_())


      page1.py:



      import os
      from PyQt5 import QtGui, uic, QtWidgets

      current_dir = os.path.dirname(os.path.abspath(__file__))
      Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/page1.ui"))

      class page1Window(Base, Form):
      def __init__(self, parent=None):
      super(self.__class__, self).__init__(parent)
      self.setupUi(self)


      if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication(sys.argv)
      w = page1Window()
      w.show()
      sys.exit(app.exec_())


      page2.py is similar to page1.py



      Suppose I have a button in page1.ui, how do I use this button to change the index of Stacked Widget using this button?



      If you feel I am missing anything or have doubt on what I exactly want to do please leave a comment.





      This question already has an answer here:




      • call a function when a button is pressed - pyqt

        3 answers








      python pyqt pyqt5 qstackedwidget






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 9:23









      manjymanjy

      4910




      4910




      marked as duplicate by eyllanesc pyqt
      Users with the  pyqt badge can single-handedly close pyqt questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 28 '18 at 13:07


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by eyllanesc pyqt
      Users with the  pyqt badge can single-handedly close pyqt questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 28 '18 at 13:07


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Okay I managed to get it working. Turns out the solution was pretty straight forward



          Here is what I did:
          main.py:



          import os
          from PyQt5 import QtGui, uic, QtWidgets
          from functools import partial


          current_dir = os.path.dirname(os.path.abspath(__file__))
          Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

          class MainWidget(Base, Form):
          def __init__(self, parent=None):
          super(self.__class__, self).__init__(parent)
          self.setupUi(self)
          buttons = (self.page1Button, self.page2Button)
          for i, button in enumerate(buttons):
          button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))


          self.page_1.pushButton.clicked.connect(self.foo)
          #the above line calls foo() inside main.py when a button on page_1 is clicked



          def foo(self):
          # set stack widget to what ever you need
          self.stackedWidget.setCurrentIndex(1)


          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication.instance()
          if app is None:
          app = QtWidgets.QApplication(sys.argv)

          app.setStyle("fusion")
          w = MainWidget()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Okay I managed to get it working. Turns out the solution was pretty straight forward



            Here is what I did:
            main.py:



            import os
            from PyQt5 import QtGui, uic, QtWidgets
            from functools import partial


            current_dir = os.path.dirname(os.path.abspath(__file__))
            Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

            class MainWidget(Base, Form):
            def __init__(self, parent=None):
            super(self.__class__, self).__init__(parent)
            self.setupUi(self)
            buttons = (self.page1Button, self.page2Button)
            for i, button in enumerate(buttons):
            button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))


            self.page_1.pushButton.clicked.connect(self.foo)
            #the above line calls foo() inside main.py when a button on page_1 is clicked



            def foo(self):
            # set stack widget to what ever you need
            self.stackedWidget.setCurrentIndex(1)


            if __name__ == '__main__':
            import sys
            app = QtWidgets.QApplication.instance()
            if app is None:
            app = QtWidgets.QApplication(sys.argv)

            app.setStyle("fusion")
            w = MainWidget()
            w.show()
            sys.exit(app.exec_())





            share|improve this answer


























              0














              Okay I managed to get it working. Turns out the solution was pretty straight forward



              Here is what I did:
              main.py:



              import os
              from PyQt5 import QtGui, uic, QtWidgets
              from functools import partial


              current_dir = os.path.dirname(os.path.abspath(__file__))
              Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

              class MainWidget(Base, Form):
              def __init__(self, parent=None):
              super(self.__class__, self).__init__(parent)
              self.setupUi(self)
              buttons = (self.page1Button, self.page2Button)
              for i, button in enumerate(buttons):
              button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))


              self.page_1.pushButton.clicked.connect(self.foo)
              #the above line calls foo() inside main.py when a button on page_1 is clicked



              def foo(self):
              # set stack widget to what ever you need
              self.stackedWidget.setCurrentIndex(1)


              if __name__ == '__main__':
              import sys
              app = QtWidgets.QApplication.instance()
              if app is None:
              app = QtWidgets.QApplication(sys.argv)

              app.setStyle("fusion")
              w = MainWidget()
              w.show()
              sys.exit(app.exec_())





              share|improve this answer
























                0












                0








                0






                Okay I managed to get it working. Turns out the solution was pretty straight forward



                Here is what I did:
                main.py:



                import os
                from PyQt5 import QtGui, uic, QtWidgets
                from functools import partial


                current_dir = os.path.dirname(os.path.abspath(__file__))
                Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

                class MainWidget(Base, Form):
                def __init__(self, parent=None):
                super(self.__class__, self).__init__(parent)
                self.setupUi(self)
                buttons = (self.page1Button, self.page2Button)
                for i, button in enumerate(buttons):
                button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))


                self.page_1.pushButton.clicked.connect(self.foo)
                #the above line calls foo() inside main.py when a button on page_1 is clicked



                def foo(self):
                # set stack widget to what ever you need
                self.stackedWidget.setCurrentIndex(1)


                if __name__ == '__main__':
                import sys
                app = QtWidgets.QApplication.instance()
                if app is None:
                app = QtWidgets.QApplication(sys.argv)

                app.setStyle("fusion")
                w = MainWidget()
                w.show()
                sys.exit(app.exec_())





                share|improve this answer












                Okay I managed to get it working. Turns out the solution was pretty straight forward



                Here is what I did:
                main.py:



                import os
                from PyQt5 import QtGui, uic, QtWidgets
                from functools import partial


                current_dir = os.path.dirname(os.path.abspath(__file__))
                Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

                class MainWidget(Base, Form):
                def __init__(self, parent=None):
                super(self.__class__, self).__init__(parent)
                self.setupUi(self)
                buttons = (self.page1Button, self.page2Button)
                for i, button in enumerate(buttons):
                button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))


                self.page_1.pushButton.clicked.connect(self.foo)
                #the above line calls foo() inside main.py when a button on page_1 is clicked



                def foo(self):
                # set stack widget to what ever you need
                self.stackedWidget.setCurrentIndex(1)


                if __name__ == '__main__':
                import sys
                app = QtWidgets.QApplication.instance()
                if app is None:
                app = QtWidgets.QApplication(sys.argv)

                app.setStyle("fusion")
                w = MainWidget()
                w.show()
                sys.exit(app.exec_())






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 28 '18 at 12:08









                manjymanjy

                4910




                4910















                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas