Can anyone tell me why the program is not showing 'hello' and just showing a simple white screen?

79 Views Asked by At

When I try to run this, it starts and runs perfectly fine with no error and a white screen just like any other normal PySide6 program but it doesn't show the text that i expected to see.

from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *

class View(QGraphicsScene):
    def __init__(self):
        super().__init__()
        self.addText("Hello!")
        self.scene = QGraphicsView(self)
        self.scene.show()

app = QApplication([])
View()
app.exec()

i expected a white screen with hello text but there's just a white screen with nothing

3

There are 3 best solutions below

0
On BEST ANSWER

You are not doing assigning View object to any variable, so it gets garbage collected nearly immediately. Assign it to something and it will stay open: window = View()

0
On

You should assign View() to a variable to avoid it being garbage collected, then show it like this to print the hello text:

from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *

class View(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.setScene(QGraphicsScene())
        self.scene().addText("Hello!")

if __name__ == "__main__":
    app = QApplication([])
    view = View()
    view.show()
    app.exec()
0
On

View object has no reference to it. However, you can fix this by assigning the View object to a variable.

app = QApplication([])
view = View()
app.exec()