PyQt5 interface immediately closes after launch
I am trying to create an interface for my market program with a refresh button in it. The problem is that the window closes almost immediately after it opens. I checked some solutions but I couldn't do anything because I am not sure if the problem is about the button or class system since I am not experienced with using the classes.
import sys
import requests
from bs4 import BeautifulSoup
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtWidgets,QtGui,QtCore
class Pencere(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("Markets")
#window.setStyleSheet("background-color: white;")
window.setGeometry(670,300,400,430)
#################################################### VERİLERİ ALMA
self.url = "https://www.doviz.com//"
self.response = requests.get(self.url)
self.html_content = (self.response).content
self.soup = BeautifulSoup((self.html_content),"html.parser")
self.titles1 = self.soup.find("span",{"data-socket-key":"USD"})
self.titles2 = self.soup.find("span",{"data-socket-key":"EUR"})
self.titles3 = self.soup.find("span",{"data-socket-key":"bitcoin"})
self.titles4 = self.soup.find("span",{"data-socket-key":"XU100"})
self.titles1 = self.titles1.text
self.titles2 = self.titles2.text
self.titles3 = self.titles3.text
self.titles4 = self.titles4.text
#################################################### RESİMLERİ KOYMA
self.image1 = QtWidgets.QLabel(window)
self.image1.setPixmap(QtGui.QPixmap("jdollar.png"))
self.image1.move(50,50)
self.image2 = QtWidgets.QLabel(window)
self.image2.setPixmap(QtGui.QPixmap("jeuro.png"))
self.image2.move(50,120)
self.image3 = QtWidgets.QLabel(window)
self.image3.setPixmap(QtGui.QPixmap("jbtc.png"))
self.image3.move(50,190)
self.image4 = QtWidgets.QLabel(window)
self.image4.setPixmap(QtGui.QPixmap("jbist.png"))
self.image4.move(58,260)
#################################################### SAYILARI YAZDIRMA
self.text1 = QtWidgets.QLabel(window)
self.text1.setText(self.titles1)
self.text1.move(125,68)
self.text2 = QtWidgets.QLabel(window)
self.text2.setText(self.titles2)
self.text2.move(125,138)
self.text3 = QtWidgets.QLabel(window)
self.text3.setText(self.titles3)
self.text3.move(125,278)
self.text4 = QtWidgets.QLabel(window)
self.text4.setText(self.titles4)
self.text4.move(125,208)
#################################################### BUTONLARI OLUŞTURMA
self.button1 = QtWidgets.QPushButton(window)
self.button1.setText("Refresh")
self.button1.move(150,330)
self.button1.clicked.connect(self.__init__)
self.button2 = QtWidgets.QPushButton(window)
self.button2.setText("Exit")
self.button2.move(150,360)
#################################################### EKRANI AÇMA
self.show()
def stats(self):
self.url = "https://www.doviz.com//"
self.response = requests.get(self.url)
self.html_content = (self.response).content
self.soup = BeautifulSoup((self.html_content),"html.parser")
self.titles1 = self.soup.find("span",{"data-socket-key":"USD"})
self.titles2 = self.soup.find("span",{"data-socket-key":"EUR"})
self.titles3 = self.soup.find("span",{"data-socket-key":"bitcoin"})
self.titles4 = self.soup.find("span",{"data-socket-key":"XU100"})
self.titles1 = self.titles1.text
self.titles2 = self.titles2.text
self.titles3 = self.titles3.text
self.titles4 = self.titles4.text
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Pencere()
window.show()
app.exec_()
Solution 1:
you should remove app = QtWidgets.QApplication(sys.argv) part in
def init_ui(self):
app = QtWidgets.QApplication(sys.argv)
you are using two QApplications. Although you can use only one.