I have been trying to develop a software in PyQt5.
I have 3 windows : 1) login
2) dashboard
3) appointments
Now I can navigate properly from login -> dashboard -> appointments perfectly.
But when I press pushbutton for back from appointments, it doesn't come back to dashboard.
But if I go from dashboard->appointments , the pushbutton works for 1 time.
I have removed irrelevant lines from the code to make analysis simpler.
login window class :
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
def retranslateUi(self, MainWindow):
self.pushButton.clicked.connect(self.next)
def next(self):
ui = Ui_MainWindow2()
ui.setupUi(self.MainWindow)
self.MainWindow.show()
dashboard window class :
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
def retranslateUi(self, MainWindow):
self.pushButton.clicked.connect(self.next)
def next(self):
ui = Ui_MainWindow3()
ui.setupUi(self.MainWindow)
self.MainWindow.show()
appointment window class :
class Ui_MainWindow3(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
def retranslateUi(self, MainWindow):
self.pushButton.clicked.connect(self.back)
def back(self):
ui = Ui_MainWindow2()
ui.setupUi(self.MainWindow)
self.MainWindow.show()
Application starts from file login.py which has :
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The forward navigation always work, the backward one keeps failing...
Any help or suggestions will be very welcome...
Turns out I was writing the next or back function slightly differently than it should have been. The next and back functions should have been like this :
def next(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2()
self.ui.setupUi(self.window)
self.oldWindow.hide()
self.window.show()
where self.oldWindow is the reference to the original window of the class enclosing the function next. This is also same for the back function.
Related
This question already has answers here:
Why does pyqtSlot decorator cause "TypeError: connect() failed"?
(2 answers)
Closed 3 years ago.
I want to drag &drop a file to a listwidget and when itemactivated, plaintextedit opens and readAll lines in file. But returned with error: "connect() failed between itemActivated(QListWidgetItem*) and read_txt()"
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(873, 663)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.listWidget = listWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(150, 330, 256, 192))
self.listWidget.setObjectName("listWidget")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(440, 200, 104, 87))
self.plainTextEdit.setObjectName("plainTextEdit")
self.listWidget.itemActivated.connect(self.read_txt)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#QtCore.pyqtSlot("QListWidgetItem*")
def read_txt(self,links):
file = QtCore.QFile(links.text())
if file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
self.plainTextEdit.setPlainText(file.readAll().data().decode())
class listWidget(QtWidgets.QListWidget):
def __init__(self, parent):
super().__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.acceptProposedAction()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
for url in event.mimeData().urls():
links=QtWidgets.QListWidgetItem(url.toLocalFile())
self.addItem(links)
event.acceptProposedAction()
if __name__ == "__main__":
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
Drag and drop works fine but could not connect QListWidgetItem with readtxt method in setupUi. What is the problem ??
The problem is that the #pyqtSlot decorator can only be used in a class that inherits from QObject. You can connect signals to any callable object so the easiest way is to just omit the decorator, but if you do need it for one reason or another, you could subclass QMainWindow and define the slot in there, e.g.
class MyMainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.listWidget.itemActivated.connect(self.read_txt)
#QtCore.pyqtSlot("QListWidgetItem*")
def read_txt(self, links):
file = QtCore.QFile(links.text())
if file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
self.ui.plainTextEdit.setPlainText(file.readAll().data().decode())
if __name__ == '__main__':
...
mainWindow = MyMainWindow()
mainWindow.show()
...
In my program there is a QGroupBox displayed that has many QPushButton's within it. During the execution of the program, the user can click a button outside of the QGroupBox and all of the buttons within it will be removed or hidden. Problem is that I can't seem to find a way to do this to the buttons directly or by clearing the QGroupBox.
I have already tried deleteLater on the buttons but that didn't work. I then tried clearing the layout of the QGroupBox but that didn't work either. Here is some code I just wrote up that has a the same problem:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import random
class UI_Dialog(object):
def addButtons(self,looping):
# Code to remove the previous QPushButton's goes here.
placement = -100
for i in range(looping):
currentName = 'btn' + str(i)
placement = placement + 110
self.btnB = QtWidgets.QPushButton(self.groupBox)
self.btnB.setGeometry(QtCore.QRect(10+placement, 30+placement, 100, 100))
self.btnB.show()
self.btnB.setObjectName(currentName)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1300, 800)
self.btnA = QtWidgets.QPushButton(Dialog)
self.btnA.setGeometry(QtCore.QRect(10, 80, 101, 131))
self.btnA.setObjectName("btn1")
self.btnA.clicked.connect(self.pushed)
self.formLayout = QtWidgets.QFormLayout()
self.groupBox = QtWidgets.QGroupBox("Results")
self.groupBox.setLayout(self.formLayout)
self.resultScrollArea = QtWidgets.QScrollArea(Dialog)
self.resultScrollArea.setWidget(self.groupBox)
self.resultScrollArea.setGeometry(QtCore.QRect(20, 220, 1011, 531))
self.resultScrollArea.setWidgetResizable(True)
self.resultScrollArea.setObjectName("resultScrollArea")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def pushed(self):
unkownLength = random.randint(1,20)
self.addButtons(unkownLength)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Example Program"))
self.btnA.setText(_translate("Dialog", "Push Button"))
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = UI_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
There are things here that probably don't make sense like the window size, the multiple function calls or it being a Dialog window instead of a QMainWindow. However, in the context of the actual program they do make sense so just ignore that, I know it's inefficient. Also the whole point of the unkownLength variable is to emulate that in the actual program, the number of buttons generated will be determined by user input. The buttons must also not be there at the start so that is why they're created with a button click. When the button is clicked again it should remove or hide all the buttons it created before. Any ideas?
Taking advantage that the buttons are children of the QGroupBox we can get the buttons using findChildren() to use deleteLater():
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import random
class UI_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1300, 800)
self.btnA = QtWidgets.QPushButton(Dialog)
self.btnA.setGeometry(QtCore.QRect(10, 80, 101, 131))
self.btnA.setObjectName("btn1")
self.formLayout = QtWidgets.QFormLayout()
self.groupBox = QtWidgets.QGroupBox("Results")
self.groupBox.setLayout(self.formLayout)
self.resultScrollArea = QtWidgets.QScrollArea(Dialog)
self.resultScrollArea.setWidget(self.groupBox)
self.resultScrollArea.setGeometry(QtCore.QRect(20, 220, 1011, 531))
self.resultScrollArea.setWidgetResizable(True)
self.resultScrollArea.setObjectName("resultScrollArea")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Example Program"))
self.btnA.setText(_translate("Dialog", "Push Button"))
class Dialog(QtWidgets.QDialog, UI_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.btnA.clicked.connect(self.pushed)
#QtCore.pyqtSlot()
def pushed(self):
unkownLength = random.randint(1, 20)
self.addButtons(unkownLength)
def addButtons(self, looping):
for button in self.groupBox.findChildren(QtWidgets.QPushButton):
button.deleteLater()
placement = -100
pos = QtCore.QPoint(20, 40)
for i in range(looping):
currentName = "btn" + str(i)
self.btnB = QtWidgets.QPushButton(
self.groupBox, objectName=currentName
)
self.btnB.setGeometry(QtCore.QRect(pos, QtCore.QSize(100, 100)))
pos += QtCore.QPoint(110, 110)
self.btnB.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())
I need to make a UI for a class which makes a number of calculations from a set of variables. The code roughly looks like this:
class MainWindow:
def __init__(self):
pulse = Pulse()
self.var1_lineEdit.textEdited.connect(self.set_attribute(pulse, "var1", new_value))
self.var2_lineEdit.textEdited.connect(self.set_attribute(pulse, "var2", new_value))
self.var3_lineEdit.textEdited.connect(self.set_attribute(pulse, "var3", new_value))
....
def set_attribute(pulse, var_name, value):
# get value from line edit
# update value in pulse
# update other calculations
class Pulse:
def __init__(self):
var1 = 1
var2 = 2
var3 = 3
....
My problem here is how to code set_attribute. Is there a way to access individual variables of another class by passing the name as a function argument? I would like to avoid having to define a new function for each variable.
Or is there a simpler way to do this? I am not very familiar with OOP and the proper way of coding UIs.
EDIT
The signal/slot connections you've proposed will get you in trouble. Signals are connected to slots using the connect function, which accepts a python callable as its argument--not the function call itself. That's the difference between
widget.textChanged.connect(self.set_attribute) # right syntax
widget.textChanged.connect(self.set_attribute()) # wrong syntax
So then, how do you tell set_attribute what to do? ekhumoro informed me the error of my ways re: use of QSignalMapper. It's an outdated technique. Best to simply use lambda functions (or partials) to get the desired effect.
I updated my demo for use of lambdas
For the purposes of this demo, I've created a simple QMainWindow with two QLineEdit widgets and a QPushButton.
# file ui_main.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(359, 249)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.formLayoutWidget.setGeometry(QtCore.QRect(0, 0, 351, 51))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setObjectName("formLayout")
self.configParam1Label = QtWidgets.QLabel(self.formLayoutWidget)
self.configParam1Label.setObjectName("configParam1Label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.configParam1Label)
self.configEntry = QtWidgets.QLineEdit(self.formLayoutWidget)
self.configEntry.setObjectName("configEntry")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.configEntry)
self.configParam2Label = QtWidgets.QLabel(self.formLayoutWidget)
self.configParam2Label.setObjectName("configParam2Label")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.configParam2Label)
self.configEntry2 = QtWidgets.QLineEdit(self.formLayoutWidget)
self.configEntry2.setObjectName("configEntry2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.configEntry2)
self.pulseButton = QtWidgets.QPushButton(self.centralwidget)
self.pulseButton.setGeometry(QtCore.QRect(130, 140, 75, 23))
self.pulseButton.setObjectName("pulseButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.configParam1Label.setText(_translate("MainWindow", "Config Param 1"))
self.configParam2Label.setText(_translate("MainWindow", "Config Param 2"))
self.pulseButton.setText(_translate("MainWindow", "GetPulse"))
In another file, after some imports, define Pulse--a simple class that contains two attributes
# file main.py
from PyQt5.QtCore import (QSignalMapper, pyqtSlot)
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox)
from ui_main import Ui_MainWindow
class Pulse:
def __init__(self):
self.param_one = None
self.param_two = None
Now, define MainWindow, setting up its UI and giving it a Pulse:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.pulse = Pulse()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.widget_one = self.ui.configEntry
self.widget_two = self.ui.configEntry2
self.pulse_button = self.ui.pulseButton
self.widget_one.textChanged.connect(lambda: self.widget_handler('param_one', self.widget_one.text()))
self.widget_one.textChanged.connect(lambda: self.widget_handler('param_two', self.widget_two.text()))
def widget_handler(self, attr, value):
setattr(self.pulse,attr,value)
def handler(self, idx):
widget, attr = self.attribute_widget_list[idx]
widget_value = widget.text()
setattr(self.pulse,attr,widget_value)
setattr is equivalent to, for example, self.pulse.param_one = widget_value.
To prove it works, let's use that QPushButton to probe the state of Pulse:
#pyqtSlot()
def on_pulseButton_clicked(self):
message = QMessageBox()
message.setText("{} - {}".format(self.pulse.param_one, self.pulse.param_two))
message.exec()
Here I'm using a different signal/slot connection scheme, which you can read about here
And here's how we run the script:
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Hope that helps!
I have created short application with Pyqt5 designer:
Here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(752, 674)
self.formLayout_2 = QtWidgets.QFormLayout(Form)
self.formLayout_2.setObjectName("formLayout_2")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.formLayout_2.setLayout(0, QtWidgets.QFormLayout.LabelRole, self.formLayout)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.pushButton)
self.widget = QtWidgets.QWidget(Form)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setStyleSheet("background-color:white;")
self.widget.setObjectName("widget")
self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.widget)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.pushButton_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Letters"))
self.pushButton_2.setText(_translate("Form", "Numbers"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
This script is connected to appexe script(dialog):
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import string
from functools import partial
from setup_test import Ui_Form
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication
class myprog(Ui_Form):
def __init__ (self, dialog):
Ui_Form.__init__(self)
self.setupUi(dialog)
self.symbols = [string.ascii_lowercase, string.digits]
self.buttons = [self.pushButton, self.pushButton_2]
for i,v in enumerate(self.buttons):
self.buttons[i].clicked.connect(partial(self.application, i))
def application(self, i):
self.grid1 = QGridLayout(self.widget)
names = [x for x in self.symbols[i]]
positions = [(j,d) for j in range(7) for d in range(6)]
for a,b in zip(positions, names):
button = QPushButton(b)
self.grid1.addWidget(button, *a)
print(self.grid1.count())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
appexe = myprog(dialog)
dialog.show()
sys.exit(app.exec_())
I am new to Pyqt5. The basic idea is: whenever you click the button 'Letters' or button 'Numbers', the widget below should layout all the sysmbols within Letters or buttons.
If you do execute the appexe script, you can see, it works for the first try. (Please ignore the visual design). I am happy with the result. When button is pressed, it shows the buttons in grid(widget).
The problem I am facing is, how to clear the grid and it's widget when other button is hit > and display the new grid, new widgets.
If you look close, there is also a print statement:
print(self.grid1.count())
When I am in application and i am clicking letter and number button, it tells me how many widgets are within grid, however I just can't find a way how to display the new setup.
I would appreciate your help and your ideas. Thank you.
There are a number of different ways to remove a layout and its child widgets. However, the only one that seems to work properly with your particular example, is this one:
def application(self, i):
layout = self.widget.layout()
if layout is not None:
QWidget().setLayout(layout)
...
This works because setting the layout on a different widget will automatically re-parent that layout and all its children. Then when the temporary parent widget is immediately garbage-collected, Qt will automatically delete all its children as well.
I am trying to get keypressevent work with the following code
import sys,
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(QtGui.QWidget):
def __init__(self):
super(Ui_MainWindow, self).__init__()
def keyPressEvent(self, event):
print 'a'
def setupUi(self, MainWindow):
MainWindow.setObjectName(("MainWindow"))
MainWindow.resize(371, 345)
MainWindow.setMaximumSize(QtCore.QSize(401, 600))
MainWindow.setWindowIcon(QtGui.QIcon('icons/icon.png'))
screen = QtGui.QDesktopWidget().screenGeometry()
mysize = MainWindow.geometry()
hpos = ( screen.width() - mysize.width() ) / 2
vpos = ( screen.height() - mysize.height() ) / 2
MainWindow.move(hpos, vpos)
#some GUI
MainWindow.setCentralWidget(self.centralwidget)
cd=MainWindow.centralWidget()
cd.setFocusPolicy(QtCore.Qt.StrongFocus)
cd.setFocus()
self.actionHardware = QtGui.QAction(MainWindow)
self.actionHardware.setObjectName(("actionHardware"))
self.retranslateUi(MainWindow)
#COnnect odes
def retranslateUi(self, MainWindow):
#sime button text codes
if __name__=="__main__" :
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The code was partially generated using QTDesigner. I noticed that replacing Mainwindow.show() by ui.show() enables keypressevent but at the cost of not showing any buttons i create in the Mainwindow central widget
It looks like the problem is in the way you are re-using the code output by Designer. You defined Ui_MainWindow.keyPressEvent, and created an instance "ui" of the class. However: "ui" is never directly incorporated into the GUI anywhere (ui.setupUi adds other widgets, but not itself, to MainWindow) and thus events are never delivered to ui.
My approach would look more like this:
class Ui_MainWindow(object): ## note this does not need to inherit QWidget
... ## and ideally, this code should not be changed
... ## after designer generates it
...
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def keyPressEvent(self, ev):
print "key press"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = Window()
MainWindow.show()
sys.exit(app.exec_())