Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:python:python_qt:qt_kezdes

< Python Qt

Qt kezdés

Helló Világ

hello.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        self.setGeometry(100,100,320,200)
        self.setWindowTitle('Helló Világ')
        self.show()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

Felirat

label.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QLabel
 
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        helloLabel = QLabel(self)
        helloLabel.setText('Működik')
        helloLabel.move(120, 90)        
 
        self.setGeometry(100,100,320,200)
        self.setWindowTitle('Helló Világ')
        self.show()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

Gomb eseménnyel

gomb.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        button1 = QPushButton(self)
        button1.setText("Mehet")
        button1.move(50, 100)
        button1.clicked.connect(self.on_click_button1)
 
        self.setGeometry(100,100,320,200)
        self.show()
 
 
    def on_click_button1(self):    
        self.setWindowTitle('egy')
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

Beviteli mező

bevitel.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        button1 = QPushButton(self)
        button1.setText("Mehet")
        button1.move(50, 100)
        button1.clicked.connect(self.on_click_button1)
 
        self.entry1 = QLineEdit(self)
        self.entry1.move(50, 60)
 
        self.setGeometry(100,100,320,200)
        self.setWindowTitle("Helló Világ")
        self.show()
 
 
    def on_click_button1(self):    
        text1 = self.entry1.text()
        self.setWindowTitle(text1)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

Gyakorlat

triangle.py

  • Írjon programot, amely bekéri egy háromszög alapját és magasságát, majd kiszámítja a területét.
triangle.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QLabel, QLineEdit, QPushButton
 
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        baseLabel = QLabel(self)
        baseLabel.setText('Alap')
        baseLabel.move(50, 30)
        self.baseEntry = QLineEdit(self)
        self.baseEntry.move(130, 30)
 
        heightLabel = QLabel(self)
        heightLabel.setText('Magasság')
        heightLabel.move(50, 70)
        self.heightEnty = QLineEdit(self)
        self.heightEnty.move(130, 70)
 
        calcButton = QPushButton(self)
        calcButton.setText('Számít')
        calcButton.move(90, 120)
        calcButton.clicked.connect(self.on_click_calcbutton)
 
        areaLabel = QLabel(self)
        areaLabel.setText('Terület')
        areaLabel.move(50, 160)
 
        self.areaEnty = QLineEdit(self)
        self.areaEnty.move(130, 160)
 
        self.setGeometry(100,100,320,200)
        self.setWindowTitle('Háromszög területe')
        self.show()
 
    def on_click_calcbutton(self):
        base = int(self.baseEntry.text())
        height = int(self.heightEnty.text())
        area = base * height / 2
        self.areaEnty.setText(str(area))
        self.baseEntry.setText('')
        self.heightEnty.setText('')
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

ComboBox

combo.py
import sys
from PyQt5.QtWidgets import QApplication, QComboBox, QMainWindow
from PyQt5.QtWidgets import QComboBox
 
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        super().__init__()
        combo = QComboBox(self)
        combo.move(50, 50)
        combo.addItem('Alma')
        combo.addItem('Körte')
        combo.addItem('Barack')
        combo.addItem('Szilva')
 
        self.setGeometry(100, 100, 320, 200)
        self.show()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())
oktatas/programozas/python/python_qt/qt_kezdes.txt · Utolsó módosítás: 2023/08/21 20:57 szerkesztette: admin