[GUI with Python] PyQt5 -Widget II-

Last time continued

widgets Ⅱ I will summarize this site roughly in Japanese.

[Optimization of image display]

QPixmap.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, 
    QLabel, QApplication)
from PyQt5.QtGui import QPixmap


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        hbox = QHBoxLayout(self)
        #Create QPixmap object
        pixmap = QPixmap("imoyokan.jpg ")

        #Make a label and put an image in it
        lbl = QLabel(self)
        lbl.setPixmap(pixmap)

        hbox.addWidget(lbl)
        self.setLayout(hbox)
        
        self.move(300, 200)
        self.setWindowTitle('Imoyokan')
        self.show()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
pixmap.png

[Display the entered text on the label]

QLineEdit.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QLabel, 
    QLineEdit, QApplication)


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        #Label object creation
        self.lbl = QLabel(self)
        #QLineEdit object creation
        qle = QLineEdit(self)
        
        qle.move(60, 100)
        self.lbl.move(60, 40)

        #Call the onChanged function when a character is entered in qle
        qle.textChanged[str].connect(self.onChanged)
        
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QLineEdit')
        self.show()
        
        
    def onChanged(self, text):
        
        #Insert the text entered on the label
        self.lbl.setText(text)
        #Adjust label length according to the text entered
        self.lbl.adjustSize()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
lineedit.png

[Change widget size by dragging]

QSplitter


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


import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame, 
    QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        hbox = QHBoxLayout(self)

        #Creating a frame object
        topleft = QFrame(self)
        #Frame shape setting
        topleft.setFrameShape(QFrame.StyledPanel)
 
        topright = QFrame(self)
        topright.setFrameShape(QFrame.StyledPanel)

        bottom = QFrame(self)
        bottom.setFrameShape(QFrame.StyledPanel)

        #Create a horizontal QSplitter object and add a frame
        splitter1 = QSplitter(Qt.Horizontal)
        splitter1.addWidget(topleft)
        splitter1.addWidget(topright)

        # #Create a vertical QSplitter object and add a frame
        splitter2 = QSplitter(Qt.Vertical)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(bottom)

        hbox.addWidget(splitter2)
        self.setLayout(hbox)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QSplitter')
        self.show()
        
        
    def onChanged(self, text):
        
        self.lbl.setText(text)
        self.lbl.adjustSize()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
aplitter.png

【combo box】

QComboBox.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QLabel, 
    QComboBox, QApplication)


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        #Label creation, initial name to Ubuntu
        self.lbl = QLabel("Ubuntu", self)

        #Creating a QComboBox object
        combo = QComboBox(self)
        #Item naming
        combo.addItem("Ubuntu")
        combo.addItem("Mandriva")
        combo.addItem("Fedora")
        combo.addItem("Arch")
        combo.addItem("Gentoo")

        combo.move(50, 50)
        self.lbl.move(50, 150)

        #Call the onActivated function when an item is selected
        combo.activated[str].connect(self.onActivated)        
         
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QComboBox')
        self.show()
        
        
    def onActivated(self, text):
        
        #Set the name of the selected item on the label
        self.lbl.setText(text)
        #Adjust label length
        self.lbl.adjustSize()  
        
                
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
combobox.png

Next time will try Drag & drop roughly.

Recommended Posts

[GUI with Python] PyQt5 -Widget II-
[GUI with Python] PyQt5 -Custom Widget-
[GUI in Python] PyQt5 -Widget-
[GUI with Python] PyQt5 -Preparation-
[GUI with Python] PyQt5 -Paint-
I made a GUI application with Python + PyQt5
Python compilation with pyqt deploy
[GUI with Python] PyQt5-Layout management-
Introducing GUI: PyQt5 in Python
[GUI in Python] PyQt5 -Event-
Let's make a GUI with python.
[GUI with Python] PyQt5-The first step-
[GUI with Python] PyQt5-Drag and drop-
I played with PyQt5 and Python3
Happy GUI construction with electron and python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
GUI image cropping tool made with Python + Tkinter
GUI automation with Python x Windows App Driver
You can easily create a GUI with Python
Create a color-specified widget with Python + Qt (PySide)
Simple sales tool creation with Python GUI: Quote creation
Open a file dialog with a python GUI (tkinter.filedialog)
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
"Object-oriented" learning with python
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
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3