Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] PyQt5 Full Tutorial
#1
A Full Tutorial on the latest version (PyQt5) of the popular PyQt GUI library. All the way from setting it up and installing it to using advanced (and fancy) widgets such as the QProgressBar and QSlider.

-> Installation and set up guide.
-> Main PyQt5 tutorial
-> Widget sample
Reply
#2
Do you create your examples with a designer?

I think using setGeometry for all widgets is not a good idea.

Shrinking the window has the effect that your widgets are no longer visible.

I changed your ProgressBar Example this way

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
 
def update():
    reset()
    completed = 0

    while completed < 100:
        completed += 0.0001
        prog_bar.setValue(completed)
 
def reset():
    value = 0
    prog_bar.setValue(value)
 
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,150)
win.setWindowTitle("CodersLegacy")
 
prog_bar = QtWidgets.QProgressBar()
prog_bar.setFixedHeight(26)
prog_bar.setValue(0)
 
button = QtWidgets.QPushButton(win)
button.setText("Update")
button.setFixedWidth(80)
button.clicked.connect(update)
 
reset_button = QtWidgets.QPushButton()
reset_button.setText("Reset")
reset_button.setFixedWidth(80)
reset_button.clicked.connect(reset)

prog_barwid = QtWidgets.QVBoxLayout()
prog_barwid.addWidget(prog_bar)

btn_wid = QtWidgets.QHBoxLayout()
btn_wid.addWidget(button)
btn_wid.addWidget(reset_button)

layout = QtWidgets.QVBoxLayout()
layout.addLayout(prog_barwid)
layout.addLayout(btn_wid)

main_wid = QtWidgets.QWidget()
main_wid.setLayout(layout)
main_wid.setFixedWidth(280)
main_wid.setFixedHeight(100)

win.setCentralWidget(main_wid)

win.show()

sys.exit(app.exec_())
Reply
#3
That's certainly an interesting take. I'm still a bit new to PyQt5, so I'm still working out some of the finer details. Maybe a tutorial on resizing and sizes is needed. It's certainly an important concept.

Generally though, I try to keep my examples as simple as possible to get the point across. Adding in extra precautions can sometimes be a little confusing to someone completely new.
Reply
#4
and better use a Class for your Main Window

from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QProgressBar, 
                            QPushButton, QVBoxLayout, QHBoxLayout)
import sys


class MyWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__() 

        self.setGeometry(400,400,300,150)
        self.setWindowTitle("CodersLegacy")
          
        self.prog_bar = QProgressBar()
        self.prog_bar.setFixedHeight(26)
        self.prog_bar.setValue(0)
          
        self.button = QPushButton()
        self.button.setText("Update")
        self.button.setFixedWidth(80)
        self.button.clicked.connect(self.update)
          
        self.reset_button = QPushButton()
        self.reset_button.setText("Reset")
        self.reset_button.setFixedWidth(80)
        self.reset_button.clicked.connect(self.reset)
         
        prog_barwid = QVBoxLayout()
        prog_barwid.addWidget(self.prog_bar)
         
        btn_wid = QHBoxLayout()
        btn_wid.addWidget(self.button)
        btn_wid.addWidget(self.reset_button)
         
        layout = QVBoxLayout()
        layout.addLayout(prog_barwid)
        layout.addLayout(btn_wid)
         
        main_wid = QWidget()
        main_wid.setLayout(layout)
        main_wid.setFixedWidth(280)
        main_wid.setFixedHeight(100)
         
        self.setCentralWidget(main_wid)
        
    def update(self):
        self.reset()
        completed = 0
     
        while completed < 100:
            completed += 0.0001
            self.prog_bar.setValue(completed)
  
    def reset(self):
        value = 0
        self.prog_bar.setValue(value)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

and look at Pyqt5 Examples
Reply
#5
Thanks for that resource. I'll definitely use it as reference.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020