Qt (Cute) is an application user interface (UI) framework written in the C ++ language.
Qt is widely known as a GUI toolkit, but it is also widely used in non-GUI programs such as console tools and servers. From Wikipedia
A framework that makes it easy to create GUI applications. It also supports cross-platform development.
PyQt is a cross-platform GUI toolkit, Qt's Python binding, and is one of the options for GUI programming in Python. From Wikipedia
You can easily create Qt and GUI applications set for Python.
I won't explain the environment settings here, so ggrks
window.py
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
When I run it, an empty window appears
button.py
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
button = QPushButton('button')
layout = QGridLayout()
layout.addWidget(button)
self.setLayout(layout)
self.setWindowTitle("Button")
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
With button and title
input-output.py
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.button = QPushButton('Read / display')
self.button.clicked.connect(self.output)
self.inputText = QLineEdit()
self.inputText.setText("")
self.outputText = QLineEdit()
self.outputText.setText("")
self.outputText.setReadOnly(True)
textLayout = QHBoxLayout()
textLayout.addWidget(self.inputText)
textLayout.addWidget(self.outputText)
layout = QVBoxLayout()
layout.addLayout(textLayout)
layout.addWidget(self.button)
self.setLayout(layout)
self.setWindowTitle("Button")
def output(self):
self.outputText.setText(self.inputText.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
When you enter text and press the button, the text is output
string-graphic.py
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class ShowString(QGraphicsItem):
def __init__(self, width=200, height=10, text=""):
super(ShowString, self).__init__()
self.width = width
self.height = height
self.text = text
def paint(self, painter, option, widget):
painter.setPen(Qt.black)
painter.drawText(0, 20, self.text)
def boundingRect(self):
return QRectF(0,0,400,100)
def setText(self, text):
self.text = text
self.update()
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.graphicView = QGraphicsView()
self.showString = ShowString()
scene = QGraphicsScene(self.graphicView)
scene.setSceneRect(0, 0, 400, 100)
self.graphicView.setScene(scene)
self.graphicView.resize(300,50)
scene.addItem(self.showString)
self.button = QPushButton('Read / display')
self.button.clicked.connect(self.output)
self.inputText = QLineEdit()
self.inputText.setText("")
self.outputText = QLineEdit()
self.outputText.setText("")
self.outputText.setReadOnly(True)
textLayout = QHBoxLayout()
textLayout.addWidget(self.inputText)
textLayout.addWidget(self.outputText)
layout = QVBoxLayout()
layout.addWidget(self.graphicView)
layout.addLayout(textLayout)
layout.addWidget(self.button)
self.setLayout(layout)
self.setWindowTitle("Button")
def output(self):
self.outputText.setText(self.inputText.text())
self.showString.setText(self.inputText.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
Added character string output in QGraphicsView to the previous program
Continued: Try to open a subwindow with PyQt5 and Python
That's all for playing with PyQt5 and Python3. I like it because I can design GUIs really intuitively.
I used it as a reference below. Thank you very much. http://d.hatena.ne.jp/mFumi/20141112/1415806010 http://qiita.com/kenasman/items/55505654823e9d040e6e
Recommended Posts