PyQt Creates Seperate Window For Button - python

Hi recently I started experimenting with the PyQt5 GUI library since Tkinter doesn't look very modern or nice. I am trying to create a very simple window with a button on it, but for some reason it opens a new window for the button. How can I solve this? Here is the code:
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
window.show()
btn = QtWidgets.QPushButton("Test")
btn.resize(100,100)
btn.move(100,100)
btn.show()
(app.exec_())
I am aware that I should be using classes for the GUI, but I'd like to learn the basics before I start doing that.

For a widget like the QPushButton to be part of a window, the widget must be fulfilled:
be child of the window or
be a child of some child from the window or
it is part of a layout that belongs to the window.
In your case QPushButton does not meet any of it so it will be a new window.So it can be solved using the 1 or 3 rule:
import sys
from PyQt5 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
btn = QtWidgets.QPushButton("Test", window)
btn.resize(100,100)
btn.move(100,100)
window.show()
sys.exit(app.exec_())
Or:
import sys
from PyQt5 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
btn = QtWidgets.QPushButton("Test")
lay = QtWidgets.QHBoxLayout(window)
lay.addWidget(btn)
window.show()
sys.exit(app.exec_())

Related

how to confine a xterm/rcvt/gnome-terminal to a specific region of screen

I want to launch a QProcess and have it display / render in a specific region of the screen. The process is an xterm(1) or rcvt(1) or gnome-terminal(1) and I embed the rxvt(1) onto my main window.
self.winIdStr = str(int(self.winId()))
self.process.start('rxvt', ['-embed', self.winIdStr , '-e', './goo'])
But my main window looks like this:
And I want to confine rxvt(1) to the QTextEdit area. Unfortunately I don't know Qt lingo well. So would I need to create a QFrame or some other thing to get this going?
You have to create a QWidget where you want the terminal to be placed and pass the winId() of that widget:
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.container = QtWidgets.QWidget()
self.container.setFixedSize(600, 400)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QLineEdit())
lay.addWidget(QtWidgets.QPushButton())
lay.addWidget(self.container)
self.process = QtCore.QProcess(self)
self.winIdStr = str(int(self.container.winId()))
self.process.start("rxvt", ["-embed", self.winIdStr, "-e", "./goo"])
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

how to close a window in pyqt5 and terminate the program? program gets stuck

I want to create a window in pyqt5 and then close it. For some reason the program does not exit after closing the window. It gets stuck. I have been reading several related posts but none give a clear answer.
I have already tried code such as "self.object.close()", "app.quit()", even "self.object.destroy()", but all work in the same way. The only thing that really closes the window is by clicking the x (close) at the window itself. But this is not the behavior I need. I would like to close the window using my code.
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot, pyqtSignal
class window(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(300, 300, 480, 300)
self.setWindowTitle('Hello World')
self.setWindowLayout()
def setWindowLayout(self):
self.w = QtWidgets.QWidget(self)
self.layout = QtWidgets.QHBoxLayout()
self.label = QtWidgets.QLabel('Hello World Label')
self.layout.addWidget(self.label)
self.w.setLayout(self.layout)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = window()
main_window.show()
main_window.close()
sys.exit(app.exec_())
Once I close the window using
main_window.close(). I want my program to exit.
I apologize if this was resolved in a different post. I searched the answer and none solves my problem.
Thanks.
It seems to me that it is a bug since according to the docs the application should be closed if there is no top-level window but it seems that it is not verified if the closing of the window is not after the event-loop starts. A workaround is to use QTimer.singleShot(0, ...) to close the window:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class window(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(300, 300, 480, 300)
self.setWindowTitle('Hello World')
self.setWindowLayout()
def setWindowLayout(self):
self.w = QtWidgets.QWidget(self)
self.layout = QtWidgets.QHBoxLayout()
self.label = QtWidgets.QLabel('Hello World Label')
self.layout.addWidget(self.label)
self.w.setLayout(self.layout)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = window()
main_window.show()
QtCore.QTimer.singleShot(0, main_window.close) # <---
sys.exit(app.exec_())

How to add QSlider to toolbar in mainwindow using pyqt5 in python

I'm now using PyQt5 in python to develop a GUI for my program. I have added some QAction to the toolbar, like start, pause and stop icon. However, I don't know how to add a QSlider to the toolbar in my mainwindow. I prefer to add the QSlider to the position which is marked by a red rectangle. Thanks!
QToolBar has the addWidget() method that allows you to add widgets:
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
toolbar = self.addToolBar("toolbar")
toolbar.addAction("start")
toolbar.addAction("pause")
toolbar.addAction("stop")
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
toolbar.addWidget(self.slider)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

How do I run a Python Qt file in Ubuntu?

Here's the sample code that I want to run:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("Dummy PyQt file")
window.show()
The file is saved as sample.py. The following command isn't working:
$ python ./sample.py
You need to start the Qt event loop by calling app.exec_() once you have initialised the widgets and called show() on your main window.
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("Dummy PyQt file")
window.show()
app.exec_()
You need to call app.exec_() to start the Qt event loop. Without it the program exits immediately before anything can be shown on screen.

PyQt5 : How to make a button close the gui after clicking

Here is my code for the click button:
run_btn=QtWidgets.QPushButton("Run")
def main():
print ('Starting Program')
run_btn.clicked.connect(main)
But after I click "Run", it just prints "Starting Program" again and again, and the GUI window doesn't disappear:
How can I make the button print it once and go on with the program ?
I am using PyQt5 and Python 3.4.0
Suppose that the QPushButton is inside the main widget (in the example QWidget), to close the window we use the close()
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
line1_edit = QtWidgets.QLineEdit()
line2_edit = QtWidgets.QLineEdit()
run_btn=QtWidgets.QPushButton("Run")
def main():
print ('Starting Program')
w.close()
run_btn.clicked.connect(main)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(line1_edit)
layout.addWidget(line2_edit)
layout.addWidget(run_btn)
w.setLayout(layout)
w.show()
sys.exit(app.exec_())

Categories