When I close an application window in PyQt the console is still left running in the background and python.exe process is present until I close the console. I think the sys.exit(app.exec_()) is not able to operate properly.
Mainscript (which opens Firstwindow):
if __name__ == '__main__':
from firstwindow import main
main()
Firstwindow
On button press:
self.close() #close firstprogram
Start() #function to open mainprogram
Start():
def Start():
global MainWindow
MainWindow = QtWidgets.QMainWindow()
ui = genui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
main() (suggested here):
def main_window():
return form
def main():
global form
app = QtWidgets.QApplication(sys.argv)
form = MyApp()
form.show()
app.exec_()
sys.exit(app.exec_())
The problem is that you are calling exec_() twice in the main() function:
def main():
global form
app = QtWidgets.QApplication(sys.argv)
form = MyApp()
form.show()
app.exec_()
sys.exit(app.exec_())
The first app.exec_() line will start an event-loop, which means the main() function will pause there while you interact with the gui. When you close the top-level window (or call quit() on the application), the event-loop will stop, exec_() will return, and the main() function will continue.
But the next line calls sys.exit(app.exec_()), which restarts the event-loop, and pauses the main() function again - including the sys.exit() call, which must wait for exec_() to return. However, it will wait forever, because there is now no gui to interact with, and so there's nothing you can to do to stop the event-loop other than forcibly terminating the script.
Related
When I open the application MyWindow showing and I get some information from the user and clase this window with self.close and open opencv window recording. After a time, record fnishing I try to restart MyWindow (first) process with below codes but it doesnt restart.
When timer end, I am calling below line:
QtGui.qApp.exit(MyWindow.EXIT_CODE_REBOOT)
And this is the main code:
if __name__ == '__main__':
currentExitCode = MyWindow.EXIT_CODE_REBOOT
while currentExitCode == MyWindow.EXIT_CODE_REBOOT:
a = QApplication(sys.argv)
w = MyWindow()
w.show()
currentExitCode = a.exec_()
a = None # delete the QApplication object
and I also have this value:
class MyWindow(QtWidgets.QWidget):
EXIT_CODE_REBOOT = -123
Where is my fault ? How can I restart application ?
I'm working on a basic project with using PyQt5. I made a break method so I can close app with pressing a button. I made a loop in this but when I close the window loop doesn't go back. I want to able to close the window without quitting the program fully.
There is 'main' side:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = TextBar()
mainWin.show()
sys.exit(app.exec_())
And there is button and its command:
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200, 32)
pybutton.move(80, 100)
def clickMethod(self):
print('Name: ' + self.tool.text(), '\nProperties:' + self.prop.text(), '\n')
TextBar.close(self)
I want to close ONLY window when I press OK, NOT full of program.
when the Mainwindow is closed nothing should be running, unless you consider hidding it, like
YourQMainWindow.hide()
self.YourQMainWindow.hide()
2-Methode initialize 2 difference instances, like that once one is closed, another remains or, at about to close, your run a second instance
if __name__ == "__main__":
app = Qtw.QApplication(sys.argv)
main_window = MainWindow() #---->first instance
main_window1 = MainWindow() #---->second instance
sys.exit(app.exec())
if you run then both, they take more ressouces,
so i suggest you create a button to close the first one, that alse start at the same time the second instance of your app,....
Unless you provide more reproductible example (a piece of code), this is how i can only go in advicing you,...(please confirm if it's working for you, me i always use this technic, & it's working),...thank you
I'm trying to open a second window in a new process to not freeze the main window with PyQt5. For this reason, I define a new class that inherits from multiprocessing.Process and shows the window. This is the main code:
class GuiMain(QMainWindow):
...
# Main window with several functions. When a button is clicked, executes
# self.button_pressed()
def button_pressed(self):
proc1 = OpenWindowProcess()
proc1.start()
class OpenWindowProcess(mp.Process)
def __init__(self):
mp.Process.__init__(self)
print(self.pid)
def run(self):
print("Opening window...")
window = QtGui.QWindow()
window.show()
time.sleep(10)
if __name__ == '__main__':
app = QApplication(sys.argv)
application = GuiMain()
sys.exit(app.exec_())
The process is created and gets a PID. When run() function is called, the message "Opening window.." is displayed, but nothing else happens. No window, no error... I can't figure out whats happening. Thank you in advance!
I've come to a solution. You have to create a new QtApplication and attach to it a new QMainWindow instance. This code works fine:
class GuiMain(QMainWindow):
...
# Main window with several functions. When a button is clicked, executes
# self.button_pressed()
def button_pressed(self):
proc1 = OpenWindowProcess()
proc1.start()
class OpenWindowProcess(mp.Process)
def __init__(self):
mp.Process.__init__(self)
print("Process PID: " + self.pid)
def run(self):
print("Opening window...")
app = QApplication(sys.argv)
window = QMainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
application = GuiMain()
sys.exit(app.exec_())
I have a simple login application which prompts user for username and password.
there are 2 buttons, one for logging in and other for cancel.
in the slot for the cancel button, I have the following code
self.cancelbutton.clicked.connect(self.closeIt)
def closeIt(self):
self.close()
however when I hit the Esc Key, or click the 'X' in the QDialog, the application window disappears, but I do not get the command prompt back.
following is the code for the main function
app = QApplication(sys.argv)
form = x_LoginForm()
form.exec_()
form.close()
sys.exit(app.exec_())
I am not able to work out what I am doing wrong.
You're calling sys.exit(). That will immediately exit out of python. Also, it doesn't really make much sense to call form.exec_() and then call app.exec_()
You should only need the app.exec_()
app = QApplication(sys.argv)
form = x_LoginForm()
form.show()
sys.exit(app.exec_())
I have a PyQt wizard that includes a dialog box that asks the user a question. This dialog box is optional and only for use if the user wants it. A button sends a signal that the app receives and opens the window. The problem I have is that when the dialog is closed, it closes the whole app with it. How do I make sure that when the dialog is closed, the main app stays open and running? Here the code that handles the dialog box:
def new_item(self):
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.exec_()
I tried adding a 'Cancel' button to close it manually but the result was the same, the whole app closed.
QtCore.QObject.connect(self.cancel, QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.close)
You shouldn't create new QApplication objects in your code, and I am not surprised that destroying that object closes the application.
Your code should look something like this:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.dialog = QtGui.QMessageBox(self)
self.dialog.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
self.dialog.setIcon(QtGui.QMessageBox.Question)
self.dialog.setText("Click on a button to continue.")
self.pushButtonQuestion = QtGui.QPushButton(self)
self.pushButtonQuestion.setText("Open a Dialog!")
self.pushButtonQuestion.clicked.connect(self.on_pushButtonQuestion_clicked)
self.layoutHorizontal = QtGui.QHBoxLayout(self)
self.layoutHorizontal.addWidget(self.pushButtonQuestion)
#QtCore.pyqtSlot()
def on_pushButtonQuestion_clicked(self):
result = self.dialog.exec_()
if result == QtGui.QMessageBox.Ok:
print "Dialog was accepted."
elif result == QtGui.QMessageBox.Cancel:
print "Dialog was rejected."
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())
Try to use Dialog.reject instead of Dialog.close
.close() method is being used mith QMainWindow Widget, .reject() with QDialog.
In my case, I had QSystemTrayIcon as an "entry point" to my app instead of QMainWindow or QWidget.
Calling .setQuitOnLastWindowClosed(False) on my main QApplication instance helped, thanks to this answer