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_())
Related
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've put together some test code to figure out how to launch a simple pyqt Ui_Dialog multiple times during a script. I want launch the window depending on certain conditions as my script is running. The idea is that it is just a popup notification window with different text and the user will click ok. I designed the window in QtDesigner and inherited the generated code in another class:
class MsgWindow(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.app = QtGui.QApplication(sys.argv)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.OK, QtCore.SIGNAL('clicked()'),self.close)
def AddText(self,text):
""" Method to add text to textEdit box """
self.ui.textEdit.append(text + '\n')
def closeEvent(self, event):
print "Closing the app"
#self.deleteLater()
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
myapp = MsgWindow()
myapp.show()
sys.exit(app.exec_())
So then my test script just launches the window (user closes it) and repeats. The issue is that after the last close, python crashes (python.exe has stopped working). Here is the test script code:
app = QtGui.QApplication(sys.argv)
dlg = MsgWindow()
for x in range(0,3):
dlg.AddText("This is a test, iteration: %d"%x)
dlg.exec_()
#sys.exit(app.exec_())
time.sleep(2)
What is the correct way to do this without crashing?
Thank you.
I am writing a program where I have do the following.
Get User Credentials using a Dialog Box
If successful Launch another application, if Unsuccessful prompt again for credentials.
I am implementing this using two classes called x_LoginForm and x_Application
The code is as follows
class x_LoginForm(QDialog)
Elements in Class
QLineEdit to get Username
QLineEdit to get Password
QPushbutton to Cancel
QPushbutton to Login
If Login is successful, I will set a flag main_window_flag to 1
class x_Application()
This class will Launch a new application and provide control to the user.
The object of this class will be instantiated if the flag main_window_flag is set to 1
main_window_flag=0
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
form = x_LoginForm()
form.show()
if main_window_flag == 1:
window = x_Application()
window.show()
sys.exit(app.exec_())
in the class x_LoginForm
when I have validated I want to pass the control back to main, and instantiate an object of the x_Application class.
to exit from the class x_LoginForm, If I issue self.close - the entire program gets closed.
What is happening is that form.show() returns immediately and your program does the if check then which is likely evaluating to False since you have main_window_flag=0 if you change form.show() to form.exec_() it will wait until the form closes and then proceed with the code. Read here for more about modal dialogs.
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.
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