Last time continued
First programs in PyQt5 I will summarize this site roughly in Japanese.
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_())
 
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_())  
 
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_())


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_())
 
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_())
 
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