41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit
|
|
|
|
# 1. Import QApplication and all the required widgets
|
|
# from PyQt5.QtWidgets import QApplication, QLabel, QWidget
|
|
|
|
def addLabel(layout, text):
|
|
layout.addWidget(QLabel(text))
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication([])
|
|
|
|
window = QWidget()
|
|
layout = QVBoxLayout(window)
|
|
# Create a label Widget and add it to the layout
|
|
label = QLabel('Enter some text!')
|
|
layout.addWidget(label)
|
|
|
|
line_edit = QLineEdit()
|
|
layout.addWidget(line_edit)
|
|
|
|
# Create a QPushButton object with a caption on it
|
|
qbtn= QPushButton('Add Label')
|
|
|
|
# Add the QPushButton to the layout
|
|
layout.addWidget(qbtn)
|
|
|
|
# Close the application when the button is pressed
|
|
# Here I am using slots & signals, which I will demonstrate later in this tutorial
|
|
qbtn.clicked.connect(lambda:addLabel(layout, line_edit.text()))
|
|
|
|
window.setWindowTitle("PyQt App")
|
|
window.setGeometry(100, 100, 280, 80)
|
|
# helloMsg = QLabel("<h1>Hello, World!</h1>", parent=window)
|
|
# helloMsg.move(60, 15)
|
|
|
|
window.show()
|
|
|
|
# 5. Run your application's event loop
|
|
sys.exit(app.exec())
|