[GUI with Python] PyQt5-The first step-

Last time continued

First programs in PyQt5 I will summarize this site roughly in Japanese.

【Screen display】

Simple_example.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == '__main__':
    
    #Objects that must be created
    app = QApplication(sys.argv)

    #Creating a widget object(The screen)
    w = QWidget()
    #Set the screen width to 250px and height to 150px
    w.resize(250, 150)
    # x=300,y=Move screen to 300 locations
    w.move(300, 300)
    #Set title
    w.setWindowTitle('Simple')
    #Screen display
    w.show()
    #Quit the program cleanly
    sys.exit(app.exec_())
simple.png

[Add and display icons]

An_application_icon.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon


class Example(QWidget):
    #constructor
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        # setGeometry(x,y,Width,height)
        self.setGeometry(300, 300, 300, 220)
        #Title setting
        self.setWindowTitle('Icon')
        #Set the icon at the top left of the screen
        self.setWindowIcon(QIcon('imoyokan.jpg'))        
    
        #Screen display
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  
icon.png

[Speech balloon]

Showing_a_tooltip.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import (QWidget, QToolTip, 
    QPushButton, QApplication)
from PyQt5.QtGui import QFont    


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        #Use 10px sans serif font for callouts
        QToolTip.setFont(QFont('SansSerif', 10))
        
        #Screen balloon settings
        self.setToolTip('This is a <b>QWidget</b> widget')
        
        #Button making
        btn = QPushButton('Button', self)
        #Button callout settings
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        #Automatically set the button size to a nice feeling
        btn.resize(btn.sizeHint())
        #Button position setting
        btn.move(50, 50)       
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Tooltips')    
        self.show()
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

tooltips1.png tooltips2.png

[Close the screen with the button]

Closing_a_window.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()        
        
    def initUI(self):               
        
        #The first argument of QPushButton is the label
        #The second argument of QPushButton is the parent widget(Example widget inherited by QWidget)
        qbtn = QPushButton('Quit', self)
        #Click the Quit button to close the screen
        qbtn.clicked.connect(QCoreApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)       
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Quit button')    
        self.show()
   
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
quit.png

[Confirmation screen display when the screen is closed]

Message_Box.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        
        self.setGeometry(300, 300, 250, 150)        
        self.setWindowTitle('Message box')    
        self.show()
        
    #Event handler that displays a confirmation screen when the screen is closed
    def closeEvent(self, event):
        
        #Various message screen settings
        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()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
message.png

[Screen display in the middle of the desktop window]

Centering_window_on_the_screen.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication


class Example(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):

        #Create a rectangle qr with information about the entire window
        qr = self.frameGeometry()
        #Get the center of the window
        cp = QDesktopWidget().availableGeometry().center()
        #Move the rectangle qr to the center of the window
        qr.moveCenter(cp)
        #Align the upper left of the rectangle qr with the upper left of the screen
        self.move(qr.topLeft())
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  

Next time will try Menus and toolbars roughly.

Recommended Posts

[GUI with Python] PyQt5-The first step-
Web scraping with Python First step
First neuron simulation with NEURON + Python
[GUI with Python] PyQt5-Layout management-
[GUI with Python] PyQt5 -Preparation-
[GUI with Python] PyQt5 -Paint-
Let's make a GUI with python.
[GUI with Python] PyQt5-Drag and drop-
C / C ++ programmer challenges Python (first step)
[Python] The first step to making a game with Pyxel
[GUI with Python] PyQt5 -Custom Widget-
The first step in Python Matplotlib
"First Elasticsearch" starting with a python client
Happy GUI construction with electron and python
First Python 3 ~ First comparison ~
Statistics with python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
First time python
with syntax (Python)
First Python ~ Coding 2 ~
Bingo with python
Zundokokiyoshi with python
First python [O'REILLY]
Excel with Python
Microcomputer with Python
Cast with python
I made a GUI application with Python + PyQt5
The first step in the constraint satisfaction problem in Python
GUI automation with Python x Windows App Driver
You can easily create a GUI with Python
Simple sales tool creation with Python GUI: Quote creation
First python ① Environment construction with pythonbrew & Hello World !!
[Introduction to Udemy Python3 + Application] 9. First, print with print
Open a file dialog with a python GUI (tkinter.filedialog)
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
[First API] Try to get Qiita articles with Python
"Object-oriented" learning with python
First Python 3rd Edition
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
PyQ ~ Python First Steps ~
1.1 Getting Started with Python
Collecting tweets with Python