<jupyter_start><jupyter_text>An application icon<jupyter_code># The application icon is a small image which is usually displayed in the top left corner of the titlebar.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    
    def initUI(self):
        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('web.png'))
        self.show()
            
def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
        
if __name__ == '__main__':
    main()
# In this example we create a new class called Example. The Example class inherits from the QWidget class.
# This means that we call two constructors: the first one for the Example class and the second one for the inherited class.
# The super() method returns the parent object of the Example class and we call its constructor.
# The __init__() method is a constructor method in Python language.
# The creation of the GUI is delegated to the initUI() method.
# setGeometry(), setWindowTitle() and setWindowIcon() all these three methods have been inherited from the QWidget class.
# The setGeometry() locates the iwndow on the screen and sets it size. It combines the resize() adn move() methods in one method.
# To have icon you must have it downloaded and renamed to 'web.png'.<jupyter_output><empty_output><jupyter_text>Showing a tooltip in PyQt5<jupyter_code>import sys
from PyQt5.QtWidgets import QWidget, QToolTip, QPushButton, QApplication
from PyQt5.QtGui import QFont
class Example2(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif', 10))
        self.setToolTip('This is a <b>QWidget</b> widget')

        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Tooltips')
        self.show()


def main():
    app = QApplication(sys.argv)
    ex = Example2()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
# In this example, we show a tooltip for two PyQt5 widgets.
# To create a tooltip, we call the setTooltip() method. We can use rich text formatting.
# We create a push button widget and set a tooltip for it.
# The button is being resized and moved on the window. The sizeHint() method gives a recommended size for the button.<jupyter_output><empty_output><jupyter_text>Closing a window<jupyter_code>import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
class Example3(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):
        qbtn = QPushButton('Quit', self)
        qbtn.clicked.connect(QApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)
        
        self.setGeometry(300, 300, 300, 250)
        self.setWindowTitle('Quit button')
        self.show()
        
def main():
    app = QApplication(sys.argv)
    ex = Example3()
    sys.exit(app.exec_())
    
if __name__ == '__main__':
    main()
# If you run this code in Jupyter Notebook, after clickign Quit Button you have to Restart Kernel.    
# In this example we create a quit button. Upon clicking on the button, the application terminates.
# The button is an instance of the QPushButton class. 
# The first parameter of the constructor is the label of the button. The second parameter is the parent widget.
# The parent widget is the Example widget, which is a QWidget by inheritance.<jupyter_output><empty_output><jupyter_text>PyQt5 message box<jupyter_code>import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
class Example4 (QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Message box')
        self.show()
        
    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message', 
                                     "Are you sure to quit?", QMessageBox.Yes | 
                                     QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

def main():
    app = QApplication(sys.argv)
    ex = Example4()
    sys.exit(app.exec_())
    
if __name__ == '__main__':
    main()
# If we close a QWidget, the QCLoseEvent is generated. To modify the widget behaviour we need to reimpement the closeEvent() event handler.
# The first string in message box appears on the titlebar.
# The second string is the message text displayed by the dialog.
# The third argument specifies the combination of buttons appearing in the dialog.
# The last parameter is the default button. It is the button which has initially the keyboard focus.<jupyter_output><empty_output><jupyter_text>Centering window on the screen<jupyter_code>import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication
# This example shows how we can center a window on the desktop screen.
class Example5 (QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    
    def initUI(self):
        self.resize(250, 150)
        self.center()
        self.setWindowTitle('Center')
        self.show()
        
    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
        
def main():
    app = QApplication(sys.argv)
    ex = Example5()
    sys.exit(app.exec_())
    
if __name__ == '__main__':
    main()
# The QDesktopWidget class provides information about the user's desktop, including the screen size.
# We get the rectangle specifying the geometry of the main window. This includes any window frame.
# Our rectangle has already its width and height. Now we set the center of the rectangle to the center of the screen.
# The rectangle's size is unchanged.
# We move the top-left point of the application window to the top-left point of the qr rectangle, thus centering the window on our screen.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08933312509761049, 'fraction_numerical': 0.012806496954552554, 'mean_word_length': 3.115681233933162, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '201701', 'license': 'MIT', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1840, 'n_words': 683, 'path': '02 First programs in PyQt5.ipynb', 'repo_name': 'Szymon-Budziak/PyQt5', 'size': '6403'}