Script is still running after closing pyqt5 window - python

import sys
import requests
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("tabletutorial.ui",self)
self.request_function()
def request_function(self):
for i in range(0,100):
resp=requests.get("https://www.google.com")
print(resp.status_code)
# main
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
This window is created by another main window.
Now the problem is when i quit the pyqt5 window the script is still running and i am getting the status code .I am running a big application with a bunch of requests.Anyone with relatable solution please ?
I TRIED:
self.close() not worked for me. #QtCore.pyqtSlot() also not worked.
I am new to here . Please ignore mistakes and kind answer are appriciated.

Related

Why is my next page button in python not working?

I followed a GUI course but the exact code does not work for me. I was trying to be guided to the next page when I clicked the button. But every time I click it, Python stops working with:
Process finished with exit code -1073740791 (0xC0000409)
Here's the code:
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("screen1.ui", self)
self.button.clicked.connect(self.gotoScreen2)
def gotoScreen2(self):
widget.setCurrentIndex(widget.currentWidget()+1)
class Screen2(QMainWindow):
def __init__(self):
super(Screen2, self).__init__()
loadUi("screen2.ui",self)
self.button2.clicked.connect(self.gotoScreen1)
def gotoScreen1(self):
widget.setCurrentIndex(widget.currentWidget()+1)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
mainwindow = MainWindow()
widget.addWidget(mainwindow)
screen2 = Screen2()
widget.addWidget(screen2)
widget.setFixedWidth(600)
widget.setFixedWidth(800)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting..")
There may be some errors in code. It's my first time working on a GUI.
I have changed the
widget.setCurrentIndex(widget.currentWidget()+1)
to
widget.setCurrentIndex(1)
and it solved my problem. Now I can switch between pages without any crashes.
You have to install a module named 'PyQt5'

Getting blank form when running PyQt application

I wrote the calculator code, made the design in QT designer, converted the design file from ui format to py. When the program starts, a white window pops up, just without everything. Maybe someone knows what the problem is, I will be very grateful if someone helps)
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from design import Ui_MainWindow
class Calculator(QMainWindow):
def __init__(self):
super(Calculator, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__== "__main__":
app = QApplication(sys.argv)
window = Calculator()
window.show()
sys.exit(app.exec())

Blank Third window opening up with Second Window PyQt

Ok I'm having an issue where when I click a button on my first window to open up the second window, a third blank window is opening up with it. I've been trying to figure out where in the code this is initializing the 3rd. hoping someone can help.
Here is the code for the first main window...
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from PyQt5 import uic
from PyQt5 import QtCore
import sys
class GuitarSpec_ViewWindow(QMainWindow):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
def __init__(self):
super(GuitarSpec_ViewWindow, self).__init__()
uic.loadUi("guitarSpecViewLayout.ui", self)
self.click = self.findChild(QPushButton, "click_pushButton")
self.click.clicked.connect(self.openEditWindow)
self.show()
def openEditWindow(self):
from guitarSpecEditorTestDelete import Ui_GuitarSpec_EditorWindow
self.window = QMainWindow()
self.Ui = Ui_GuitarSpec_EditorWindow()
self.Ui.setupUi(self.window)
self.window.show()
app = QtWidgets.QApplication(sys.argv)
UIWindow = GuitarSpec_ViewWindow()
app.exec_()
sys.exit(app.exec_())
The Second window when it opens automatically opens up a blank third window. here is that code...
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5 import uic
class Ui_GuitarSpec_EditorWindow(QMainWindow):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
def setupUi(self, GuitarSpec_EditorWindow):
GuitarSpec_EditorWindow.setObjectName("GuitarSpec_EditorWindow")
uic.loadUi("guitarSpecEdit.ui", self)
self.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
GuitarSpec_EditorWindow = QtWidgets.QMainWindow()
ui = Ui_GuitarSpec_EditorWindow()
ui.setupUi(GuitarSpec_EditorWindow)
GuitarSpec_EditorWindow.show()
sys.exit(app.exec_())
Any Help would be greatly appreciated! I am new to this so it's probably a fairly simple solution.

Open second window with QTDesigner 5 in Python 3

I have problem with QTDesigner 5, which should be trivial, but I just can't figure out the problem.
What I want to do is to open a second Window when clicking on a button:
I have designed the Main Window and the secondary one with QTDesigner (PyQT5!) and converted them with pyuic to .py files. The Main Window opens without problems with the following Code:
from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport
class MainWindow(UIImport.Ui_MainWindow):
def __init__(self, window):
UIImport.Ui_MainWindow.__init__(self)
self.setupUi(window)
self.radioButtonGI.clicked.connect(self.openGIPrompt)
def openGIPrompt(self):
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()
class GIPrompt(GIImport.Ui_GIPrompt):
def __init__(self, windowGI):
GIImport.Ui_GIPrompt.__init__(self)
self.setupUi(windowGI)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
sys.exit(app.exec_())
If I add the following to the main function, the "GiPrompt" Window opens as well along with the Main Window:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
"""Open secondWindow"""
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()
sys.exit(app.exec_())
If I try to open the second window via the openGIPrompt function, nothing happens. I do not get an error message, and no window appears. A print command however tells me that the init_function of the second Window is called...
Has someone an idea, what the problem could be?
Thanks in advance!
I have figured out the problem:
Apparently, the initialized Window is disposed of by garbage collection, as the variables are not declared as self:
This fixed the problem:
from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport
class MainWindow(UIImport.Ui_MainWindow):
windowGI=None
Gi=None
def __init__(self, window):
UIImport.Ui_MainWindow.__init__(self)
self.setupUi(window)
self.radioButtonGI.clicked.connect(self.openGIPrompt)
def openGIPrompt(self):
self.windowGI = QtWidgets.QDialog()
self.Gi = GIPrompt(self.windowGI)
self.windowGI.show()
class GIPrompt(GIImport.Ui_GIPrompt):
def __init__(self, windowGI):
GIImport.Ui_GIPrompt.__init__(self)
self.setupUi(windowGI)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
sys.exit(app.exec_())

Basic Widget Interaction with PyQt

Please can someone tell me what im doing wrong here with respect to calling pwTxt.text.
#!/usr/bin/python
import sys
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def on_pwExtract_pressed(self):
print self.pwTxt.text
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
The line print self.pwTxt.text fails because it can't find the widget, pwTxt is a QLineEdit defined on the main window. I just made it in QTDesigner and generated python code with pyuic4.
How do I correctly reference other widgets on the same window, in this case I just want to get the text from a QLineEdit named pwTxt when the QPushButton pwExtract is pressed.
Thanks a lot.
Try:
print self.ui.pwTxt.text()

Categories