Python PyQt5 basics

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QLabel,QPushButton,QVBoxLayout,QHBoxLayout
from random import choice

app = QApplication([])
main_window = QWidget()
main_window.setWindowTitle("My app")
main_window.resize(400,300)

title = QLabel("Random keywords")
text1 = QLabel("?")
button1 = QPushButton("Click me")
my_words = ['astraja', 'pikademia','technology','python']

master_layout = QVBoxLayout()
row1 = QHBoxLayout()
row2 = QHBoxLayout()
row3 = QHBoxLayout()

row1.addWidget(title, alignment=Qt.AlignCenter)
row2.addWidget(text1, alignment=Qt.AlignCenter)
row3.addWidget(button1, alignment=Qt.AlignCenter)

master_layout.addLayout(row1)
master_layout.addLayout(row2)
master_layout.addLayout(row3)

def random_word():
    word = choice(my_words)
    text1.setText(word)

button1.clicked.connect(random_word)

main_window.setLayout(master_layout)

main_window.show()
app.exec_()

Leave a Comment

Scroll to Top