Python GUI Apps mit PyQt5 (ohne OOP)

Das folgende Programm erstellt ein einfaches

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
 
"""
Info:
- PyQt5: cross platform gui framework (runs on Win,Mac,Linux,iOS,Android)
- Install: pip install pyqt5
"""
 
app = QApplication(sys.argv) # create pyqt app 
win = QMainWindow() # create a window for pyqt app
win.setGeometry(500,200,500,300) # set position and windows size of win: x0,y0,width,height
win.setWindowTitle("Title of my awesome app!") # sets a title
 
# add label
label1 = QtWidgets.QLabel(win) # add a label to the window win
label1.move(20,20)
label1.setObjectName("label1")
label1.setText("Label 1")
 
# add button
button1 = QtWidgets.QPushButton(win)
button1.setGeometry(200,100,80,80)
button1.setObjectName("button1")
button1.setText("Button 1")
button1.clicked.connect(lambda: button1_clicked(button1)) # even: calls function button1_clicked when clicking button1 and passes the button as an argument
 
# events
def button1_clicked(b):
    print(f"Button with objectName {b.objectName()} clicked!")
 
win.show()
sys.exit(app.exec_()) # properly exits python program when closing pyqt app

Es stehen viele weitere Widgets zur Verfügung wie:

Widget What it does
QCheckbox A checkbox
QComboBox A dropdown list box
QDateEdit For editing dates and datetimes
QDateTimeEdit For editing dates and datetimes
QDial Rotatable dial
QDoubleSpinbox A number spinner for floats
QFontComboBox A list of fonts
QLCDNumber A quite ugly LCD display
QLabel Just a label, not interactive
QLineEdit Enter a line of text
QProgressBar A progress bar
QPushButton A button
QRadioButton A toggle set, with only one active item
QSlider A slider
QSpinBox An integer spinner
QTimeEdit For editing times
  • talit/python_pyqt_no_oop.1630141170.txt.gz
  • Zuletzt geändert: 2021-08-28 08:59
  • von sca