├── README.md ├── qlabel ├── colours.py ├── cpu.png ├── drive.png ├── hyperlink.py ├── images.py ├── laptop.png ├── player.png └── text_labels.py ├── qlineedit ├── alignment.py ├── completer.py ├── echo_mode.py ├── hex_validator.py └── text_changed.py ├── qlistwidget ├── clear_count.py ├── simple.py └── sort_items.py ├── qradiobutton ├── radio_button.py └── radio_button2.py ├── qshortcut ├── button.py ├── menu.py └── simple.py ├── qslider ├── horizontal.py ├── max.png ├── med.png ├── min.png ├── mute.png ├── vertical.py └── volume_control.py ├── qspinbox ├── show_value.py ├── spinbox.py └── spinbox2.py ├── qtooltip ├── mouse_positions.py ├── shapes.py ├── simple.py └── styled.py └── qwebengineview ├── back_forw.py ├── export_pdf.py ├── simple.py └── test.html /README.md: -------------------------------------------------------------------------------- 1 | # PyQt-Examples 2 | Sources and images for ZetCode's [PyQt tutorials](http://zetcode.com/all/#pyqt) 3 | 4 | This repository contains more in-depth examples for different topics. 5 | 6 | The ZetCode [PyQt5 tutorial](http://zetcode.com/gui/pyqt5/) has its own [PyQt5-Tutorial-Examples](https://github.com/janbodnar/PyQt5-Tutorial-Examples) repository. 7 | 8 | 9 | Author's [Advanced PyQt5 e-book](http://zetcode.com/ebooks/advancedpyqt5/) 10 | -------------------------------------------------------------------------------- /qlabel/colours.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | import sys 5 | from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QGridLayout 6 | from PyQt5.QtGui import QPixmap 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | 17 | def initUI(self): 18 | 19 | grid = QGridLayout() 20 | 21 | lbl1 = QLabel() 22 | lbl1.setStyleSheet("background-color:firebrick; border-radius:5px") 23 | 24 | lbl2 = QLabel() 25 | lbl2.setStyleSheet("background-color:gold; border-radius:5px") 26 | 27 | lbl3 = QLabel() 28 | lbl3.setStyleSheet("background-color:seagreen; border-radius:5px") 29 | 30 | lbl4 = QLabel() 31 | lbl4.setStyleSheet("background-color:royalblue; border-radius:5px") 32 | 33 | lbl5 = QLabel() 34 | lbl5.setStyleSheet("background-color:crimson; border-radius:5px") 35 | 36 | lbl6 = QLabel() 37 | lbl6.setStyleSheet("background-color:salmon; border-radius:5px") 38 | 39 | lbl7 = QLabel() 40 | lbl7.setStyleSheet("background-color:deeppink; border-radius:5px") 41 | 42 | lbl8 = QLabel() 43 | lbl8.setStyleSheet("background-color:tomato; border-radius:5px") 44 | 45 | lbl9 = QLabel() 46 | lbl9.setStyleSheet("background-color:darkkhaki; border-radius:5px") 47 | 48 | lbl10 = QLabel() 49 | lbl10.setStyleSheet("background-color:cornflowerblue; border-radius:5px") 50 | 51 | lbl11 = QLabel() 52 | lbl11.setStyleSheet("background-color:rosybrown; border-radius:5px") 53 | 54 | lbl12 = QLabel() 55 | lbl12.setStyleSheet("background-color:chocolate; border-radius:5px") 56 | 57 | lbl13 = QLabel() 58 | lbl13.setStyleSheet("background-color:slategray; border-radius:5px") 59 | 60 | grid.addWidget(lbl1, 0, 0) 61 | grid.addWidget(lbl2, 0, 1) 62 | grid.addWidget(lbl3, 0, 2) 63 | grid.addWidget(lbl4, 0, 3) 64 | grid.addWidget(lbl5, 1, 0) 65 | grid.addWidget(lbl6, 1, 1) 66 | grid.addWidget(lbl8, 1, 2) 67 | grid.addWidget(lbl9, 1, 3) 68 | grid.addWidget(lbl10, 2, 0) 69 | grid.addWidget(lbl11, 2, 1) 70 | grid.addWidget(lbl12, 2, 2) 71 | grid.addWidget(lbl13, 2, 3) 72 | 73 | self.setLayout(grid) 74 | 75 | self.setGeometry(300, 300, 420, 200) 76 | self.setWindowTitle('Colours') 77 | self.show() 78 | 79 | 80 | def main(): 81 | 82 | app = QApplication(sys.argv) 83 | ex = Example() 84 | sys.exit(app.exec_()) 85 | 86 | 87 | if __name__ == '__main__': 88 | main() 89 | -------------------------------------------------------------------------------- /qlabel/cpu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qlabel/cpu.png -------------------------------------------------------------------------------- /qlabel/drive.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qlabel/drive.png -------------------------------------------------------------------------------- /qlabel/hyperlink.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QHBoxLayout 5 | from PyQt5.QtCore import Qt 6 | 7 | 8 | class Example(QWidget): 9 | 10 | def __init__(self): 11 | super().__init__() 12 | 13 | self.initUI() 14 | 15 | def initUI(self): 16 | 17 | 18 | hbox = QHBoxLayout() 19 | 20 | link = QLabel('zetcode.com') 21 | link.setOpenExternalLinks(True) 22 | 23 | hbox.addWidget(link) 24 | 25 | self.setLayout(hbox) 26 | 27 | self.setGeometry(300, 300, 350, 250) 28 | self.setWindowTitle('HTML link') 29 | self.show() 30 | 31 | 32 | def main(): 33 | 34 | app = QApplication(sys.argv) 35 | ex = Example() 36 | sys.exit(app.exec_()) 37 | 38 | 39 | if __name__ == '__main__': 40 | main() 41 | -------------------------------------------------------------------------------- /qlabel/images.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | import sys 5 | from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QHBoxLayout 6 | from PyQt5.QtGui import QPixmap 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | 17 | def initUI(self): 18 | 19 | hbox = QHBoxLayout() 20 | 21 | lbl1 = QLabel() 22 | lbl1.setPixmap(QPixmap("cpu.png")) 23 | 24 | lbl2 = QLabel() 25 | lbl2.setPixmap(QPixmap("drive.png")) 26 | 27 | lbl3 = QLabel() 28 | lbl3.setPixmap(QPixmap("laptop.png")) 29 | 30 | lbl4 = QLabel() 31 | lbl4.setPixmap(QPixmap("player.png")) 32 | 33 | hbox.addWidget(lbl1) 34 | hbox.addWidget(lbl2) 35 | hbox.addWidget(lbl3) 36 | hbox.addWidget(lbl4) 37 | 38 | self.setLayout(hbox) 39 | 40 | self.move(300, 300) 41 | self.setWindowTitle('Images') 42 | self.show() 43 | 44 | 45 | def main(): 46 | 47 | app = QApplication(sys.argv) 48 | ex = Example() 49 | sys.exit(app.exec_()) 50 | 51 | 52 | if __name__ == '__main__': 53 | main() 54 | -------------------------------------------------------------------------------- /qlabel/laptop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qlabel/laptop.png -------------------------------------------------------------------------------- /qlabel/player.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qlabel/player.png -------------------------------------------------------------------------------- /qlabel/text_labels.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QHBoxLayout 5 | 6 | class Example(QWidget): 7 | 8 | def __init__(self): 9 | super().__init__() 10 | 11 | self.initUI() 12 | 13 | 14 | def initUI(self): 15 | 16 | hbox = QHBoxLayout() 17 | 18 | hbox.addWidget(QLabel("falcon")) 19 | hbox.addWidget(QLabel("owl")) 20 | hbox.addWidget(QLabel("eagle")) 21 | hbox.addWidget(QLabel("skylark")) 22 | 23 | self.setLayout(hbox) 24 | 25 | self.setGeometry(300, 300, 350, 250) 26 | self.setWindowTitle('QLabel') 27 | self.show() 28 | 29 | 30 | def main(): 31 | 32 | app = QApplication(sys.argv) 33 | ex = Example() 34 | sys.exit(app.exec_()) 35 | 36 | 37 | if __name__ == '__main__': 38 | main() 39 | -------------------------------------------------------------------------------- /qlineedit/alignment.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | 5 | from PyQt5.QtCore import Qt 6 | from PyQt5.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QLineEdit, 7 | QWidget) 8 | 9 | 10 | class Example(QWidget): 11 | 12 | def __init__(self): 13 | super().__init__() 14 | 15 | self.initUI() 16 | 17 | 18 | def initUI(self): 19 | 20 | hbox = QHBoxLayout() 21 | 22 | combo = QComboBox(self) 23 | combo.addItem('Left') 24 | combo.addItem('Center') 25 | combo.addItem('Right') 26 | 27 | combo.activated[str].connect(self.onActivated) 28 | 29 | self.qle = QLineEdit(self) 30 | 31 | hbox.addWidget(combo) 32 | hbox.setSpacing(20) 33 | hbox.addWidget(self.qle) 34 | 35 | self.setLayout(hbox) 36 | 37 | self.setWindowTitle('Text alignment') 38 | self.show() 39 | 40 | 41 | def onActivated(self, text): 42 | 43 | if text == 'Left': 44 | self.qle.setAlignment(Qt.AlignLeft) 45 | elif text == 'Center': 46 | self.qle.setAlignment(Qt.AlignCenter) 47 | else: 48 | self.qle.setAlignment(Qt.AlignRight) 49 | 50 | 51 | def main(): 52 | 53 | app = QApplication(sys.argv) 54 | ex = Example() 55 | sys.exit(app.exec_()) 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | 61 | -------------------------------------------------------------------------------- /qlineedit/completer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import (QWidget, QLabel, QCompleter, QLineEdit, 5 | QHBoxLayout, QApplication) 6 | from PyQt5.QtCore import Qt 7 | 8 | data = [ 9 | 'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola','Antigua & Deps', 10 | 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 11 | 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 12 | 'Benin', 'Bhutan', 'Bolivia', 'Bosnia Herzegovina', 'Botswana', 13 | 'Brazil', 'Brunei', 'Bulgaria', 'Burkina', 'Burundi', 'Cambodia', 'Cameroon', 14 | 'Canada', 'Cape Verde', 'Central African Rep', 'Chad', 'Chile', 'China', 15 | 'Colombia', 'Comoros', 'Congo', 'Congo {Democratic Rep}', 'Costa Rica', 16 | 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 17 | 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 18 | 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 19 | 'Fiji', 'Finland', 'France', 'Gabon', 'Gambia', 'Georgia', 'Germany', 20 | 'Ghana', 'Greece', 'Grenada', 'Guatemala', 'Guinea', 'Guinea-Bissau', 21 | 'Guyana', 'Haiti', 'Honduras', 'Hungary', 'Iceland', 'India', 'Indonesia', 22 | 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Ivory Coast', 'Jamaica', 23 | 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Korea North', 24 | 'Korea South', 'Kosovo', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 25 | 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 26 | 'Luxembourg', 'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 27 | 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius', 'Mexico', 28 | 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 29 | 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 30 | 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 31 | 'Palau', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 32 | 'Poland', 'Portugal', 'Qatar', 'Romania', 'Russian Federation', 'Rwanda', 33 | 'St Kitts & Nevis', 'St Lucia', 'Saint Vincent & the Grenadines', 34 | 'Samoa', 'San Marino', 'Sao Tome & Principe', 'Saudi Arabia', 'Senegal', 35 | 'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 36 | 'Solomon Islands', 'Somalia', 'South Africa', 'South Sudan', 'Spain', 37 | 'Sri Lanka', 'Sudan', 'Suriname', 'Swaziland', 'Sweden', 'Switzerland', 38 | 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania', 'Thailand', 'Togo', 'Tonga', 39 | 'Trinidad & Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda', 40 | 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 41 | 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City', 'Venezuela', 'Vietnam', 42 | 'Yemen', 'Zambia', 'Zimbabwe' 43 | ] 44 | 45 | 46 | class Example(QWidget): 47 | 48 | def __init__(self): 49 | super().__init__() 50 | 51 | self.initUI() 52 | 53 | 54 | def initUI(self): 55 | 56 | hbox = QHBoxLayout() 57 | 58 | label = QLabel('Enter country: ') 59 | 60 | self.qle = QLineEdit(self) 61 | completer = QCompleter(data) 62 | self.qle.setCompleter(completer) 63 | 64 | hbox.addWidget(label) 65 | hbox.addSpacing(20) 66 | hbox.addWidget(self.qle) 67 | 68 | self.setLayout(hbox) 69 | 70 | self.setWindowTitle('Completer') 71 | self.show() 72 | 73 | 74 | def main(): 75 | 76 | app = QApplication(sys.argv) 77 | ex = Example() 78 | sys.exit(app.exec_()) 79 | 80 | 81 | if __name__ == '__main__': 82 | main() 83 | 84 | -------------------------------------------------------------------------------- /qlineedit/echo_mode.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import (QWidget, QComboBox, QPushButton, QLineEdit, 5 | QHBoxLayout, QApplication, QMessageBox) 6 | from PyQt5.QtCore import Qt 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | 17 | def initUI(self): 18 | 19 | hbox = QHBoxLayout() 20 | 21 | combo = QComboBox(self) 22 | combo.addItem('Normal') 23 | combo.addItem('Password') 24 | combo.addItem('PasswordEchoOnEdit') 25 | combo.addItem('NoEcho') 26 | 27 | combo.activated[str].connect(self.onActivated) 28 | 29 | self.qle = QLineEdit(self) 30 | 31 | showBtn = QPushButton('Show', self) 32 | showBtn.clicked.connect(self.onClicked) 33 | 34 | hbox.addWidget(combo) 35 | hbox.setSpacing(20) 36 | hbox.addWidget(self.qle) 37 | hbox.setSpacing(20) 38 | hbox.addWidget(showBtn) 39 | 40 | self.setLayout(hbox) 41 | 42 | self.setWindowTitle('Echo mode') 43 | self.show() 44 | 45 | 46 | def onActivated(self, text): 47 | 48 | if text == 'Normal': 49 | self.qle.setEchoMode(QLineEdit.EchoMode.Normal) 50 | elif text == 'Password': 51 | self.qle.setEchoMode(QLineEdit.EchoMode.Password) 52 | elif text == 'PasswordEchoOnEdit': 53 | self.qle.setEchoMode(QLineEdit.EchoMode.PasswordEchoOnEdit) 54 | elif text == 'NoEcho': 55 | self.qle.setEchoMode(QLineEdit.EchoMode.NoEcho) 56 | 57 | 58 | def onClicked(self): 59 | 60 | text = self.qle.text() 61 | QMessageBox.information(self, 'info', text) 62 | 63 | 64 | def main(): 65 | 66 | app = QApplication(sys.argv) 67 | ex = Example() 68 | sys.exit(app.exec_()) 69 | 70 | 71 | if __name__ == '__main__': 72 | main() 73 | 74 | -------------------------------------------------------------------------------- /qlineedit/hex_validator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QSizePolicy, 5 | QHBoxLayout, QApplication) 6 | from PyQt5.QtGui import QRegExpValidator, QPalette, QColor 7 | from PyQt5.QtCore import QRegExp 8 | 9 | 10 | class Example(QWidget): 11 | 12 | def __init__(self): 13 | super().__init__() 14 | 15 | self.initUI() 16 | 17 | 18 | def initUI(self): 19 | 20 | hbox = QHBoxLayout() 21 | 22 | label = QLabel('HEX colour:', self) 23 | 24 | self.qle = QLineEdit(self) 25 | validator = QRegExpValidator(QRegExp("[0-9A-Fa-f]{6}")) 26 | self.qle.setValidator(validator) 27 | 28 | self.qle.editingFinished.connect(self.onEditingFinished) 29 | 30 | self.colLabel = QLabel(self) 31 | self.colLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) 32 | self.colLabel.setAutoFillBackground(True) 33 | 34 | hbox.addWidget(label) 35 | hbox.addSpacing(20) 36 | hbox.addWidget(self.qle) 37 | hbox.addSpacing(20) 38 | 39 | hbox.addWidget(self.colLabel) 40 | pal = QPalette() 41 | pal.setColor(QPalette.Background, QColor('#333333')) 42 | self.colLabel.setPalette(pal) 43 | 44 | self.setLayout(hbox) 45 | self.resize(450, 200) 46 | self.setWindowTitle('Validator') 47 | self.show() 48 | 49 | def onEditingFinished(self): 50 | 51 | pal = QPalette() 52 | pal.setColor(QPalette.Background, QColor(f'#{self.qle.text()}')) 53 | self.colLabel.setPalette(pal) 54 | 55 | 56 | def main(): 57 | 58 | app = QApplication(sys.argv) 59 | ex = Example() 60 | sys.exit(app.exec_()) 61 | 62 | 63 | if __name__ == '__main__': 64 | main() 65 | 66 | -------------------------------------------------------------------------------- /qlineedit/text_changed.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | 5 | from PyQt5.QtWidgets import (QApplication, QLabel, QLineEdit, 6 | QVBoxLayout, QWidget) 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | hbox = QVBoxLayout(self) 19 | 20 | self.lbl = QLabel(self) 21 | qle = QLineEdit(self) 22 | 23 | qle.textChanged[str].connect(self.onChanged) 24 | 25 | hbox.addWidget(self.lbl) 26 | hbox.addSpacing(20) 27 | hbox.addWidget(qle) 28 | 29 | self.resize(250, 200) 30 | self.setWindowTitle('QLineEdit') 31 | self.show() 32 | 33 | def onChanged(self, text): 34 | 35 | self.lbl.setText(text) 36 | self.lbl.adjustSize() 37 | 38 | 39 | def main(): 40 | 41 | app = QApplication(sys.argv) 42 | ex = Example() 43 | sys.exit(app.exec_()) 44 | 45 | 46 | if __name__ == '__main__': 47 | main() 48 | 49 | -------------------------------------------------------------------------------- /qlistwidget/clear_count.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | import sys 5 | from PyQt5.QtWidgets import (QListWidget, QPushButton, QWidget, QHBoxLayout, 6 | QMessageBox, QApplication, QVBoxLayout) 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | vbox = QVBoxLayout(self) 19 | hbox = QHBoxLayout() 20 | 21 | self.listWidget = QListWidget(self) 22 | 23 | self.listWidget.addItems(['sparrow', 'robin', 'crow', 'raven', 24 | 'woopecker', 'hummingbird']) 25 | 26 | clearBtn = QPushButton('Clear', self) 27 | clearBtn.clicked.connect(self.onClearClicked) 28 | 29 | countBtn = QPushButton('Count', self) 30 | countBtn.clicked.connect(self.onCountClicked) 31 | 32 | vbox.addWidget(self.listWidget) 33 | hbox.addWidget(clearBtn) 34 | hbox.addWidget(countBtn) 35 | vbox.addLayout(hbox) 36 | 37 | self.setLayout(vbox) 38 | 39 | self.setGeometry(300, 300, 350, 250) 40 | self.setWindowTitle('QListWidget') 41 | self.show() 42 | 43 | 44 | def onClearClicked(self): 45 | 46 | self.listWidget.clear() 47 | 48 | def onCountClicked(self): 49 | 50 | QMessageBox.information(self, "Info", 51 | f'# of birds {self.listWidget.count()}') 52 | 53 | 54 | def main(): 55 | 56 | app = QApplication(sys.argv) 57 | ex = Example() 58 | sys.exit(app.exec_()) 59 | 60 | 61 | if __name__ == '__main__': 62 | main() 63 | -------------------------------------------------------------------------------- /qlistwidget/simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import (QListWidget, QWidget, QMessageBox, 5 | QApplication, QVBoxLayout) 6 | 7 | 8 | class Example(QWidget): 9 | 10 | def __init__(self): 11 | super().__init__() 12 | 13 | self.initUI() 14 | 15 | def initUI(self): 16 | 17 | vbox = QVBoxLayout(self) 18 | 19 | listWidget = QListWidget(self) 20 | 21 | listWidget.addItem("sparrow") 22 | listWidget.addItem("robin") 23 | listWidget.addItem("crow") 24 | listWidget.addItem("raven") 25 | listWidget.addItem("woodpecker") 26 | listWidget.addItem("hummingbird") 27 | 28 | listWidget.itemDoubleClicked.connect(self.onClicked) 29 | 30 | vbox.addWidget(listWidget) 31 | self.setLayout(vbox) 32 | 33 | self.setGeometry(300, 300, 350, 250) 34 | self.setWindowTitle('QListWidget') 35 | self.show() 36 | 37 | def onClicked(self, item): 38 | 39 | QMessageBox.information(self, "Info", item.text()) 40 | 41 | 42 | def main(): 43 | 44 | app = QApplication(sys.argv) 45 | ex = Example() 46 | sys.exit(app.exec_()) 47 | 48 | 49 | if __name__ == '__main__': 50 | main() 51 | 52 | -------------------------------------------------------------------------------- /qlistwidget/sort_items.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import (QCheckBox, QListWidget, QPushButton, QWidget, 5 | QHBoxLayout, QApplication, QVBoxLayout) 6 | from PyQt5.QtCore import Qt 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | vbox = QVBoxLayout(self) 19 | hbox = QHBoxLayout() 20 | 21 | self.listWidget = QListWidget(self) 22 | 23 | self.listWidget.addItems(['sparrow', 'robin', 'crow', 'raven', 24 | 'woopecker', 'hummingbird']) 25 | 26 | self.sortOrder = QCheckBox('Ascending', self) 27 | 28 | sortBtn = QPushButton('Sort', self) 29 | sortBtn.clicked.connect(self.onSorted) 30 | 31 | vbox.addWidget(self.listWidget) 32 | hbox.addWidget(self.sortOrder) 33 | hbox.addWidget(sortBtn) 34 | vbox.addLayout(hbox) 35 | 36 | self.setLayout(vbox) 37 | 38 | self.setGeometry(300, 300, 350, 250) 39 | self.setWindowTitle('Sorting items') 40 | self.show() 41 | 42 | 43 | def onSorted(self): 44 | 45 | if self.sortOrder.isChecked(): 46 | order = Qt.AscendingOrder 47 | else: 48 | order = Qt.DescendingOrder 49 | 50 | self.listWidget.sortItems(order) 51 | 52 | 53 | def main(): 54 | 55 | app = QApplication(sys.argv) 56 | ex = Example() 57 | sys.exit(app.exec_()) 58 | 59 | 60 | if __name__ == '__main__': 61 | main() 62 | 63 | -------------------------------------------------------------------------------- /qradiobutton/radio_button.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QRadioButton, QHBoxLayout, QVBoxLayout, 5 | QLabel, QApplication) 6 | import sys 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | vbox = QVBoxLayout() 19 | hbox = QHBoxLayout() 20 | 21 | rb1 = QRadioButton("Large", self) 22 | rb1.toggled.connect(self.updateLabel) 23 | 24 | rb2 = QRadioButton("Middle", self) 25 | rb2.toggled.connect(self.updateLabel) 26 | 27 | rb3 = QRadioButton("Small", self) 28 | rb3.toggled.connect(self.updateLabel) 29 | 30 | self.label = QLabel('', self) 31 | 32 | hbox.addWidget(rb1) 33 | hbox.addWidget(rb2) 34 | hbox.addWidget(rb3) 35 | 36 | vbox.addSpacing(15) 37 | 38 | vbox.addLayout(hbox) 39 | vbox.addWidget(self.label) 40 | 41 | self.setLayout(vbox) 42 | 43 | self.setGeometry(300, 300, 350, 250) 44 | self.setWindowTitle('QRadioButton') 45 | self.show() 46 | 47 | def updateLabel(self, value): 48 | 49 | rbtn = self.sender() 50 | 51 | if rbtn.isChecked() == True: 52 | self.label.setText(rbtn.text()) 53 | 54 | 55 | def main(): 56 | 57 | app = QApplication(sys.argv) 58 | ex = Example() 59 | sys.exit(app.exec_()) 60 | 61 | 62 | if __name__ == '__main__': 63 | main() 64 | -------------------------------------------------------------------------------- /qradiobutton/radio_button2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QRadioButton, QHBoxLayout, QVBoxLayout, 5 | QButtonGroup, QLabel, QApplication) 6 | import sys 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | vbox = QVBoxLayout() 19 | 20 | hbox1 = QHBoxLayout() 21 | 22 | bg1 = QButtonGroup(self) 23 | 24 | rb1 = QRadioButton("Large", self) 25 | rb1.toggled.connect(self.updateLabel1) 26 | 27 | rb2 = QRadioButton("Middle", self) 28 | rb2.toggled.connect(self.updateLabel1) 29 | 30 | rb3 = QRadioButton("Small", self) 31 | rb3.toggled.connect(self.updateLabel1) 32 | 33 | 34 | hbox2 = QHBoxLayout() 35 | bg2 = QButtonGroup(self) 36 | 37 | rb4 = QRadioButton("Red", self) 38 | rb4.toggled.connect(self.updateLabel2) 39 | 40 | rb5 = QRadioButton("Green", self) 41 | rb5.toggled.connect(self.updateLabel2) 42 | 43 | rb6 = QRadioButton("Blue", self) 44 | rb6.toggled.connect(self.updateLabel2) 45 | 46 | self.label1 = QLabel('', self) 47 | self.label2 = QLabel('', self) 48 | 49 | bg1.addButton(rb1) 50 | bg1.addButton(rb2) 51 | bg1.addButton(rb3) 52 | 53 | bg2.addButton(rb4) 54 | bg2.addButton(rb5) 55 | bg2.addButton(rb6) 56 | 57 | hbox1.addWidget(rb1) 58 | hbox1.addWidget(rb2) 59 | hbox1.addWidget(rb3) 60 | 61 | hbox2.addWidget(rb4) 62 | hbox2.addWidget(rb5) 63 | hbox2.addWidget(rb6) 64 | 65 | vbox.addLayout(hbox1) 66 | vbox.addLayout(hbox2) 67 | vbox.addWidget(self.label1) 68 | vbox.addWidget(self.label2) 69 | 70 | self.setLayout(vbox) 71 | 72 | self.setGeometry(300, 300, 350, 250) 73 | self.setWindowTitle('QRadioButton') 74 | self.show() 75 | 76 | def updateLabel1(self, value): 77 | 78 | rbtn = self.sender() 79 | 80 | if rbtn.isChecked() == True: 81 | self.label1.setText(rbtn.text()) 82 | 83 | def updateLabel2(self, value): 84 | 85 | rbtn = self.sender() 86 | 87 | if rbtn.isChecked() == True: 88 | self.label2.setText(rbtn.text()) 89 | 90 | 91 | def main(): 92 | 93 | app = QApplication(sys.argv) 94 | ex = Example() 95 | sys.exit(app.exec_()) 96 | 97 | 98 | if __name__ == '__main__': 99 | main() 100 | -------------------------------------------------------------------------------- /qshortcut/button.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QApplication, 5 | QPushButton, QMessageBox, QSizePolicy) 6 | from PyQt5.QtGui import QKeySequence 7 | from PyQt5.QtCore import Qt 8 | import sys 9 | 10 | 11 | class Example(QWidget): 12 | 13 | def __init__(self): 14 | super().__init__() 15 | 16 | self.initUI() 17 | 18 | 19 | def initUI(self): 20 | 21 | hbox = QHBoxLayout() 22 | 23 | msgBtn = QPushButton('&Show message', self) 24 | msgBtn.clicked.connect(lambda : QMessageBox.information(self, 25 | 'Message', 'Information message')) 26 | 27 | hbox.addWidget(msgBtn) 28 | msgBtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) 29 | hbox.setAlignment(Qt.AlignLeft) 30 | 31 | self.setLayout(hbox) 32 | 33 | self.move(300, 300) 34 | self.setWindowTitle('Shortcuts') 35 | self.show() 36 | 37 | 38 | def main(): 39 | 40 | app = QApplication(sys.argv) 41 | ex = Example() 42 | sys.exit(app.exec_()) 43 | 44 | 45 | if __name__ == '__main__': 46 | main() 47 | -------------------------------------------------------------------------------- /qshortcut/menu.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication 5 | from PyQt5.QtGui import QIcon 6 | 7 | 8 | class Example(QMainWindow): 9 | 10 | def __init__(self): 11 | super().__init__() 12 | 13 | self.initUI() 14 | 15 | def initUI(self): 16 | 17 | exitAct = QAction(QIcon('exit.png'), '&Exit', self) 18 | exitAct.setShortcut('Ctrl+Q') 19 | exitAct.setStatusTip('Exit application') 20 | exitAct.triggered.connect(qApp.quit) 21 | 22 | self.statusBar() 23 | 24 | menubar = self.menuBar() 25 | fileMenu = menubar.addMenu('&File') 26 | fileMenu.addAction(exitAct) 27 | 28 | self.setGeometry(300, 300, 300, 200) 29 | self.setWindowTitle('Simple menu') 30 | self.show() 31 | 32 | 33 | def main(): 34 | 35 | app = QApplication(sys.argv) 36 | ex = Example() 37 | sys.exit(app.exec_()) 38 | 39 | 40 | if __name__ == '__main__': 41 | main() 42 | -------------------------------------------------------------------------------- /qshortcut/simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import QWidget, QShortcut, QApplication, QMessageBox 5 | from PyQt5.QtGui import QKeySequence 6 | import sys 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | 17 | def initUI(self): 18 | 19 | self.msgSc = QShortcut(QKeySequence('Ctrl+M'), self) 20 | 21 | self.msgSc.activated.connect(lambda : QMessageBox.information(self, 22 | 'Message', 'Ctrl + M initiated')) 23 | 24 | self.quitSc = QShortcut(QKeySequence('Ctrl+Q'), self) 25 | self.quitSc.activated.connect(QApplication.instance().quit) 26 | 27 | self.setGeometry(300, 300, 300, 200) 28 | self.setWindowTitle('Shortcuts') 29 | self.show() 30 | 31 | 32 | def main(): 33 | 34 | app = QApplication(sys.argv) 35 | ex = Example() 36 | sys.exit(app.exec_()) 37 | 38 | 39 | if __name__ == '__main__': 40 | main() 41 | -------------------------------------------------------------------------------- /qslider/horizontal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QSlider, QHBoxLayout, 5 | QLabel, QApplication) 6 | from PyQt5.QtCore import Qt 7 | from PyQt5.QtGui import QPixmap 8 | import sys 9 | 10 | 11 | class Example(QWidget): 12 | 13 | def __init__(self): 14 | super().__init__() 15 | 16 | self.initUI() 17 | 18 | def initUI(self): 19 | 20 | hbox = QHBoxLayout() 21 | 22 | sld = QSlider(Qt.Horizontal, self) 23 | sld.setRange(0, 100) 24 | sld.setFocusPolicy(Qt.NoFocus) 25 | sld.setPageStep(5) 26 | 27 | sld.valueChanged.connect(self.updateLabel) 28 | 29 | self.label = QLabel('0', self) 30 | self.label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter) 31 | self.label.setMinimumWidth(80) 32 | 33 | hbox.addWidget(sld) 34 | hbox.addSpacing(15) 35 | hbox.addWidget(self.label) 36 | 37 | self.setLayout(hbox) 38 | 39 | self.setGeometry(300, 300, 350, 250) 40 | self.setWindowTitle('QSlider') 41 | self.show() 42 | 43 | def updateLabel(self, value): 44 | 45 | self.label.setText(str(value)) 46 | 47 | 48 | def main(): 49 | 50 | app = QApplication(sys.argv) 51 | ex = Example() 52 | sys.exit(app.exec_()) 53 | 54 | 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /qslider/max.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qslider/max.png -------------------------------------------------------------------------------- /qslider/med.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qslider/med.png -------------------------------------------------------------------------------- /qslider/min.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qslider/min.png -------------------------------------------------------------------------------- /qslider/mute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/PyQt-Examples/e2326dafaf745bed7d73b65da9416c4c522d1499/qslider/mute.png -------------------------------------------------------------------------------- /qslider/vertical.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QSlider, QHBoxLayout, 5 | QLabel, QApplication) 6 | from PyQt5.QtCore import Qt 7 | from PyQt5.QtGui import QPixmap 8 | import sys 9 | 10 | 11 | class Example(QWidget): 12 | 13 | def __init__(self): 14 | super().__init__() 15 | 16 | self.initUI() 17 | 18 | def initUI(self): 19 | 20 | hbox = QHBoxLayout() 21 | 22 | sld = QSlider(Qt.Vertical, self) 23 | sld.setFocusPolicy(Qt.NoFocus) 24 | 25 | sld.setRange(0, 100) 26 | sld.setPageStep(5) 27 | 28 | sld.valueChanged.connect(self.changeValue) 29 | 30 | self.label = QLabel("0", self) 31 | self.label.setStyleSheet('QLabel { background: #007AA5; border-radius: 3px;}') 32 | self.label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter) 33 | self.label.setMinimumWidth(80) 34 | 35 | hbox.addStretch() 36 | hbox.addWidget(sld) 37 | hbox.addSpacing(15) 38 | hbox.addWidget(self.label) 39 | hbox.addStretch() 40 | 41 | self.setLayout(hbox) 42 | 43 | self.setGeometry(300, 300, 350, 250) 44 | self.setWindowTitle('QSlider') 45 | self.show() 46 | 47 | def changeValue(self, value): 48 | 49 | self.label.setText(str(value)) 50 | 51 | def main(): 52 | 53 | app = QApplication(sys.argv) 54 | ex = Example() 55 | sys.exit(app.exec_()) 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /qslider/volume_control.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QSlider, QHBoxLayout, 5 | QLabel, QApplication) 6 | from PyQt5.QtCore import Qt 7 | from PyQt5.QtGui import QPixmap 8 | import sys 9 | 10 | 11 | class Example(QWidget): 12 | 13 | def __init__(self): 14 | super().__init__() 15 | 16 | self.initUI() 17 | 18 | def initUI(self): 19 | 20 | hbox = QHBoxLayout() 21 | 22 | sld = QSlider(Qt.Horizontal, self) 23 | sld.setFocusPolicy(Qt.NoFocus) 24 | sld.valueChanged.connect(self.changeValue) 25 | 26 | self.label = QLabel(self) 27 | self.label.setPixmap(QPixmap('mute.png')) 28 | 29 | hbox.addWidget(sld) 30 | hbox.addSpacing(15) 31 | hbox.addWidget(self.label) 32 | 33 | self.setLayout(hbox) 34 | 35 | self.move(300, 300) 36 | self.setWindowTitle('Volume control') 37 | self.show() 38 | 39 | def changeValue(self, value): 40 | 41 | if value == 0: 42 | 43 | self.label.setPixmap(QPixmap('mute.png')) 44 | elif 0 < value <= 30: 45 | 46 | self.label.setPixmap(QPixmap('min.png')) 47 | elif 30 < value < 80: 48 | 49 | self.label.setPixmap(QPixmap('med.png')) 50 | else: 51 | 52 | self.label.setPixmap(QPixmap('max.png')) 53 | 54 | 55 | def main(): 56 | 57 | app = QApplication(sys.argv) 58 | ex = Example() 59 | sys.exit(app.exec_()) 60 | 61 | 62 | if __name__ == '__main__': 63 | main() 64 | -------------------------------------------------------------------------------- /qspinbox/show_value.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QSpinBox, QHBoxLayout, QMessageBox, 5 | QPushButton, QApplication, QSizePolicy) 6 | import sys 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | hbox = QHBoxLayout() 19 | 20 | self.sbox = QSpinBox(self) 21 | 22 | btn = QPushButton('Show', self) 23 | btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) 24 | btn.clicked.connect(self.showSpinboxValue) 25 | 26 | hbox.addWidget(self.sbox) 27 | hbox.addSpacing(15) 28 | hbox.addWidget(btn) 29 | 30 | self.setLayout(hbox) 31 | 32 | self.setGeometry(300, 300, 350, 250) 33 | self.setWindowTitle('QSpinBox') 34 | self.show() 35 | 36 | def showSpinboxValue(self): 37 | 38 | val = self.sbox.value() 39 | QMessageBox.information(self, 'Value', f'Value: {val}') 40 | 41 | 42 | def main(): 43 | 44 | app = QApplication(sys.argv) 45 | ex = Example() 46 | sys.exit(app.exec_()) 47 | 48 | 49 | if __name__ == '__main__': 50 | main() 51 | -------------------------------------------------------------------------------- /qspinbox/spinbox.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QSpinBox, QHBoxLayout, 5 | QLabel, QApplication) 6 | import sys 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | hbox = QHBoxLayout() 19 | 20 | sbox = QSpinBox(self) 21 | 22 | sbox.valueChanged.connect(self.updateLabel) 23 | 24 | self.label = QLabel('0', self) 25 | 26 | hbox.addWidget(sbox) 27 | hbox.addSpacing(15) 28 | hbox.addWidget(self.label) 29 | 30 | self.setLayout(hbox) 31 | 32 | self.setGeometry(300, 300, 350, 250) 33 | self.setWindowTitle('QSpinBox') 34 | self.show() 35 | 36 | def updateLabel(self, value): 37 | 38 | self.label.setText(str(value)) 39 | 40 | 41 | def main(): 42 | 43 | app = QApplication(sys.argv) 44 | ex = Example() 45 | sys.exit(app.exec_()) 46 | 47 | 48 | if __name__ == '__main__': 49 | main() 50 | -------------------------------------------------------------------------------- /qspinbox/spinbox2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | from PyQt5.QtWidgets import (QWidget, QSpinBox, QHBoxLayout, 5 | QLabel, QApplication) 6 | import sys 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | hbox = QHBoxLayout() 19 | 20 | sbox = QSpinBox(self) 21 | sbox.setRange(0, 200) 22 | sbox.setSingleStep(5) 23 | sbox.setSuffix(' km') 24 | 25 | sbox.valueChanged.connect(self.updateLabel) 26 | 27 | self.label = QLabel('0', self) 28 | 29 | hbox.addWidget(sbox) 30 | hbox.addSpacing(15) 31 | hbox.addWidget(self.label) 32 | 33 | self.setLayout(hbox) 34 | 35 | self.setGeometry(300, 300, 350, 250) 36 | self.setWindowTitle('QSpinBox') 37 | self.show() 38 | 39 | def updateLabel(self, value): 40 | 41 | self.label.setText(str(value)) 42 | 43 | 44 | def main(): 45 | 46 | app = QApplication(sys.argv) 47 | ex = Example() 48 | sys.exit(app.exec_()) 49 | 50 | 51 | if __name__ == '__main__': 52 | main() 53 | -------------------------------------------------------------------------------- /qtooltip/mouse_positions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | import sys 5 | from PyQt5.QtCore import Qt 6 | from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QToolTip 7 | 8 | 9 | class MyWidget(QWidget): 10 | 11 | def __init__(self, parent): 12 | super().__init__() 13 | 14 | 15 | self.setAttribute(Qt.WA_StyledBackground, True) 16 | self.setStyleSheet('QWidget { background: #007AA5; border-radius: 3px;}') 17 | self.setMouseTracking(True) 18 | 19 | 20 | def mouseMoveEvent(self, e): 21 | 22 | self.x = e.x() 23 | self.y = e.y() 24 | 25 | p = self.mapToGlobal(e.pos()) 26 | 27 | QToolTip.showText(p, f'{self.x}:{self.y}') 28 | 29 | 30 | class Example(QWidget): 31 | 32 | def __init__(self): 33 | super().__init__() 34 | 35 | self.initUI() 36 | 37 | def initUI(self): 38 | 39 | grid = QGridLayout() 40 | 41 | self.w1 = MyWidget(self) 42 | self.w2 = MyWidget(self) 43 | self.w3 = MyWidget(self) 44 | self.w4 = MyWidget(self) 45 | self.w5 = MyWidget(self) 46 | self.w6 = MyWidget(self) 47 | self.w7 = MyWidget(self) 48 | self.w8 = MyWidget(self) 49 | self.w9 = MyWidget(self) 50 | 51 | grid.addWidget(self.w1, 0, 0) 52 | grid.addWidget(self.w2, 0, 1) 53 | grid.addWidget(self.w3, 0, 2) 54 | grid.addWidget(self.w4, 1, 0) 55 | grid.addWidget(self.w5, 1, 1) 56 | grid.addWidget(self.w6, 1, 2) 57 | grid.addWidget(self.w7, 2, 0) 58 | grid.addWidget(self.w8, 2, 1) 59 | grid.addWidget(self.w9, 2, 2) 60 | 61 | self.setLayout(grid) 62 | 63 | self.setGeometry(300, 300, 500, 350) 64 | self.setWindowTitle('Mouse positions') 65 | self.show() 66 | 67 | 68 | def main(): 69 | 70 | app = QApplication(sys.argv) 71 | ex = Example() 72 | sys.exit(app.exec_()) 73 | 74 | 75 | if __name__ == '__main__': 76 | main() 77 | -------------------------------------------------------------------------------- /qtooltip/shapes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | 5 | from PyQt5.QtCore import QEvent, QPoint, QPointF, Qt 6 | from PyQt5.QtGui import QColor, QPainter, QPainterPath, QPolygonF 7 | from PyQt5.QtWidgets import QApplication, QToolTip, QWidget 8 | 9 | 10 | class Shape(object): 11 | 12 | def __init__(self): 13 | 14 | self.mypath = QPainterPath() 15 | self.col = QColor() 16 | self.toottip = '' 17 | 18 | def path(self): 19 | return self.mypath 20 | 21 | def color(self): 22 | return self.col 23 | 24 | def toolTip(self): 25 | return self.toottip 26 | 27 | def setPath(self, path): 28 | self.mypath = path 29 | 30 | def setToolTip(self, tooltip): 31 | self.toottip = tooltip 32 | 33 | def setPosition(self, position): 34 | self.pos = position 35 | 36 | def setColor(self, color): 37 | self.col = color 38 | 39 | 40 | class Example(QWidget): 41 | 42 | def __init__(self): 43 | super(Example, self).__init__() 44 | 45 | self.initUI() 46 | 47 | def initUI(self): 48 | 49 | self.circlePath = QPainterPath() 50 | self.squarePath = QPainterPath() 51 | self.trianglePath = QPainterPath() 52 | self.pentagonPath = QPainterPath() 53 | self.shapes = [] 54 | 55 | self.circlePath.addEllipse(30, 50, 100, 100) 56 | self.squarePath.addRect(180, 50, 100, 100) 57 | 58 | x = self.trianglePath.currentPosition().x() 59 | y = self.trianglePath.currentPosition().y() 60 | 61 | self.trianglePath.moveTo(320, 150) 62 | self.trianglePath.lineTo(450, 150) 63 | self.trianglePath.lineTo(415, 50) 64 | self.trianglePath.lineTo(320, 150) 65 | 66 | polygon = QPolygonF() 67 | polygon.append(QPoint(130, 240)) 68 | polygon.append(QPoint(100, 280)) 69 | polygon.append(QPoint(50, 280)) 70 | polygon.append(QPoint(20, 240)) 71 | polygon.append(QPoint(75, 200)) 72 | 73 | self.pentagonPath.addPolygon(polygon) 74 | 75 | self.createShape(self.circlePath, 'Circle', QColor('#c72602')) 76 | self.createShape(self.squarePath, 'Square', QColor('#32a852')) 77 | self.createShape(self.trianglePath, 'Triangle', QColor('#205f6e')) 78 | self.createShape(self.pentagonPath, 'Pentagon', QColor('#e0b107')) 79 | 80 | self.setWindowTitle('Shapes') 81 | self.resize(480, 300) 82 | self.show() 83 | 84 | def event(self, e): 85 | 86 | if e.type() == QEvent.ToolTip: 87 | 88 | index = self.itemIndexAt(e.pos()) 89 | 90 | if index != -1: 91 | QToolTip.showText(e.globalPos(), 92 | self.shapes[index].toolTip()) 93 | else: 94 | QToolTip.hideText() 95 | e.ignore() 96 | 97 | return True 98 | 99 | return super(Example, self).event(e) 100 | 101 | 102 | def paintEvent(self, e): 103 | 104 | painter = QPainter(self) 105 | painter.setRenderHint(QPainter.Antialiasing) 106 | painter.setPen(Qt.NoPen) 107 | 108 | for shape in self.shapes: 109 | 110 | painter.setBrush(shape.color()) 111 | painter.drawPath(shape.path()) 112 | 113 | 114 | def itemIndexAt(self, pos): 115 | 116 | for i in range(len(self.shapes)): 117 | 118 | item = self.shapes[i] 119 | 120 | if item.path().contains(QPointF(pos)): 121 | 122 | return i 123 | 124 | return -1 125 | 126 | 127 | def createShape(self, path, toolTip, color): 128 | 129 | shape = Shape() 130 | shape.setPath(path) 131 | shape.setToolTip(toolTip) 132 | shape.setColor(color) 133 | 134 | self.shapes.append(shape) 135 | 136 | 137 | def main(): 138 | 139 | app = QApplication(sys.argv) 140 | ex = Example() 141 | sys.exit(app.exec_()) 142 | 143 | 144 | if __name__ == '__main__': 145 | main() 146 | -------------------------------------------------------------------------------- /qtooltip/simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import QWidget, QToolTip, QPushButton, QApplication 5 | from PyQt5.QtGui import QFont 6 | 7 | 8 | class Example(QWidget): 9 | 10 | def __init__(self): 11 | super().__init__() 12 | 13 | self.initUI() 14 | 15 | 16 | def initUI(self): 17 | 18 | QToolTip.setFont(QFont('SansSerif', 10)) 19 | 20 | self.setToolTip('This is a QWidget widget') 21 | 22 | btn = QPushButton('Button', self) 23 | btn.setToolTip('This is a QPushButton widget') 24 | btn.resize(btn.sizeHint()) 25 | btn.move(50, 50) 26 | 27 | self.setGeometry(300, 300, 300, 200) 28 | self.setWindowTitle('Tooltips') 29 | self.show() 30 | 31 | 32 | def main(): 33 | 34 | app = QApplication(sys.argv) 35 | ex = Example() 36 | sys.exit(app.exec_()) 37 | 38 | 39 | if __name__ == '__main__': 40 | main() 41 | -------------------------------------------------------------------------------- /qtooltip/styled.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import QWidget, QToolTip, QApplication 5 | from PyQt5.QtGui import QFont, QPalette, QColor 6 | 7 | 8 | class Example(QWidget): 9 | 10 | def __init__(self): 11 | super().__init__() 12 | 13 | self.initUI() 14 | 15 | 16 | def initUI(self): 17 | 18 | self.setStyleSheet('''QToolTip { 19 | background-color: #8ad4ff; 20 | color: black; 21 | border: #8ad4ff solid 1px 22 | }''') 23 | 24 | QToolTip.setFont(QFont('Georgia', 11)) 25 | 26 | pal = QPalette() 27 | pal.setColor(QPalette.Background, QColor('#348ceb')) 28 | self.setPalette(pal) 29 | 30 | self.setToolTip('This is QWidget') 31 | 32 | self.setGeometry(300, 300, 300, 200) 33 | self.setWindowTitle('Styled QToolTip') 34 | self.show() 35 | 36 | 37 | def main(): 38 | 39 | app = QApplication(sys.argv) 40 | ex = Example() 41 | sys.exit(app.exec_()) 42 | 43 | 44 | if __name__ == '__main__': 45 | main() 46 | -------------------------------------------------------------------------------- /qwebengineview/back_forw.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtCore import QUrl 5 | from PyQt5.QtGui import QIcon 6 | from PyQt5.QtWidgets import (QApplication, QLineEdit, QMainWindow, 7 | QPushButton, QToolBar) 8 | from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView 9 | 10 | 11 | class Example(QMainWindow): 12 | 13 | def __init__(self): 14 | super(Example, self).__init__() 15 | 16 | self.initUI() 17 | 18 | 19 | def initUI(self): 20 | 21 | self.toolBar = QToolBar(self) 22 | self.addToolBar(self.toolBar) 23 | 24 | self.backBtn = QPushButton(self) 25 | self.backBtn.setEnabled(False) 26 | 27 | self.backBtn.setIcon(QIcon(':/qt-project.org/styles/commonstyle/images/left-32.png')) 28 | self.backBtn.clicked.connect(self.back) 29 | self.toolBar.addWidget(self.backBtn) 30 | 31 | self.forBtn = QPushButton(self) 32 | self.forBtn.setEnabled(False) 33 | self.forBtn.setIcon(QIcon(':/qt-project.org/styles/commonstyle/images/right-32.png')) 34 | 35 | self.forBtn.clicked.connect(self.forward) 36 | self.toolBar.addWidget(self.forBtn) 37 | 38 | self.address = QLineEdit(self) 39 | self.address.returnPressed.connect(self.load) 40 | self.toolBar.addWidget(self.address) 41 | 42 | self.webEngineView = QWebEngineView(self) 43 | self.setCentralWidget(self.webEngineView) 44 | 45 | self.webEngineView.page().urlChanged.connect(self.onLoadFinished) 46 | 47 | self.webEngineView.page().titleChanged.connect(self.setWindowTitle) 48 | self.webEngineView.page().urlChanged.connect(self.urlChanged) 49 | 50 | self.setGeometry(300, 300, 500, 400) 51 | self.setWindowTitle('QWebEnginePage') 52 | self.show() 53 | 54 | def onLoadFinished(self): 55 | 56 | if self.webEngineView.history().canGoBack(): 57 | self.backBtn.setEnabled(True) 58 | else: 59 | self.backBtn.setEnabled(False) 60 | 61 | if self.webEngineView.history().canGoForward(): 62 | self.forBtn.setEnabled(True) 63 | else: 64 | self.forBtn.setEnabled(False) 65 | 66 | 67 | def load(self): 68 | 69 | url = QUrl.fromUserInput(self.address.text()) 70 | 71 | if url.isValid(): 72 | self.webEngineView.load(url) 73 | 74 | def back(self): 75 | self.webEngineView.page().triggerAction(QWebEnginePage.Back) 76 | 77 | def forward(self): 78 | self.webEngineView.page().triggerAction(QWebEnginePage.Forward) 79 | 80 | def urlChanged(self, url): 81 | self.address.setText(url.toString()) 82 | 83 | 84 | def main(): 85 | 86 | app = QApplication(sys.argv) 87 | ex = Example() 88 | sys.exit(app.exec_()) 89 | 90 | 91 | if __name__ == '__main__': 92 | main() 93 | -------------------------------------------------------------------------------- /qwebengineview/export_pdf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import (QHBoxLayout, QPushButton, QWidget, 5 | QApplication, QVBoxLayout, QMessageBox) 6 | from PyQt5.QtWidgets import QApplication 7 | from PyQt5.QtWebEngineWidgets import QWebEngineView 8 | 9 | 10 | class Example(QWidget): 11 | 12 | def __init__(self): 13 | super().__init__() 14 | 15 | self.initUI() 16 | 17 | def initUI(self): 18 | 19 | vbox = QVBoxLayout(self) 20 | hbox = QHBoxLayout() 21 | 22 | self.webEngineView = QWebEngineView() 23 | self.loadPage() 24 | 25 | expBtn = QPushButton('Export', self) 26 | expBtn.clicked.connect(self.onClicked) 27 | 28 | hbox.addWidget(expBtn) 29 | 30 | vbox.addWidget(self.webEngineView) 31 | vbox.addLayout(hbox) 32 | self.setLayout(vbox) 33 | 34 | self.setGeometry(300, 300, 350, 250) 35 | self.setWindowTitle('QWebEngineView') 36 | self.show() 37 | 38 | def onClicked(self): 39 | 40 | self.webEngineView.page().printToPdf('myfile.pdf') 41 | QMessageBox.information(self, 'info', 'page exported') 42 | 43 | def loadPage(self): 44 | 45 | with open('test.html', 'r') as f: 46 | 47 | html = f.read() 48 | self.webEngineView.setHtml(html) 49 | 50 | def main(): 51 | 52 | app = QApplication(sys.argv) 53 | ex = Example() 54 | sys.exit(app.exec_()) 55 | 56 | 57 | if __name__ == '__main__': 58 | main() 59 | -------------------------------------------------------------------------------- /qwebengineview/simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout 5 | from PyQt5.QtWidgets import QApplication 6 | from PyQt5.QtWebEngineWidgets import QWebEngineView 7 | 8 | 9 | class Example(QWidget): 10 | 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.initUI() 15 | 16 | def initUI(self): 17 | 18 | vbox = QVBoxLayout(self) 19 | 20 | self.webEngineView = QWebEngineView() 21 | self.loadPage() 22 | 23 | vbox.addWidget(self.webEngineView) 24 | 25 | self.setLayout(vbox) 26 | 27 | self.setGeometry(300, 300, 350, 250) 28 | self.setWindowTitle('QWebEngineView') 29 | self.show() 30 | 31 | def loadPage(self): 32 | 33 | with open('test.html', 'r') as f: 34 | 35 | html = f.read() 36 | self.webEngineView.setHtml(html) 37 | 38 | def main(): 39 | 40 | app = QApplication(sys.argv) 41 | ex = Example() 42 | sys.exit(app.exec_()) 43 | 44 | 45 | if __name__ == '__main__': 46 | main() 47 | -------------------------------------------------------------------------------- /qwebengineview/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |10 | This is a simple HTML page. 11 |
12 | 13 | 14 | --------------------------------------------------------------------------------