I am trying to access all the buttons, labels, text widgets, etc inside nested QVBoxLayout and QHBoxLayout layouts using the children() method. But I am not getting the results I expect: I am only getting the layouts but not the buttons, labels, etc.
Is there a Pythonic way of iterating through all the nested layouts to obtain the buttons, labels, textedit, etc.?
Here is the part where I am having trouble:
for child in self.ui.gridLayout.children():
print "child_name:%s - type:%s" %(child.objectName(),type(child))
for grand_child in child.children():
print "gc_name:%s - type:%s" %(grand_child.objectName(),type(grand_child))
# never prints the buttons, labels, etc
for ggc in grand_child.children():
print "ggc_name:%s - type:%s" %(ggc.objectName(),type(ggc))
# never finds the QLabels
qreg = QRegExp('.+')
widgets = grand_child.findChildren(QLabel,qreg)
print widgets # prints None
Here is the whole code:
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mygui import Ui_Form
class MyApp(QtGui.QWidget):
def __init__(self,parent=None):
super(MyApp, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton_test.clicked.connect(self.click_button)
def click_button(self):
print "Printing layouts and widgets"
for child in self.ui.gridLayout.children():
print "child_name:%s - type:%s" %(child.objectName(),type(child))
for grand_child in child.children():
print "gc_name:%s - type:%s" %(grand_child.objectName(),type(grand_child))
# never prints
for ggc in grand_child.children():
print "ggc_name:%s - type:%s" %(ggc.objectName(),type(ggc))
qreg = QRegExp('.+')
widgets = grand_child.findChildren(QLabel,qreg)
print widgets # prints None
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyApp()
myapp.show()
sys.exit(app.exec_())
And here's mygui.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mygui.ui'
#
# Created: Thu Mar 28 14:07:12 2013
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(454, 323)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout_2.addWidget(self.label_2)
self.label = QtGui.QLabel(Form)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout_2.addWidget(self.label)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.textEdit = QtGui.QTextEdit(Form)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.verticalLayout.addWidget(self.textEdit)
self.pushButton_test = QtGui.QPushButton(Form)
self.pushButton_test.setObjectName(_fromUtf8("pushButton_test"))
self.verticalLayout.addWidget(self.pushButton_test)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_test.setText(QtGui.QApplication.translate("Form", "Print All Widgets", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
I appreciate your help and input.
Paul
I realized that all the widgets can be accessed from self. Here is the final code I used:
wtypes = [QPushButton,QLabel,QTextEdit]
qreg = QRegExp(r'.*')
mywidgets = {}
for t in wtypes:
mywidgets[t] = self.findChildren(t,qreg)
for button in mywidgets[QPushButton]:
print "button:", button.objectName()
for label in mywidgets[QLabel]:
print "label:", label.objectName()
Related
I am trying to embed two custom widgets into the pages of a stacked widget on a dialog page. I have mocked up my problem using the main script dialog.py which has a stacked widget with two pages promoting widget_1_UI from widget_1.py and widget_2_UI from widget_2.py to each page, respectively. It doesn't throw any errors when run but also doesn't show the content from the two modules in the stacked widget.
dialog.py
from PyQt5 import QtCore, QtGui, QtWidgets
from widget_1 import widget_1_UI
from widget_2 import widget_2_UI
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setWindowTitle("Dialog")
Dialog.resize(640, 480)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.stackedWidget = QtWidgets.QStackedWidget(Dialog)
self.page_1 = widget_1_UI()
self.stackedWidget.addWidget(self.page_1)
self.page_2 = widget_2_UI()
self.stackedWidget.addWidget(self.page_2)
self.verticalLayout.addWidget(self.stackedWidget)
self.btn_nextPage = QtWidgets.QPushButton(Dialog)
self.btn_nextPage.setText("PushButton")
self.verticalLayout.addWidget(self.btn_nextPage)
self.btn_nextPage.clicked.connect(self.nextPage)
def nextPage(self):
pageNum = self.stackedWidget.currentIndex()
if pageNum == 0:
self.stackedWidget.setCurrentIndex(1)
else:
self.stackedWidget.setCurrentIndex(0)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
widget_1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class widget_1_UI(QtWidgets.QWidget):
def setupUi(self, Form):
Form.resize(614, 411)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.label = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(26)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setText("AY YO BBY GRILL")
self.verticalLayout.addWidget(self.label)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setText("Dis one do nuffin")
self.verticalLayout.addWidget(self.pushButton)
widget_2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class widget_2_UI(QtWidgets.QWidget):
def setupUi(self, Form):
Form.resize(614, 411)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.label = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(26)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setText("HOW YOU DOIN")
self.verticalLayout.addWidget(self.label)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setText("Dis do nuffin 2")
self.verticalLayout.addWidget(self.pushButton)
Figured it out! In dialog.py widget_1_UI() is set as the promoted widget for the first page of the stacked widget but there is nothing to tell it what should be inside it. Adding self.page_1.setupUi(self.page_1) does just that.
self.page_1 = widget_1_UI()
self.page_1.setupUi(self.page_1)
self.stackedWidget.addWidget(self.page_1)
The same is applied to widget_2_UI.
I am trying to use this solution by #eyllanesc:
pyqt qtabwidget horizontal tab and horizontal text in QtDesigner.
My main window has a QStackedWidget in which one of the pages is the page with horizontal tabs. I used Qt designer to create a dialog with a tab widget, and set the tabPosition to West. Then I promoted the TabWidget like shown in the original post.
This is what the resulting window looks like. There are no outlines around the tabs, and the tabs are not clickable:
This is my main code:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from tabs import TabsUi_Dialog
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.mainMenuWidget = MainStack(self)
self.setCentralWidget(self.mainMenuWidget)
self.show()
class MainStack(QWidget):
def __init__(self,parent=None):
super(MainStack,self).__init__(parent)
self.initUI()
def initUI(self):
layout = QVBoxLayout(self)
self.stack = QStackedWidget(parent=self)
self.tabPage = TabPage()
#Add Pages to Stack
self.stack.addWidget(self.tabPage)
#Add Stack to Layout
self.stack.setCurrentWidget(self.tabPage)
layout.addWidget(self.stack)
class TabPage(QWidget):
def __init__(self,parent=None):
super(TabPage,self).__init__(parent)
self.tabs = TabsUi_Dialog()
self.tabs.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
ret = app.exec_()
sys.exit( ret )
And this is the tabs.py file created after converting the Qt Designer UI file with py-uic:
from PySide import QtCore, QtGui
class TabsUi_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.verticalLayout = QtGui.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.tabWidget = TabWidget(Dialog)
self.tabWidget.setTabPosition(QtGui.QTabWidget.West)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtGui.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.verticalLayout.addWidget(self.tabWidget)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), QtGui.QApplication.translate("Dialog", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), QtGui.QApplication.translate("Dialog", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))
from tabwidget import TabWidget
For example When I open the app to show me one form, when I click the button show second form that will contain the data entry fields
Home
Second form
There are several ways to achieve what you want. Easiest (not cleanest) is to group the widgets in your "forms" into containers (QFrame, QGroupbox, etc) and connect the triggered signal from your actions into suitable slots that hide/show these containers. Below you can see an example (PySide) of this, on which I'm using two QFrames in order to separate your "forms". The layout behaviour can be modified easily.
The form file (compiled from the designer's ui file):
# Form implementation generated from reading ui file 'example.ui'
#
# Created: Tue Apr 10 13:18:07 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_Example(object):
def setupUi(self, Example):
Example.setObjectName("Example")
Example.resize(800, 770)
self.centralwidget = QtGui.QWidget(Example)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.fr_one = QtGui.QFrame(self.centralwidget)
self.fr_one.setFrameShape(QtGui.QFrame.StyledPanel)
self.fr_one.setFrameShadow(QtGui.QFrame.Raised)
self.fr_one.setObjectName("fr_one")
self.verticalLayout_2 = QtGui.QVBoxLayout(self.fr_one)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.bt_quit = QtGui.QPushButton(self.fr_one)
self.bt_quit.setObjectName("bt_quit")
self.verticalLayout_2.addWidget(self.bt_quit)
self.cb_selection = QtGui.QComboBox(self.fr_one)
self.cb_selection.setObjectName("cb_selection")
self.verticalLayout_2.addWidget(self.cb_selection)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout_2.addItem(spacerItem)
self.gridLayout.addWidget(self.fr_one, 0, 0, 1, 1)
self.fr_two = QtGui.QFrame(self.centralwidget)
self.fr_two.setFrameShape(QtGui.QFrame.StyledPanel)
self.fr_two.setFrameShadow(QtGui.QFrame.Raised)
self.fr_two.setObjectName("fr_two")
self.verticalLayout_5 = QtGui.QVBoxLayout(self.fr_two)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtGui.QLabel(self.fr_two)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.le_fname = QtGui.QLineEdit(self.fr_two)
self.le_fname.setObjectName("le_fname")
self.verticalLayout.addWidget(self.le_fname)
self.verticalLayout_5.addLayout(self.verticalLayout)
self.verticalLayout_3 = QtGui.QVBoxLayout()
self.verticalLayout_3.setSpacing(0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.label_2 = QtGui.QLabel(self.fr_two)
self.label_2.setObjectName("label_2")
self.verticalLayout_3.addWidget(self.label_2)
self.le_sname = QtGui.QLineEdit(self.fr_two)
self.le_sname.setObjectName("le_sname")
self.verticalLayout_3.addWidget(self.le_sname)
self.verticalLayout_5.addLayout(self.verticalLayout_3)
self.verticalLayout_4 = QtGui.QVBoxLayout()
self.verticalLayout_4.setSpacing(0)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.label_3 = QtGui.QLabel(self.fr_two)
self.label_3.setObjectName("label_3")
self.verticalLayout_4.addWidget(self.label_3)
self.le_address = QtGui.QLineEdit(self.fr_two)
self.le_address.setObjectName("le_address")
self.verticalLayout_4.addWidget(self.le_address)
self.verticalLayout_5.addLayout(self.verticalLayout_4)
spacerItem1 = QtGui.QSpacerItem(20, 267, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout_5.addItem(spacerItem1)
self.gridLayout.addWidget(self.fr_two, 0, 1, 1, 1)
Example.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(Example)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 47))
self.menubar.setObjectName("menubar")
Example.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(Example)
self.statusbar.setObjectName("statusbar")
Example.setStatusBar(self.statusbar)
self.toolBar = QtGui.QToolBar(Example)
self.toolBar.setObjectName("toolBar")
Example.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actionAction1 = QtGui.QAction(Example)
self.actionAction1.setObjectName("actionAction1")
self.actionAction2 = QtGui.QAction(Example)
self.actionAction2.setObjectName("actionAction2")
self.toolBar.addAction(self.actionAction1)
self.toolBar.addAction(self.actionAction2)
self.retranslateUi(Example)
QtCore.QMetaObject.connectSlotsByName(Example)
def retranslateUi(self, Example):
Example.setWindowTitle(QtGui.QApplication.translate("Example", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.bt_quit.setText(QtGui.QApplication.translate("Example", "Quit", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Example", "First name", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Example", "Second name", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("Example", "Address", None, QtGui.QApplication.UnicodeUTF8))
self.toolBar.setWindowTitle(QtGui.QApplication.translate("Example", "toolBar", None, QtGui.QApplication.UnicodeUTF8))
self.actionAction1.setText(QtGui.QApplication.translate("Example", "action1", None, QtGui.QApplication.UnicodeUTF8))
self.actionAction2.setText(QtGui.QApplication.translate("Example", "action2", None, QtGui.QApplication.UnicodeUTF8))
The main file:
from PySide import QtGui, QtCore
from _example import Ui_Example
class Example(QtGui.QMainWindow):
def __init__(self, parent = None):
super(Example, self).__init__(parent)
self.ui = Ui_Example()
self.ui.setupUi(self)
self.ui.fr_two.setVisible(False)
self.ui.cb_selection.addItem("motif")
# here are the connections of the corresponding actions on the QToolBar
self.ui.actionAction1.triggered.connect(self._changeView1)
self.ui.actionAction2.triggered.connect(self._changeView2)
def _changeView1(self):
# fr_one is the first frame (the one that contains the 'quit' button and the QCombobox)
# fr_two is the second frame (the one that contains the QLineEdits)
self.ui.fr_one.setVisible(True)
self.ui.fr_two.setVisible(False)
def _changeView2(self):
self.ui.fr_one.setVisible(False)
self.ui.fr_two.setVisible(True)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
I would like to put a cursor in a blank line edit box without having to click on it.
I looked through the Reference here
http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/doc/html/qlineedit.html#selectionStart
I tried calling
QLineEdit.home(True)
But this did not select the lineEdit so to say.
here is a watered down version of the code
from PyQt4 import QtCore, QtGui
import sys
import os
import os.path
class Ui_Form1(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, Form):
#init stuff
Form.setObjectName("Form")
Form.resize(794, 538)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.hLayout = QtGui.QHBoxLayout(Form)
self.hLayout.setObjectName("hLayout")
self.vLayout = QtGui.QVBoxLayout(Form)
self.vLayout.setObjectName("vLayout")
#label for information
self.gridLayout.addLayout(self.hLayout, 0, 0)
self.hLayout.addLayout(self.vLayout, 0)
self.label = QtGui.QLabel(Form)
self.label.setObjectName("label")
#label pixmap
self.label2 = QtGui.QLabel(Form)
self.label2.setObjectName("label")
#line edit
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.lineEdit, 3,0)
self.list = QtGui.QListWidget(self)
self.list.setObjectName("outlist")
self.list.setMinimumHeight(150)
self.vLayout.addWidget(self.list, 1)
self.hLayout.addWidget(self.label, 1)
self.vLayout.addWidget(self.label2, 0)
#self.hLayout.addWidget(self.label2, 0)
self.label2.setText('line edit')
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setText('Picture would go here')
self.label2.setText('line edit')
self.list.addItem('cursor will disappear when this is pressed')
#press enter to update pic
#self.lineEdit.returnPressed.connect(self.update_pic)
#####sh
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = Ui_Form1()
ex.show()
sys.exit(app.exec_())
self.lineEdit.setFocus(QtCore.Qt.StrongFocus)
In PyQt5:
self.lineEdit.setFocus()
Im new at Python, QT4 and pyqt and I can't get the widgets to change using setCurrentIndex. I'm sure I am not using it correctly, but here's my initial code. First is the pyqt code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Thu May 5 17:15:28 2016
# by: PyQt4 UI code generator 4.9.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(400, 300)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.stackedWidget = QtGui.QStackedWidget(self.centralWidget)
self.stackedWidget.setGeometry(QtCore.QRect(-41, -41, 451, 311))
self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))
self.page = QtGui.QWidget()
self.page.setObjectName(_fromUtf8("page"))
self.label = QtGui.QLabel(self.page)
self.label.setGeometry(QtCore.QRect(180, 70, 50, 16))
self.label.setObjectName(_fromUtf8("label"))
self.pushButton = QtGui.QPushButton(self.page)
self.pushButton.setGeometry(QtCore.QRect(170, 140, 80, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.stackedWidget.addWidget(self.page)
self.page_2 = QtGui.QWidget()
self.page_2.setObjectName(_fromUtf8("page_2"))
self.label_2 = QtGui.QLabel(self.page_2)
self.label_2.setGeometry(QtCore.QRect(200, 90, 50, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.stackedWidget.addWidget(self.page_2)
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
self.stackedWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Page 1", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Go to 2", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Page 2", None, QtGui.QApplication.UnicodeUTF8))
And here is the code I'm trying:
import sys
from form import *
class MyDialog(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.Change)
def Change(self):
self.stackedWidget.setCurrentIndex(1)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyDialog()
myapp.show()
sys.exit(app.exec_())
When I push my button, I get the error"'MyDialog' object has no attribute 'stackedWidget'.
I'm sure this is easy to the experienced programmer.
The error message means exactly what it says: MyDialog doesn't create a member variable named self.stackedWidget, but it tries to access such a variable in Change, so an AttributeError is raised when you click the button. The variable you want is a member of self.ui, which is an instance of UI_MainWindow.
The solution is to change the method Change to:
def Change(self):
self.ui.stackedWidget.setCurrentIndex(1)