I am new in programming in PyQt (python interface), I like to use QProgressDialog, to show the progress of some tasks, so, the problem is, I had this:
while the change the value of progress , thank you for the help
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(250, 160, 231, 161))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.p = QProgressDialog('titel','',0,10000)
self.p.setValue(0)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("label test"))
def work(self):
for index in range(10001):
time.sleep(2)
self.p.setValue(index)
print(index)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
ui.work()
sys.exit(app.exec_())
PyQt creates a loop through app.exec_(), and this serves to manage the GUI dynamics such as signals, events, etc. So if you interrupt that cycle you would have problems like the one shown. And in its case sleep() blocks the execution of the program. One way to solve this problem is using a QTimer as shown below:
[...]
self.p = QProgressDialog('title','',0,10000)
self.p.setValue(0)
self.timer = QtCore.QTimer(MainWindow)
self.timer.timeout.connect(self.work)
self.timer.start(2000)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
[...]
def work(self):
self.p.setValue(self.p.value()+1)
if self.p.value() == self.p.maximum():
self.timer.stop()
[...]
Related
I am using Python and I have a main window with a button that opens another window which contain stackedWidget, when the user click "Finish" on the second window I want to close it. but when i try to use self.close() method it closing the main windows as well.
here is the part in my code that handle the Finish click event on the second window:
def click_next(self):
if self.next.text()!="Finish>":
if self.stackedWidget.currentIndex()!=2:
self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex()+1)
else:
self.next.setText("Finish>")
self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex() + 1)
else:
self.close()
Is there another way to close one window and keep the main open.
Thanks.
So for the example I created 2 windows ( main and screen1), the button in main will open screen1 and the button in screen1 expected to close only screen1 but it close both windows.
code for main window:
from PyQt5 import QtCore, QtGui, QtWidgets
from screen1 import Ui_screen1
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(584, 567)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.button = QtWidgets.QPushButton(self.centralwidget, clicked=lambda:
self.gotoscreen1())
self.button.setGeometry(QtCore.QRect(220, 260, 131, 51))
self.button.setObjectName("buton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 584, 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 gotoscreen1(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_screen1()
self.ui.setupUi(self.window)
self.window.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button.setText(_translate("MainWindow", "go to screen1"))
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_())
code for screen1:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_screen1(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(384, 567)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.button = QtWidgets.QPushButton(self.centralwidget, clicked=lambda: self.close())
self.button.setGeometry(QtCore.QRect(120, 260, 131, 51))
self.button.setObjectName("buton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 584, 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 close(self):
self.close()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button.setText(_translate("MainWindow", "Close"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_screen1()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem isn't that it is closing both windows, it's that your whole app is crashing because it has reached the maximum recursion depth.
The issue is with your close method.
def close(self):
self.close()
All this is doing is calling itself over and over and over again until it crashes the program.
To fix this simply remove the close method completely, then in your setupUi method change the line that creates the self.button to:
self.button = QtWidgets.QPushButton(self.centralwidget, clicked=MainWindow.close)
I would also recommend subclassing QMainWindow instead of using the uic-like design.
I am trying to make a program that has a loop inside of it. I think that the problem is that one the loop starts the program just loops inside of it.
CODE -
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(319, 258)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(20, 60, 281, 51))
self.checkBox.setObjectName("checkBox")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 319, 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.checkBox.setText(_translate("MainWindow", "CheckBox"))
self.checkBox.clicked.connect(self.potato)
def potato(self, value):
while self.checkBox.isChecked:
print('potato')
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_())```
Do not modify the code generated by Qt Designer but create another class that inherits
from the appropriate widget and use the initial class to fill it.
The while loop is blocking the interface, use a QTimer or extra thread, signals and slots.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(319, 258)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(20, 60, 281, 51))
self.checkBox.setObjectName("checkBox")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 319, 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.checkBox.setText(_translate("MainWindow", "CheckBox"))
class Demo(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.checkBox.clicked.connect(self.potato)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.on_timer)
def potato(self, value):
if value:
self.timer.start(1000)
else:
self.timer.stop()
def on_timer(self):
print('potato')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Demo()
w.show()
sys.exit(app.exec_())
I'm working on a GUI for calling a function 'my_function' when the button 'my_button' is pushed.
This function processes data iteratively. It contains a 'for' loop, and at each iteration it prints out a message that shows the progress of my function. I would like this prints to be displayed in my GUI (in this example in a textEdit widget), in real time. How could I do that?
I would like to make it clear that I need real time display. I found some solutions online, but all prints only appear when the function finishes execution. I need the prints to display in real time in order to appreciate the function progress. Thanks in advance for your tips!
Here's a minimal reproductible example (I obtained the template via qt designer):
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(163, 225)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayout = QtWidgets.QFormLayout(self.centralwidget)
self.formLayout.setObjectName("formLayout")
self.my_button = QtWidgets.QPushButton(self.centralwidget)
self.my_button.setObjectName("my_button")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.my_button)
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setObjectName("textEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.textEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 163, 22))
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)
self.my_button.clicked.connect(self.my_function)
def my_function(self):
for idx in range(10):
print('Executing iteration '+str(idx)+' ...')
time.sleep(1) # replaces time-consuming task
print('Finished!')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.my_button.setText(_translate("MainWindow", "Execute"))
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_())
Golden rule of Qt: You should not execute tasks that consume a lot of time in the main thread of the GUI since they block the event loop of Qt.
Considering the above, you must execute the time-consuming task in another thread and send the information to the GUI thread through signals.
On the other hand you should not modify the class generated by Qt Designer, instead create another class that inherits from the appropriate widget and is filled in with the initial class.
import threading
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(163, 225)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayout = QtWidgets.QFormLayout(self.centralwidget)
self.formLayout.setObjectName("formLayout")
self.my_button = QtWidgets.QPushButton(self.centralwidget)
self.my_button.setObjectName("my_button")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.my_button)
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setObjectName("textEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.textEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 163, 22))
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.my_button.setText(_translate("MainWindow", "Execute"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
valueChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.valueChanged.connect(self.on_value_changed)
self.my_button.clicked.connect(self.on_clicked)
#QtCore.pyqtSlot()
def on_clicked(self):
threading.Thread(target=self.my_function, daemon=True).start()
#QtCore.pyqtSlot(int)
def on_value_changed(self, value):
self.textEdit.append("Value: {}".format(value))
def my_function(self):
for idx in range(10):
self.valueChanged.emit(idx)
print("Executing iteration " + str(idx) + " ...")
time.sleep(1) # replaces time-consuming task
print("Finished!")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Can't understand why I'm getting the empty string from the text field, here is my code:
class MyMainWindow(QMainWindow, test.Ui_MainWindow):
def __init__(self):
super().__init__()
super().setupUi(self)
self.pushButton.clicked.connect(self.click)
def click(self):
text = self.plainTextEdit.toPlainText()
print('Text: ' + str(text))
print()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QMainWindow()
mainWindow = MyMainWindow()
mainWindow.setupUi(window)
mainWindow.show()
sys.exit(app.exec_())
https://pastebin.com/gpDSysL2
and the generated by QT Designer .py file:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(270, 260, 104, 71))
self.plainTextEdit.setObjectName("plainTextEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(270, 350, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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", "PushButton"))
when im clicking on the button it printing the empy string
here is the screenshot of the issue im getting >>> Screenshot
will be very appreciated for you any help
problem was in double calling setupUI() method:
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QMainWindow()
mainWindow = MyMainWindow()
# mainWindow.setupUi(window) <<< HERE WAS THE PROBLEM, JUST DELETEd THIS LINE AND THATS ALL
mainWindow.show()
sys.exit(app.exec_())
maybe somebody will have same issue
I don´t know how to search for, so I will ask here.
What I wanna do is, to create a Qplaintextedit within a Window.
Now I execute my programm and resize it as I want, but my qplaintextedit
isn´t changing his size.
There is any function or something I can do, to set them in relation?
Greetings
Edit:
My Code if it helps:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(799, 592)
MainWindow.setContextMenuPolicy(QtCore.Qt.PreventContextMenu)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.desk = QtWidgets.QPlainTextEdit(self.centralwidget)
self.desk.setGeometry(QtCore.QRect(10, 10, 781, 551))
self.desk.setObjectName("desk")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 799, 21))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuDatei")
self.menuHilfe = QtWidgets.QMenu(self.menubar)
self.menuHilfe.setObjectName("menuHilfe")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.openFile = QtWidgets.QAction(MainWindow)
self.openFile.setObjectName("openFile")
self.actionSpeichern = QtWidgets.QAction(MainWindow)
self.actionSpeichern.setObjectName("actionSpeichern")
self.actionSpeichern_als = QtWidgets.QAction(MainWindow)
self.actionSpeichern_als.setObjectName("actionSpeichern_als")
self.saveFile = QtWidgets.QAction(MainWindow)
self.saveFile.setObjectName("saveFile")
self.saveFileAs = QtWidgets.QAction(MainWindow)
self.saveFileAs.setObjectName("saveFileAs")
self.Credits = QtWidgets.QAction(MainWindow)
self.Credits.setObjectName("Credits")
self.menuDatei.addAction(self.openFile)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.saveFile)
self.menuDatei.addAction(self.saveFileAs)
self.menuHilfe.addAction(self.Credits)
self.menubar.addAction(self.menuDatei.menuAction())
self.menubar.addAction(self.menuHilfe.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuDatei.setTitle(_translate("MainWindow", "Datei"))
self.menuHilfe.setTitle(_translate("MainWindow", "Hilfe"))
self.openFile.setText(_translate("MainWindow", "Öffnen"))
self.actionSpeichern.setText(_translate("MainWindow", "Speichern"))
self.actionSpeichern_als.setText(_translate("MainWindow", "Speichern als..."))
self.saveFile.setText(_translate("MainWindow", "Speichern"))
self.saveFileAs.setText(_translate("MainWindow", "Speichern als..."))
self.Credits.setText(_translate("MainWindow", "Credits ©"))
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_())
You need to add layout to MainWindow. It seems to me that you're using QtDesigner. If so, then right click MainWindow in object browser, choose Lay out and select an appropriate layout. The horizontal or vertical will be ok to start with.
Or you may add layout in code. I hope this will work in Qt5:
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.desk)
self.centralwidget.setLayout(self.layout)