How to create one scrollarea in pyqt - python

I am developing a scroll area widget with a height of 300 and adding QLabel Widgets in the scroll area, each QLabel height is 100. When adding more than 2 QLabel, the QLabel in the scroll area overlaps, and the scroll bar does not It shows that I have no way to pull the scroll bar. I want to separate the QLabel from each other. At the same time, I can pull down the scroll bar to see the remaining QLabel
from PyQt4 import QtCore
from PyQt4.QtGui import QScrollArea, QLabel, QVBoxLayout
import sys
from PyQt4 import QtGui
class ScrollArea(QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
vbox = QVBoxLayout()
self.setLayout(vbox)
for i in range(4):
lb = QLabel('hjkmlasda')
lb.setStyleSheet("""
border-width: 1px;
border-style: solid;
border-color:blue;
""")
lb.setFixedSize(400, 100)
vbox.addWidget(lb)
vbox.addStretch(1)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
h = QtGui.QHBoxLayout()
h.addWidget(ScrollArea())
self.setLayout(h)
self.setGeometry(100, 100, 1000, 500)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

You don't have to set a layout in the QScrollArea but a widget, so how do I put several widgets? Well, you must use a QWidget as a container and set the widget through a layout assigned to the container, and also enable the widgetResizable property:
class ScrollArea(QtGui.QScrollArea):
def __init__(self, parent=None):
super(ScrollArea, self).__init__(parent)
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setWidgetResizable(True)
container = QtGui.QWidget()
self.setWidget(container)
vbox = QtGui.QVBoxLayout(container)
# vbox.setSpacing(0)
for i in range(4):
lb = QtGui.QLabel("hjkmlasda")
lb.setStyleSheet(
"""
border-width: 1px;
border-style: solid;
border-color:blue;
"""
)
lb.setFixedSize(400, 100)
vbox.addWidget(lb)

You should set a resizable widget for the QScrollArea and place the layout on that widget. Edit the constructor as such:
class ScrollArea(QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
w = QWidget()
self.setWidget(w)
self.setWidgetResizable(True)
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
vbox = QVBoxLayout(w)
. . .

Related

QStackedLayout shows empty window for a few moments

In this example, as the main window, I use a QWidget that contains a QStackedLayout and a QPushButton to change the current widget to a QStackedLayout.
from PySide6.QtWidgets import QFrame, QWidget, QApplication, QVBoxLayout, QStackedLayout, QPushButton
from PySide6.QtCore import Qt
class ColorWidget(QFrame):
def __init__(self, color):
super(ColorWidget, self).__init__()
self.setFixedSize(200, 200)
self.setStyleSheet(f"background-color: {color}; border-radius: 6px;")
# Some widget. In this case, just a colored background.
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.current_widget = False
layout = QStackedLayout()
layout.addWidget(ColorWidget("red"))
layout.addWidget(ColorWidget("yellow"))
layout.setCurrentIndex(0)
self.setLayout(layout)
# Main widget. Contains 2 colored widgets.
def change_visible_widget(self):
self.current_widget = not self.current_widget
self.layout().setCurrentIndex(int(self.current_widget))
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# no frame, no background
layout = QVBoxLayout()
main_widget = MainWidget()
button = QPushButton("change")
button.clicked.connect(main_widget.change_visible_widget)
# button to change QStackedLayout index in Main Widget
layout.addWidget(main_widget)
layout.addWidget(button)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication()
win = MainWindow()
win.show()
app.exec()
The problem is that when the program starts, an empty window appears for a few moments.
By trial and error, I realized that this is because of the QStackedLayout and the number of windows that appear is equal to the number of created QStackedLayout (in this case it is 1).
How can this be fixed?
Just add self to layout = QStackedLayout():
from PySide6.QtWidgets import QFrame, QWidget, QApplication, QVBoxLayout, QStackedLayout, QPushButton
from PySide6.QtCore import Qt
class ColorWidget(QFrame):
def __init__(self, color):
super(ColorWidget, self).__init__()
self.setFixedSize(200, 200)
self.setStyleSheet(f"background-color: {color}; border-radius: 6px;")
# Some widget. In this case, just a colored background.
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.current_widget = False
layout = QStackedLayout(self)
layout.addWidget(ColorWidget("red"))
layout.addWidget(ColorWidget("yellow"))
layout.setCurrentIndex(0)
# Main widget. Contains 2 colored widgets.
def change_visible_widget(self):
self.current_widget = not self.current_widget
self.layout().setCurrentIndex(int(self.current_widget))
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# no frame, no background
layout = QVBoxLayout()
main_widget = MainWidget()
button = QPushButton("change")
button.clicked.connect(main_widget.change_visible_widget)
# button to change QStackedLayout index in Main Widget
layout.addWidget(main_widget)
layout.addWidget(button)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication()
win = MainWindow()
win.show()
app.exec()

pyqt QTabWidget setCornerWidget TopRightCorner

I have a QTabWidget and add a set label to his upper right corner. When I set the width and height of the label, its border crosses the 'a' tab I added. I want to set the width and height of lalel. Do not cross the 'a' tab I added, how should I set
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QTabWidget, QLabel
import sys
from PyQt4 import QtGui
class Setting(QLabel):
def __init__(self):
super(Setting, self).__init__()
self.setText('setting')
self.setStyleSheet("""*{border-width: 1px;
border-style: solid;
border-color: red;}""")
self.setFixedSize(30, 40)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def create_tab(self):
self.tab = QTabWidget()
self.tab.addTab(QLabel('a'), 'a')
self.tab.setCornerWidget(Setting(), Qt.TopRightCorner)
def initUI(self):
self.create_tab()
h = QtGui.QHBoxLayout()
self.setLayout(h)
h.addWidget(self.tab)
self.setGeometry(100, 100, 500, 500)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you remove the setFixedSize() from your label you can control the size and position through a stylesheet on the QTabWidget by targetting ::right-corner.
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def create_tab(self):
self.tab = QTabWidget()
self.tab.setStyleSheet('''
QTabWidget::right-corner {
width: 60px;
height: 25px;
subcontrol-position: left bottom;
}''')
self.tab.addTab(QLabel('a'), 'a')
self.tab.setCornerWidget(Setting(), Qt.TopRightCorner)
Instead of subcontrol-position you can also use top, bottom, left and right with pixel values to shift the control around.

Display a QLabel at the top-centre of the window

I'm having trouble setting up PyQt on my own. My idea its to creat a music-player with a song title and album cover. I have had success in creating my own window and adding the album cover. But I can't add the label in the right position. I want the song title to be at the top-center of the window, like the image below:
I have tried a lot of ways, but had no luck.
import sys
from PyQt5.QtGui import QIcon, QPixmap, QFontDatabase, QFont
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QWidget, QGridLayout, QDialog
from PyQt5.QtCore import Qt, QRect
# Subclass QMainWindow to customise your application's main window
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.title = 'PyQt5 simple window - pythonspot.com'
self.left = 10
self.top = 10
self.width = 480
self.height = 320
self.initUI()
self.setWindowTitle("My Awesome App")
def add_font(self):
# Load the font:
font_db = QFontDatabase()
font_id = font_db.addApplicationFont('American Captain.ttf')
families = font_db.applicationFontFamilies(font_id)
ttf_font = QFont(' '.join(families), 15)
return ttf_font
def initUI(self):
ttf_font = self.add_font()
w = QWidget()
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.show()
album_cover = QLabel(self)
album_pic = QPixmap('resized_image.jpg')
album_cover.setPixmap(album_pic)
album_cover.setAlignment(Qt.AlignCenter)
self.setCentralWidget(album_cover)
art_alb = QLabel(self)
art_alb.setFont(ttf_font)
art_alb.setText("michael buble - christmas")
art_alb.setGeometry(self.x, self.y, self.x, self.y)
art_alb.setAlignment(Qt.AlignTop | Qt.AlignCenter )
art_alb.show()
self.show()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
You should use a central widget with a layout to control how the child widgets are sized and positioned in the main window. Here is a re-write of your initUI method that should do what you want:
class MainWindow(QMainWindow):
...
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
widget = QWidget()
layout = QGridLayout(widget)
art_alb = QLabel(self)
ttf_font = self.add_font()
art_alb.setFont(ttf_font)
art_alb.setText("michael buble - christmas")
layout.addWidget(art_alb, 0, 0, Qt.AlignTop | Qt.AlignHCenter)
album_cover = QLabel(self)
album_pic = QPixmap('image.jpg')
album_cover.setPixmap(album_pic)
layout.addWidget(album_cover, 1, 0, Qt.AlignHCenter)
layout.setRowStretch(1, 1)
self.setCentralWidget(widget)
Note that there's no need to keep calling show(), since this is all handled automatically by the layout. For more information, see the Layout Management article in the Qt docs.

PyQt: How to create a scrollable window

I think it should be much easier to create a scrollable window in PyQt.
I have a list of labels that goes out of the window and I would like to scroll down to view them. At the moment the code does not give me an error, but the window just doesn't appear:
class Example(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
lbl_arr = makeLabelArr()
for i in range(1,8):
qb = lbl_arr[i]
# qb.setFixedWidth(300)
layout.addWidget(qb)
layout.setAlignment(Qt.AlignTop)
scroll = QScrollArea()
scroll.setWidget(self)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout.addWidget(scroll)
self.setLayout(layout)
self.setGeometry(0, 0, 600, 220)
self.setWindowTitle('SnP watchlist')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
#print(QDesktopWidget().availableGeometry())
ex = Example()
sys.exit(app.exec_())
Make the window itself a QScrollArea, like this:
class Window(QScrollArea):
def __init__(self):
super(Window, self).__init__()
widget = QWidget()
layout = QVBoxLayout(widget)
layout.setAlignment(Qt.AlignTop)
for index in range(100):
layout.addWidget(QLabel('Label %02d' % index))
self.setWidget(widget)
self.setWidgetResizable(True)
There is an example here: https://www.learnpyqt.com/tutorials/qscrollarea/
from PyQt5.QtWidgets import (QWidget, QSlider, QLineEdit, QLabel, QPushButton, QScrollArea,QApplication,
QHBoxLayout, QVBoxLayout, QMainWindow)
from PyQt5.QtCore import Qt, QSize
from PyQt5 import QtWidgets, uic
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.scroll = QScrollArea() # Scroll Area which contains the widgets, set as the centralWidget
self.widget = QWidget() # Widget that contains the collection of Vertical Box
self.vbox = QVBoxLayout() # The Vertical Box that contains the Horizontal Boxes of labels and buttons
for i in range(1,50):
object = QLabel("TextLabel: "+str(i))
self.vbox.addWidget(object)
self.widget.setLayout(self.vbox)
#Scroll Area Properties
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.setCentralWidget(self.scroll)
self.setGeometry(600, 100, 1000, 900)
self.setWindowTitle('Scroll Area Demonstration')
self.show()
return
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You should set layout after adding the scroll bar widget.
class Example(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
lbl_arr = makeArrayOfLabelsHTML()
for i in range(1,8):
qb = lbl_arr[i]
layout.addWidget(qb)
layout.setAlignment(Qt.AlignTop)
scroll = QScrollArea()
scroll.setWidget(self)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout.addWidget(scroll)
# set layout after adding scroll bar
self.setLayout(layout)
self.setGeometry(0, 0, 600, 220)
self.setWindowTitle('SnP watchlist')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
#print(QDesktopWidget().availableGeometry())
ex = Example()
sys.exit(app.exec_())

PyQt5 set a QLabel x axis automatically center of the window

How can I set x axis of the QLabel the exact center of the window? I have this example
from PyQt5.QtWidgets import QMainWindow,QApplication,QLabel
from PyQt5 import QtCore
import sys
class cssden(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setFixedSize(700,400)
self.label = QLabel(self)
self.label.setText("Center of the window")
self.label.setStyleSheet("color:green;"
"font: bold 20pt 'Arial'")
self.label.setGeometry(150,200,300,100)
self.show()
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 2px solid rgb(20,20,20)}")
ex = cssden()
sys.exit(app.exec_())
What to do on self.label.setGeometry so the label will be on the center of the window all the time? Is there a method like setCenter() ?
Use a vertical layout. The label will expand to fill the available space, and the text will be aligned centrally by default:
from PyQt5.QtWidgets import QWidget, QVBoxLayout
class cssden(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setFixedSize(700,400)
self.label = QLabel(self)
self.label.setText("Center of the window")
self.label.setStyleSheet("color:green;"
"font: bold 20pt 'Arial'")
self.label.setAlignment(QtCore.Qt.AlignCenter)
widget = QWidget(self)
layout = QVBoxLayout(widget)
layout.addWidget(self.label)
self.setCentralWidget(widget)
self.show()

Categories

Resources