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)
Related
I tried to design a very basic GUI app that shows the entered height on a dialog, but after I press the ‘OK’ button on the main window, the whole program crashes and the process finishes with this exit code:
Process finished with exit code -1073740791 (0xC0000409)
Here’s the full code for the app, the UI files are below:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import *
class My_Dialog(QDialog):
def __init__(self):
super(My_Dialog, self).__init__()
loadUi("dialog.ui", self)
self.mid_label.setText(My_Window.mid_label_nexttext)
class My_Window(QMainWindow):
def __init__(self):
super(My_Window, self).__init__()
loadUi("mainwindow.ui", self)
self.mid_label_nexttext = None
self.height_selecter_spinbox.textChanged.connect(lambda x: self.spin_changed(x))
self.pushButton.clicked.connect(self.onMyPushButtonClick)
def onMyPushButtonClick(self):
dlg = My_Dialog()
if dlg.exec_():
print("Success!")
else:
print("Cancel!")
def spin_changed(self, s):
self.mid_label_nexttext = s
self.update()
def main():
app = QApplication(sys.argv)
window = My_Window()
window.show()
app.exec_()
if __name__ == "__main__":
main()
The main window’s UI:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(513, 171)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(310, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 40, 209, 29))
self.layoutWidget.setObjectName("layoutWidget")
self.layout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setObjectName("layout")
self.label_firstpart = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(15)
self.label_firstpart.setFont(font)
self.label_firstpart.setObjectName("label_firstpart")
self.layout.addWidget(self.label_firstpart)
self.height_selecter_spinbox = QtWidgets.QSpinBox(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(13)
self.height_selecter_spinbox.setFont(font)
self.height_selecter_spinbox.setMinimum(100)
self.height_selecter_spinbox.setMaximum(250)
self.height_selecter_spinbox.setProperty("value", 175)
self.height_selecter_spinbox.setObjectName("height_selecter_spinbox")
self.layout.addWidget(self.height_selecter_spinbox)
self.label_lastpart = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(15)
self.label_lastpart.setFont(font)
self.label_lastpart.setObjectName("label_lastpart")
self.layout.addWidget(self.label_lastpart)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 513, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "OK"))
self.label_firstpart.setText(_translate("MainWindow", "My height is "))
self.label_lastpart.setText(_translate("MainWindow", "cm."))
The dialog’s UI:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.dialog_buttonbox = QtWidgets.QDialogButtonBox(Dialog)
self.dialog_buttonbox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.dialog_buttonbox.setOrientation(QtCore.Qt.Horizontal)
self.dialog_buttonbox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.dialog_buttonbox.setObjectName("dialog_buttonbox")
self.widget = QtWidgets.QWidget(Dialog)
self.widget.setGeometry(QtCore.QRect(80, 90, 151, 41))
self.widget.setObjectName("widget")
self.dialog_layout = QtWidgets.QHBoxLayout(self.widget)
self.dialog_layout.setContentsMargins(0, 0, 0, 0)
self.dialog_layout.setObjectName("dialog_layout")
self.left_label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(12)
self.left_label.setFont(font)
self.left_label.setObjectName("left_label")
self.dialog_layout.addWidget(self.left_label)
self.mid_label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(12)
self.mid_label.setFont(font)
self.mid_label.setObjectName("mid_label")
self.dialog_layout.addWidget(self.mid_label)
self.right_label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(12)
self.right_label.setFont(font)
self.right_label.setObjectName("right_label")
self.dialog_layout.addWidget(self.right_label)
self.retranslateUi(Dialog)
self.dialog_buttonbox.accepted.connect(Dialog.accept)
self.dialog_buttonbox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.left_label.setText(_translate("Dialog", "You are"))
self.mid_label.setText(_translate("Dialog", "100"))
self.right_label.setText(_translate("Dialog", "cm tall."))
I would appreciate some help on fixing this error, and on why it occured.
You are trying to access an attribute that does not exist:
self.mid_label.setText(My_Window.mid_label_nexttext)
My_Window is a class, while mid_label_nexttext was assigned to the instance of that class (self is always the reference to the current instance).
If you want to set the text for that label from a "parent" window, you either add an extra argument to the __init__ that allows to get the text, or you set it from the main window.
Use the text as init argument
class My_Dialog(QDialog):
def __init__(self, text):
super(My_Dialog, self).__init__()
loadUi("dialog.ui", self)
# ensure that "text" is a valid string, you can't use setText(None)
if text:
self.mid_label.setText(text)
class My_Window(QMainWindow):
# ...
def onMyPushButtonClick(self):
dlg = My_Dialog(self.mid_label_nexttext)
# ...
Set the text from the parent
class My_Dialog(QDialog):
def __init__(self):
super(My_Dialog, self).__init__()
loadUi("dialog.ui", self)
# NO setText() here!!!
class My_Window(QMainWindow):
# ...
def onMyPushButtonClick(self):
dlg = My_Dialog()
if self.mid_label_nexttext:
dlg.mid_label.setText(self.mid_label_nexttext)
# ...
Note that the first method is usually better, mostly for modularity reasons: let's say that you create that dialog from different classes in different situations, whenever you need to change the object name of the label (or the whole interface structure) you can easily change its reference in the dialog subclass, otherwise you'll need to change every reference in your code.
Note: the call to self.update() is useless; if you want to update the label on the dialog whenever the spinbox value is changed, you need to directly access the label (like in the last example) or use signals. Also, you don't need to use lambda if you're using the same argument parameter, just connect to the function.
I'm pretty new to python and especially to pyqt4 but I would like to learn it right.
Therefore I want to build my new learning project in three different files.
One main.py file in which I call everything. One by the QDesigner generated UI file (Main_Ui.py) and one Main_Functions.py file in which I would like to keep all my functions.
My first question would therefore be whether this is a "good programming style" or should I do it in a different way?
During the implementation of my idea I got stuck with calling the function. The console says "TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'".
Could you please help me and tell me what I did wrong as I googled now nearly all day long and I could not find anything that would fit or I didn't understood it. It would be really great and would help me a lot learning how to do it right :)
Thank you very much :)
Here is my examplary code so far:
Main.py
import sys
from PyQt4 import QtGui, QtCore
from Main_Ui import Ui_MainWindow
import Main_Functions as f
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn1.clicked.connect(f.download(self.ui))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
Main_Functions.py
def download(window):
window.completed = 0
while window.completed < 100:
window.completed += 0.0001
window.progress.setValue(window.completed)
And the probably not so interesting Main_Ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(1000, 848)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(210, 150, 2, 2))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(50, 120, 256, 192))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(1)
self.tableWidget.setRowCount(1)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
self.progress = QtGui.QProgressBar(self.centralwidget)
self.progress.setGeometry(QtCore.QRect(180, 550, 118, 23))
self.progress.setProperty("value", 24)
self.progress.setObjectName(_fromUtf8("progress"))
self.btn1 = QtGui.QPushButton(self.centralwidget)
self.btn1.setGeometry(QtCore.QRect(490, 200, 75, 23))
self.btn1.setObjectName(_fromUtf8("btn1"))
self.lbl1 = QtGui.QLabel(self.centralwidget)
self.lbl1.setGeometry(QtCore.QRect(400, 200, 46, 13))
self.lbl1.setObjectName(_fromUtf8("lbl1"))
self.txt1 = QtGui.QLineEdit(self.centralwidget)
self.txt1.setGeometry(QtCore.QRect(330, 160, 113, 20))
self.txt1.setObjectName(_fromUtf8("txt1"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1000, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuDatei = QtGui.QMenu(self.menubar)
self.menuDatei.setObjectName(_fromUtf8("menuDatei"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionSchlie_en = QtGui.QAction(MainWindow)
self.actionSchlie_en.setObjectName(_fromUtf8("actionSchlie_en"))
self.menuDatei.addAction(self.actionSchlie_en)
self.menubar.addAction(self.menuDatei.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "T", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
self.btn1.setText(_translate("MainWindow", "PushButton", None))
self.lbl1.setText(_translate("MainWindow", "TextLabel", None))
self.menuDatei.setTitle(_translate("MainWindow", "Datei", None))
self.actionSchlie_en.setText(_translate("MainWindow", "Schließen", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem is your connection
self.ui.btn1.clicked.connect(f.download(self.ui))
You need to pass a callback to the connect function. Here, you're actually calling the download function (which returns None) and passing the results to connect.
You need to do this instead
self.ui.btn1.clicked.connect(lambda: f.download(self.ui))
It does often make sense to separate non-gui business logic out into separate modules. This allows them to be re-used in other modules and GUI's. However, in your case, you're performing GUI modification in code outside your GUI class. That's generally not a very good design and it would be better for you to define those functions inside your MainWindow class.
I'm new on stackoverflow.
I have a MainWindow on PyQt, I want click a button and open a QFileDialog for choose a file. The problem is: If I use a MainWindow, QFileDialog don't run. If I use a Dialog, QFileDialog run.
This is my code for the MainWindow.
import sys
from Import_fsa import import_fsa
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from Vannucci_Gemignani import Ui_MainWindow
class GUI_fsa(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
def Browse(self):
fname=QFileDialog.getOpenFileName()
self.lineEdit.setText(fname)
data_set=import_fsa(fname)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is the code for Dialog. Here I write the code in the .py generate using pyuic4 (QTDesigner)
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from ab1 import ABIFReader
import pylab
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(508, 363)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(10, 40, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(110, 40, 361, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('clicked()'), self.selectFile)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
def selectFile(self):
fname=QFileDialog.getOpenFileName()
self.lineEdit.setText(fname)
reader=ABIFReader(fname)
dati=reader.getData('DATA',1)
pylab.plot(dati)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
This is Vannucci_Gemignani.py:
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(1445, 744)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 771, 83))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setMargin(0)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.Button_Browse = QtGui.QPushButton(self.gridLayoutWidget)
self.Button_Browse.setObjectName(_fromUtf8("Button_Browse"))
self.gridLayout.addWidget(self.Button_Browse, 0, 0, 1, 1)
self.Button_Plot = QtGui.QPushButton(self.gridLayoutWidget)
self.Button_Plot.setObjectName(_fromUtf8("Button_Plot"))
self.gridLayout.addWidget(self.Button_Plot, 1, 0, 1, 1)
self.lineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
self.Button_Clear = QtGui.QPushButton(self.gridLayoutWidget)
self.Button_Clear.setObjectName(_fromUtf8("Button_Clear"))
self.gridLayout.addWidget(self.Button_Clear, 2, 0, 1, 1)
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 130, 61, 31))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(700, 130, 61, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 430, 61, 31))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(700, 430, 61, 31))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.widget = matplotlibWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 130, 571, 251))
self.widget.setObjectName(_fromUtf8("widget"))
self.widget_2 = matplotlibWidget2(self.centralwidget)
self.widget_2.setGeometry(QtCore.QRect(770, 130, 571, 251))
self.widget_2.setObjectName(_fromUtf8("widget_2"))
self.widget_3 = matplotlibWidget3(self.centralwidget)
self.widget_3.setGeometry(QtCore.QRect(90, 430, 571, 251))
self.widget_3.setObjectName(_fromUtf8("widget_3"))
self.widget_4 = matplotlibWidget4(self.centralwidget)
self.widget_4.setGeometry(QtCore.QRect(770, 430, 571, 251))
self.widget_4.setObjectName(_fromUtf8("widget_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1445, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "fsa Plotter", None, QtGui.QApplication.UnicodeUTF8))
self.Button_Browse.setText(QtGui.QApplication.translate("MainWindow", "Browse", None, QtGui.QApplication.UnicodeUTF8))
self.Button_Plot.setText(QtGui.QApplication.translate("MainWindow", "Plot", None, QtGui.QApplication.UnicodeUTF8))
self.Button_Clear.setText(QtGui.QApplication.translate("MainWindow", "Clear", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Channel 1", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Channel 2", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Channel 3", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Channel 4", None, QtGui.QApplication.UnicodeUTF8))
self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
from matplotlibwidget import matplotlibWidget
from matplotlibwidget3 import matplotlibWidget3
from matplotlibwidget2 import matplotlibWidget2
from matplotlibwidget4 import matplotlibWidget4
The file-dialog doesn't show, because you did not create an instance of your GUI_fsa class.
To fix that, make the following changes:
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = GUI_fsa()
# the next two lines aren't needed
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
There is another problem you will also need to fix, in your Browse method. The widgets from Ui_MainWindow need to accessed via self.ui. So also make the following changes:
def Browse(self):
fname = QFileDialog.getOpenFileName()
self.ui.lineEdit.setText(fname)
...
One final suggestion: avoid using the old-style syntax for connecting signals, and use the new-style syntax instead:
# don't do this:
# QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
# do this!
self.ui.Button_Browse.clicked.connect(self.Browse)
try changing the lines:
def Browse(self):
fname=QFileDialog.getOpenFileName()
by
from PyQt4.QtCore import QObject, pyqtSlot
#pyqtSlot()
def on_Button_Browse_clicked(self):
fname=QFileDialog.getOpenFileName()
and remove the line
QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
Explanation:
When you're using QtDesigner and pyuic4 you don't need to connect the events by your self. The pyuic4 generated class take care of that for you. The only you have to do is write your class methods correctly. For example: IF you have a button called "button_1" and you want perform some action when clicked, you just create a method like this:
class Example(QMainWindow):
...
def on_button_1_clicked(self):
pass
the key here is on_button_1_clicked which stands for: on_<widget-name>_<signal> with button_1 as widget name and clicked as signal
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()
I'd like to create a GUI app with some buttons and want to handle button press events in same way (i.e. single event handle for all key presses). But I don't understand how can button_hanlder understand which button was pressed.
Here is code
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(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(350, 190, 98, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(240, 220, 98, 27))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(350, 250, 98, 27))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(460, 220, 98, 27))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("pressed()")), self.button_handler)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Up", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "Right", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_3.setText(QtGui.QApplication.translate("MainWindow", "Down", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_4.setText(QtGui.QApplication.translate("MainWindow", "Left", None, QtGui.QApplication.UnicodeUTF8))
self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
def button_handler(self):
# Which button was pressed?
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I want in button_handler determine which button was pressed, for example by button caption.
The QButtonGroup is designed for this. It will work with any kind of button which inherits QAbstractButton, such as QPushButton, QRadioButton, QCheckBox, etc.
Just add all the buttons to the button-group using addButton and then connect the buttonClicked signal to a handler.
NB: It may be a good idea to put all the buttons inside a container widget, such as a QGroupBox, QFrame or just a plain QWidget. This will allow you to loop over the container's children and automatically add all the buttons to a button-group.
Example Code:
btngroup.py:
from PyQt4 import QtGui, QtCore
from btngroup_ui import Ui_ButtonGroup
class Window(QtGui.QWidget, Ui_ButtonGroup):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.buttonGroup = QtGui.QButtonGroup(self)
for button in self.buttonBox.findChildren(QtGui.QAbstractButton):
self.buttonGroup.addButton(button)
self.buttonGroup.buttonClicked.connect(self.handleButtonClicked)
def handleButtonClicked(self, button):
print('"%s" was clicked' % button.text())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
btngroup_ui.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'btngroup.ui'
#
# Created: Wed Nov 21 17:50:42 2012
# by: PyQt4 UI code generator 4.9.5
#
# 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_ButtonGroup(object):
def setupUi(self, ButtonGroup):
ButtonGroup.setObjectName(_fromUtf8("ButtonGroup"))
ButtonGroup.resize(240, 167)
self.horizontalLayout = QtGui.QHBoxLayout(ButtonGroup)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.buttonBox = QtGui.QWidget(ButtonGroup)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.verticalLayout = QtGui.QVBoxLayout(self.buttonBox)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.buttonA = QtGui.QPushButton(self.buttonBox)
self.buttonA.setObjectName(_fromUtf8("buttonA"))
self.verticalLayout.addWidget(self.buttonA)
self.buttonB = QtGui.QPushButton(self.buttonBox)
self.buttonB.setObjectName(_fromUtf8("buttonB"))
self.verticalLayout.addWidget(self.buttonB)
self.buttonC = QtGui.QPushButton(self.buttonBox)
self.buttonC.setObjectName(_fromUtf8("buttonC"))
self.verticalLayout.addWidget(self.buttonC)
self.buttonD = QtGui.QPushButton(self.buttonBox)
self.buttonD.setObjectName(_fromUtf8("buttonD"))
self.verticalLayout.addWidget(self.buttonD)
self.horizontalLayout.addWidget(self.buttonBox)
self.retranslateUi(ButtonGroup)
QtCore.QMetaObject.connectSlotsByName(ButtonGroup)
def retranslateUi(self, ButtonGroup):
ButtonGroup.setWindowTitle(QtGui.QApplication.translate("ButtonGroup", "Button Group", None, QtGui.QApplication.UnicodeUTF8))
self.buttonA.setText(QtGui.QApplication.translate("ButtonGroup", "Button A", None, QtGui.QApplication.UnicodeUTF8))
self.buttonB.setText(QtGui.QApplication.translate("ButtonGroup", "Button B", None, QtGui.QApplication.UnicodeUTF8))
self.buttonC.setText(QtGui.QApplication.translate("ButtonGroup", "Button C", None, QtGui.QApplication.UnicodeUTF8))
self.buttonD.setText(QtGui.QApplication.translate("ButtonGroup", "Button D", None, QtGui.QApplication.UnicodeUTF8))
You probably want to use a QtCore.QSignalMapper.
See http://srinikom.github.com/pyside-docs/PySide/QtCore/QSignalMapper.html for a detailed explanation.
Basically, it allows you to associate a string value to each connection between a button and the slot, so you can check from which button the click comes by analyzing the contents of the string parameter of the signal.