PyQt: Multiple QGridLayout - python

How can I have multiple QGridLayouts on a single widget? I want to have one grid layout on the left and one on the right.
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class FormWidget(QWidget):
def __init__(self):
super(FormWidget, self).__init__( )
self.grid = QGridLayout(self)
self.grid2 = QGridLayout(self)
self.grid.addWidget(self.grid2)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FormWidget()
sys.exit(app.exec_())

If you want to place 2 layouts horizontally then you must use a QHBoxLayout:
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout)
lay.addLayout(right_grid_layout)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())
Update:
If you want to set a weight you must set it in the stretch.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QTextEdit, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
# for testing
left_grid_layout.addWidget(QTextEdit())
right_grid_layout.addWidget(QTextEdit())
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout, stretch=1)
lay.addLayout(right_grid_layout, stretch=2)
lay.setContentsMargins(
0, # left
100, # top
0, # right
100 # bottom
)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())

Related

Implement Popup mapping in QSystemTrayIcon

I want to implement behaviour of a popup-window exactly like default volume controller in windows 10. One click on icon, window opens or closes; if window is opened, clicking outside it will close the window. How can i implement this?
Before, i found that it is possible for the widget to override the methods for pressing and releasing the mouse keys, but here it was not found, or it was poorly searched. Help me please.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, \
QWidget, QSystemTrayIcon, QStyle, qApp
import PyQt5.QtCore
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setMinimumSize(PyQt5.QtCore.QSize(600, 130))
self.setMaximumSize(PyQt5.QtCore.QSize(600, 130))
self.setWindowFlags(PyQt5.QtCore.Qt.Popup)
screen_geometry = QApplication.desktop().availableGeometry()
screen_size = (screen_geometry.width(), screen_geometry.height())
win_size = (self.frameSize().width(), self.frameSize().height())
x = screen_size[0] - win_size[0]
y = screen_size[1] - win_size[1]
self.move(x, y)
self.setWindowOpacity(0.85)
self.setWindowTitle("System Tray Application")
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
grid_layout = QGridLayout(central_widget)
grid_layout.addWidget(QLabel("Application, which can minimize to tray",
self), 0, 0)
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(self.
style().standardIcon(QStyle.SP_ComputerIcon))
self.tray_icon.activated.connect(self.trigger)
self.tray_icon.show()
self.setGeometry(500, 570, 600, 130)
self.fl = False
def trigger(self, reason):
if reason == QSystemTrayIcon.MiddleClick:
qApp.quit()
elif reason == QSystemTrayIcon.Trigger:
if not self.fl:
self.show()
else:
self.hide()
self.fl = not self.fl
elif reason == 1:
self.fl = False
# def mouseReleaseEvent(self, event):
# self.hide()
# self.fl = False
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())
If you want to toggle visibility then you must use the setVisible() and isVisible() methods:
import sys
from PyQt5.QtWidgets import (
QApplication,
QGridLayout,
QLabel,
QMainWindow,
QStyle,
QSystemTrayIcon,
QWidget,
)
from PyQt5.QtCore import Qt, QSize
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(600, 130)
self.setWindowFlag(Qt.Popup)
self.setWindowOpacity(0.85)
self.setWindowTitle("System Tray Application")
central_widget = QWidget()
self.setCentralWidget(central_widget)
grid_layout = QGridLayout(central_widget)
grid_layout.addWidget(QLabel("Application, which can minimize to tray"), 0, 0)
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))
self.tray_icon.activated.connect(self.trigger)
self.tray_icon.show()
self.setGeometry(
QStyle.alignedRect(
Qt.LeftToRight,
Qt.AlignBottom | Qt.AlignRight,
self.window().size(),
QApplication.desktop().availableGeometry(),
)
)
def trigger(self, reason):
if reason == QSystemTrayIcon.MiddleClick:
QApplication.quit()
elif reason == QSystemTrayIcon.Trigger:
self.setVisible(not self.isVisible())
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())

Pyqt5 QLabel with image at top and text at bottom

