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:
Related
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.
This question already has answers here:
Autocomplete from .ui
(4 answers)
reading UI file and connecting elements such as buttons, text, etc
(1 answer)
Closed 2 years ago.
I am working on pyqt5 project where I am designing the ui in qt designer and writing its python code in pycharm. Ui has a button and label. Once button is clicked, label value is changed. Below is the code:
import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QApplication, QMainWindow
class ROCKET(QMainWindow):
def __init__(self):
super(ROCKET, self).__init__()
loadUi('ui/gui.ui', self)
self.pushButton.clicked.connect(self.btn_click)
def btn_click(self):
self.label.setText("CLICKED")
app = QApplication(sys.argv)
window = ROCKET()
window.show()
app.exec_()
Pycharm gives warning for pushButton and label:
Unresolved attribute reference 'pushButton' for class 'ROCKET'
but if I am running the code, I am getting correct output. How can I remove these warnings and make the code correct.
The fact that you can refer to your widgets as attributes of the main window is a convenience that is offered by PyQt5 via loadUi. PyCharm is complaining because it didn't see you explicitly define the pushButton attribute for your class.
One way you can get around the warning would be to explicitly define the attribute:
self.pushButton = self.findChild(QPushButton, "name_of_push_button")
self.pushButton.clicked.connect(self.btn_click)
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_()
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
Can someone explain to me the difference between the following two code examples? Why is the top one not working? It executes without error, but the window doesn't stay open.
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
QtGui.QMainWindow().show()
app.exec_()
and:
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
win.show()
app.exec_()
In QtGui.QMainWindow().show() you are creating an object of QMainWindow and you are showing it. But you do no save that instance of the QMainWindow in your memory. So eventually python's garbage collection deletes that instance and your QMainWindow no longer shows.
In the second code: win = QtGui.QMainWindow() you save the object instance of QMainWindow to win in your memory. Python does not consider that as garbage because it is in use and hence your window stays open