How to call Main Windows Function from Widget inside QStackedWidget in PyQt5 [duplicate]
This question already has an answer here:
call a function when a button is pressed - pyqt
3 answers
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
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.
python pyqt pyqt5 qstackedwidget
marked as duplicate by eyllanesc
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.
add a comment |
This question already has an answer here:
call a function when a button is pressed - pyqt
3 answers
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
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.
python pyqt pyqt5 qstackedwidget
marked as duplicate by eyllanesc
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.
add a comment |
This question already has an answer here:
call a function when a button is pressed - pyqt
3 answers
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
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.
python pyqt pyqt5 qstackedwidget
This question already has an answer here:
call a function when a button is pressed - pyqt
3 answers
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
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
python pyqt pyqt5 qstackedwidget
asked Dec 28 '18 at 9:23
manjymanjy
4910
4910
marked as duplicate by eyllanesc
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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_())
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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_())
add a comment |
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_())
add a comment |
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_())
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_())
answered Dec 28 '18 at 12:08
manjymanjy
4910
4910
add a comment |
add a comment |