2016-09-16 11:52:03 +00:00
---
category: tool
2016-09-16 14:02:48 +00:00
tool: PyQT
2016-10-23 12:24:50 +00:00
filename: learnpyqt.py
2016-09-16 11:52:03 +00:00
contributors:
- ["Nathan Hughes", "https://github.com/sirsharpest"]
---
**Qt** is a widely-known framework for developing cross-platform software that can be run on various software and hardware platforms with little or no change in the code, while having the power and speed of native applications. Though **Qt** was originally written in *C++* .
This is an adaption on the C++ intro to QT by [Aleksey Kholovchuk](https://github.com/vortexxx192
), some of the code examples should result in the same functionality
2024-04-03 11:13:28 +00:00
this version just having been done using pyqt!
2016-09-16 11:52:03 +00:00
2016-10-23 12:24:50 +00:00
```python
2016-09-16 11:52:03 +00:00
import sys
from PyQt4 import QtGui
2024-04-03 11:13:28 +00:00
2016-09-16 11:52:03 +00:00
def window():
2024-04-03 11:13:28 +00:00
# Create an application object
2016-09-16 11:52:03 +00:00
app = QtGui.QApplication(sys.argv)
# Create a widget where our label will be placed in
w = QtGui.QWidget()
2024-04-03 11:13:28 +00:00
# Add a label to the widget
2016-09-16 11:52:03 +00:00
b = QtGui.QLabel(w)
2024-04-03 11:13:28 +00:00
# Set some text for the label
2016-09-16 11:52:03 +00:00
b.setText("Hello World!")
2024-04-03 11:13:28 +00:00
# Give some size and placement information
2016-09-16 11:52:03 +00:00
w.setGeometry(100, 100, 200, 50)
b.move(50, 20)
2024-04-03 11:13:28 +00:00
# Give our window a nice title
2016-09-16 11:52:03 +00:00
w.setWindowTitle("PyQt")
# Have everything display
w.show()
# Execute what we have asked for, once all setup
sys.exit(app.exec_())
if __name__ == '__main__':
window()
```
2024-04-03 11:13:28 +00:00
In order to get some of the more advanced features in **pyqt** we need to start looking at building additional elements.
2016-09-16 11:52:03 +00:00
Here we show how to introduce a dialog popup box, useful for asking the user to confirm a decision or to provide information.
2024-04-03 11:13:28 +00:00
```python
2016-09-16 11:52:03 +00:00
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
def window():
app = QApplication(sys.argv)
w = QWidget()
# Create a button and attach to widget w
b = QPushButton(w)
b.setText("Press me")
b.move(50, 50)
# Tell b to call this function when clicked
# notice the lack of "()" on the function call
b.clicked.connect(showdialog)
w.setWindowTitle("PyQt Dialog")
w.show()
sys.exit(app.exec_())
2024-04-03 11:13:28 +00:00
2016-09-16 11:52:03 +00:00
# This function should create a dialog window with a button
# that waits to be clicked and then exits the program
def showdialog():
d = QDialog()
b1 = QPushButton("ok", d)
b1.move(50, 50)
d.setWindowTitle("Dialog")
# This modality tells the popup to block the parent whilst it's active
d.setWindowModality(Qt.ApplicationModal)
# On click I'd like the entire process to end
b1.clicked.connect(sys.exit)
d.exec_()
if __name__ == '__main__':
window()
```