I am trying to create a PyQt5 - QLabel with both image and text. I would like to have a text at the bottom of the image. Below is a part of the code
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Window(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
l4=QLabel()
l4.setText('delete')
l4.setAlignment(Qt.AlignBottom)
pixmap = QPixmap("/home/moh/Documents/My_GUI/Icons/Delete.png")
l4.setPixmap(pixmap)
l4.setAlignment(Qt.AlignTop)
self.layout = QGridLayout()
self.layout.addWidget(l4, 0, 0)
self.setLayout(self.layout)
self.show()
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
You have to use 2 QLabel in a QVBoxLayout:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
pixmap_label = QLabel(
pixmap=QPixmap("/home/moh/Documents/My_GUI/Icons/Delete.png")
)
text_label = QLabel(text="delete")
lay = QVBoxLayout(self)
lay.addWidget(pixmap_label, alignment=Qt.AlignCenter)
lay.addWidget(text_label, alignment=Qt.AlignCenter)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
If you want to use a circle Qlabel image, use this code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainterPath, QPainter
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
class Label(QLabel):
def __init__(self, *args, antialiasing=True, **kwargs):
super(Label, self).__init__(*args, **kwargs)
self.Antialiasing = antialiasing
self.setMaximumSize(90, 90)
self.setMinimumSize(90, 90)
self.radius = 45
self.target = QPixmap(self.size())
self.target.fill(Qt.transparent)
p = QPixmap("Image.jpg").scaled(
90, 90, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
painter = QPainter(self.target)
if self.Antialiasing:
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
path = QPainterPath()
path.addRoundedRect(
0, 0, self.width(), self.height(), self.radius, self.radius)
painter.setClipPath(path)
painter.drawPixmap(0, 0, p)
self.setPixmap(self.target)
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
pixmap_label = Label()
text_label = QLabel(text="delete")
lay = QVBoxLayout(self)
lay.addWidget(pixmap_label, alignment=Qt.AlignCenter)
lay.addWidget(text_label, alignment=Qt.AlignCenter)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())

QHBoxLayout (size, resize, move)

Task:
For example (the code below) we create QHBoxLayout in which there are two buttons.
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
hbox = QHBoxLayout()
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
self.setLayout(hbox)
self.setGeometry(100, 100, 500, 500)
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Necessary:
Set the size for hbox (QHBoxLayout) (W_pix, H_pix) and its coordinates (X_pos, Y_pos) on the main window (they do not fit, I did not find such functions in the documentation for the description for QHBoxLayout, QWidget).
v.2
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import (QWidget, QPushButton,
QHBoxLayout, QApplication, QMainWindow, QVBoxLayout)
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.hbox = QVBoxLayout()
self.okButton = QPushButton("OK")
self.cancelButton = QPushButton("Cancel")
self.hbox.addWidget(self.okButton)
self.hbox.addWidget(self.cancelButton)
self.setGeometry(100, 100, 500, 500)
self.setLayout(self.hbox)
self.hbox.setGeometry(QtCore.QRect(200, 200, 300, 300))
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
A QXLayout is not a visual element, so establishing the geometry (position and size) cannot be applied directly but must be interpreted.
The task of QHBoxLayout is to distribute widgets horizontally using as information the sizeHint, sizePolicy, minimum and maximum sizes, etc. and will use the maximum available size that gives the widget where it was established.
Considering the last point you can extrapolate your requirement to that widget since as described the geometry that handles the layout is that of that widget. So in this case a QWidget used as a container is created.
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QMainWindow, QPushButton, QWidget
class Example(QMainWindow):
def __init__(self):
super().__init__()
x_pos, y_pos = 10, 10
w_pix, h_pix = 150, 150
container = QWidget(self)
container.setContentsMargins(0, 0, 0, 0)
container.setFixedSize(w_pix, h_pix)
container.move(x_pos, y_pos)
container.setStyleSheet("background-color:salmon;")
hbox = QHBoxLayout(container)
hbox.setContentsMargins(0, 0, 0, 0)
self.okButton = QPushButton("OK")
self.cancelButton = QPushButton("Cancel")
hbox.addWidget(self.okButton)
hbox.addWidget(self.cancelButton)
self.resize(640, 480)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

PyQt5, I need not to print continuously but instead it only changes the QLabel

I need it to be not continuously printing but instead it only change the QLabel,
I dont need to add more, just whenever you write in Line edit it should replace the existing text. I need it like a stocks
This is the code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.hbox = QHBoxLayout()
self.game_name = QLabel("Stocks:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Print", self)
self.search_button.clicked.connect(self.on_click)
self.hbox.addWidget(self.game_name)
self.hbox.addWidget(self.game_line_edit)
self.hbox.addWidget(self.search_button)
self.setLayout(self.hbox)
self.show()
#pyqtSlot()
def on_click(self):
game = QLabel(self.game_line_edit.text(), self)
self.hbox.addWidget(game)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
You have to create a QLabel, set it in the layout and only update the text with setText():
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.game_name = QLabel("Stocks:")
self.game_line_edit = QLineEdit()
self.search_button = QPushButton("Print")
self.search_button.clicked.connect(self.on_click)
self.game = QLabel()
hbox = QHBoxLayout(self)
hbox.addWidget(self.game_name)
hbox.addWidget(self.game_line_edit)
hbox.addWidget(self.search_button)
hbox.addWidget(self.game)
self.show()
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Capturing Text with QLineEdit, QPushButton and displaying that text with QLabel pypt5

I'm trying to capture Text with a click on a QPushButton and Display it in a QLabel with pyqt5
I really new to this stuff so go easy on me !
here is the code I have so far:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
game_name = QLabel("Game Name:", self)
game_line_edit = QLineEdit(self)
search_button = QPushButton("Search", self)
search_button.clicked.connect(self.on_click)
hbox.addWidget(game_name)
hbox.addWidget(game_line_edit)
hbox.addWidget(search_button)
self.setLayout(hbox)
self.show()
#pyqtSlot()
def on_click(self):
game = QLabel(game_line_edit.text(), self)
hbox.addWidget(game)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
I keep getting this error:
game = QLabel(game_line_edit.text(), self)
NameError: name 'game_line_edit' is not defined
I am not sure why game_line_edit is not defined but have a feeling it's because it not is the same "class" as my on_click class but am not sure
any help would be appreciated
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.hbox = QHBoxLayout()
self.game_name = QLabel("Game Name:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Search", self)
self.search_button.clicked.connect(self.on_click)
self.hbox.addWidget(self.game_name)
self.hbox.addWidget(self.game_line_edit)
self.hbox.addWidget(self.search_button)
self.setLayout(self.hbox)
self.show()
#pyqtSlot()
def on_click(self):
game = QLabel(self.game_line_edit.text(), self)
self.hbox.addWidget(game)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Categories