PyQt5 window not appearing every time code is run [duplicate] - python

This question already has answers here:
closing a pyqt widget in ipython notebook without using sys.exit()
(1 answer)
Python kernel dies for second run of PyQt5 GUI
(3 answers)
Closed 3 years ago.
I have been experimenting with PyQt5 and testing very basic examples I've found online. I'm running this using anaconda spyder and python 3.7. The first time I run the code, the window appears fine and how I'd expect it to look. But the second time I try to run it, the window doesn't appear at all, it looks like my IPython console restarts and I have to manually terminate the code to get it to respond. I suspected it was because I wasn't terminating properly in the code but I've tried multiple ways to terminate including sys.exit(), exit, quit, making a class and app=QApplication(sys.argv) followed by sys.exit(app.exec_()), etc. I still get the same issue. What could it be
Here's some examples I tried
Ex:
import sys
from PyQt5.QtWidgets import QPushButton, QApplication, QGridLayout, QVBoxLayout, QWidget, QGroupBox
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
layout.addWidget(QPushButton('Top'))
layout.addWidget(QPushButton('Bottom'))
window.setLayout(layout)
window.show()
app.exec_()
Another Example:
app = QApplication([])
window=QWidget()
layout=QGridLayout()
groupbox=QGroupBox("Box")
vbox=QVBoxLayout()
vbox.addWidget(QPushButton("Button"))
groupbox.setLayout(vbox)
layout.addWidget(groupbox, 0, 0)
window.setLayout(layout)
window.show()
app.exec_()

Related

PyQt5 MainWindow with flag instantly goes out of scope

I created UI using Qt Designer. Then I converted the ui file to a .py file (pyuic -x) - it works fine if launched directly. Then I tried to subclass my ui in a separate file to implement additional logic. And this is where things start to go wrong. Inheriting from QMainWindow and my Qt Designer file works OK with no issues, as expected. However, the moment I set any WindowFlag for my QMainWindow (any flag - I tried these: StaysOnTop, FramelessWindowHint) and run the file, the window appears and instantly disappears. The program continues to run in a console window, or as a PyCharm process, but the window is gone. It looks to me like it is getting out of scope - but why setting a simple flag would make any difference to the garbage collector? Could someone explain this behaviour?
Minimum code required to reproduce this phenomenon:
from ui import Ui_MainWindow
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Logic(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.show()
# self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
# self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Logic()
sys.exit(app.exec_())
The window should appear and stay on the screen until one (or more) of the flags are uncommented. I use Python 3.8 (32-bit) with PyQt5. Run environment provided by PyCharm. Windows 10.
From the documentation of setWindowFlags():
Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..
So, just move self.show() after setting the flags, or call it from outside the __init__ (after the instance is created), which is the most common and suggested way to do so, as it's considered good practice to show a widget only after it has been instanciated.

PyQt5 Widget Disappears When No Breakpoint is Used

Don't critisize me using different classes - the reasons for his is becausethere will be more GUIs in my project created by the QtDesigner, but this should not be important for now.
In general, I have two Python scripts:
main.py:
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
import time
from gui_class import Gui
app = QtWidgets.QApplication(sys.argv)
gui = Gui()
sys.exit(app.exec_())
gui_class.py:
from PyQt5 import QtWidgets
class Gui():
def __init__(self):
w = QtWidgets.QWidget()
w.resize(500, 500)
self.button = QtWidgets.QPushButton(w)
self.button.setGeometry(100, 100, 300, 300)
w.show()
If I run the main.py-script, then the window appears for a split second and disappears right away. I can't see it, I can't click it. The code does not terminate, though. It's still waiting for the application to finish - I can't do anything, though.
If I put a breakpoint before the line saying w.show() in the gui_class.py and simply continue the code after it stopped in that line, then the GUI is visible and I can click the button and the code terminates after I close the window - everything works as expected.
I am using PyQt5: 5.15.2 with Python3.7.
The problem is that w is a local variable that will be destroyed when the scope where it was created finishes executing. The solution is to extend its life cycle by increasing its scope by making it an attribute of the class, for this you must change w with self.w.

Getting Blady placed ()'s error when trying to run a qt in vscode [duplicate]

This question already has an answer here:
pyqt5 not showing window [duplicate]
(1 answer)
Closed 3 years ago.
I'm getting an unknown error 'Badly placed ()'s' when attempting to build a QT widget .
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(50,50,500,300)
window.setWindowTitle("pyqt window")
window.show()
A empty window with "pyqt window" as title
It worked after adding sys.exit(app.exec_()) to the code

PyQt setText not rendering text properly in MacOs

I am creating an application using PyQt5 (pythyon 3.7 , MacOs X)
When I modify the text in a textbox using the instruction
self.line_main.setText(final_text)
from a function (which is connected to a push button)
The new text does not render properly in the textbox (see screenshot) and both the old and new text are overlapping in a strange way.
An over-simplified code to illustrate the problem is this one:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(200, 200))
self.setWindowTitle("PyQt test")
self.line_main = QLineEdit(self)
self.line_main.move(20,20)
bt_upperCase = QPushButton('Upper Case', self)
bt_upperCase.move(20, 60)
bt_upperCase.clicked.connect(self.click_upCase)
def click_upCase(self):
final_text=self.line_main.text().upper()
self.line_main.setText(final_text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
screenshot of mini-app, with 're' as input as 'RE' as output
The same thing may happen for labels, although at times, only the old text is visible, and I need to select the text with the mouse or resize the main window to 'refresh' the textbox and see the new value.
The problem does not happen if I run the code in a PC, only in MacOs (tested in High Sierra and Mojave with identical results)
Some weird behaviours are:
If position the textbox at the (0,0) position in the main window, which is not very practical, then the setText functions correctly. In some other locations of the box, too, but only if it is very close to the top-left corner, and no other element is near.
If I modify the textbox value at the initialization of the main window, then it renders correctly in all cases, The problem appears when you try to do it from a function (after clicking a button, for instance), like in the code attached.
The problem does not happen if I run the code in a PC, only in MacOs (tested in High Sierra and Mojave with identical results)
If position the textbox at the (0,0) position in the main window, (which is not very practical), then the setText functions correctly. In some other locations of the box, too, but only if it is very close to the top-left corner, and no other element is near.
Does anyone have a guess why this may happen, and how could it be solved?
UPDATE.
The problem dissapears if i run the code with python 3.6 in a conda installation (PyQt version 5.9.2). Still very intrigued to know what caused the problem.

How to remove minimize button in PySide [duplicate]

This question already has answers here:
Qt hide minimize, maximize and close buttons
(6 answers)
Closed 7 years ago.
How can I remove the minimize button from QMainWindow in Python?
I'm using Python 3.4 with PySide.
thanks
You need to utilize setWindowFlags do prevent the minimize and maximize button from appearing. You'll have to set the appropriate flags as well.
In this case, you need to enable CustomizeWindowHint and then disable both WindowMinimizeButtonHint and WindowMaximizeButtonHint (alternatively, you could just disable WindowMinMaxButtonsHint, which handles the previous two flags).
A very simple program demonstrating how this works:
import sys
from PySide import QtGui
from PySide import QtCore
def main():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.setWindowFlags(w.windowFlags() & QtCore.Qt.CustomizeWindowHint)
w.setWindowFlags(w.windowFlags() & ~QtCore.Qt.WindowMinMaxButtonsHint)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Outputs:

Categories

Resources