python - pro - 22 - PYQT5 - PYQT6
Installing
PYQT5
pip install PyQt5 - artik bitmis gibi- sept-2024 -
Pypi.org da tools`u yok - indirmek icin:
https://build-system.fman.io/qt-designer-download -pyqt6 de isliyor mu bakmadim
Pyqt6
pip install PyQt6
PYQT6 tools
pip install pyqt6-tools
Ilk bos window
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
def window():
app = QApplication(sys.argv)
win = QMainWindow()
win.show()
sys.exit(app.exec_())
window()
cikan resim:
title, icon, tooltip, konum ve boyut vs:
mport sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolTip
from PyQt5.QtGui import QIcon
def window():
app = QApplication(sys.argv)
win = QMainWindow() #bu nesne uzerinde calisiyoruz
win.setWindowTitle('my first app') # Title
win.setGeometry(200,200,800,500) # Konum: yatay:200px - dikey:200px // Olcu: yatay 800 - dikey 500
win.setToolTip('en kahraman Ridvan') # Tooltip
win.setWindowIcon(QIcon('logo.svg')) #icon
win.show()
sys.exit(app.exec_())
window()
nesne ekleme
QT Designer`i kullanarak:
QT desıgner ile nesne adlarını düzenliyoruz
Form'u bizim python klasörüne kaydediyoruz (ui extension'u alır otomatik olarak):
MainWindow.ui
Bu dosya xml olarak kaydediliyor
MsDos prompt'da şu command'ı yazıyoruz - ui dosyadan py dosyası elde etmeliyiz
pyuic5 MainWindow.ui -o MainWindow.py
(Python User Interface Converter 5)
Calculator - app -
from PyQt5 import QtWidgets
import sys
from MainWindow import Ui_UI_MainWindow
class AppWindowCLS(QtWidgets.QMainWindow):
def __init__(self):
super(AppWindowCLS,self).__init__()
self.uiWndw=Ui_UI_MainWindow()
self.uiWndw.setupUi(self)
self.uiWndw.btn_toplama.clicked.connect(self.hesapla)
self.uiWndw.btn_cikarma.clicked.connect(self.hesapla)
self.uiWndw.btn_carpma.clicked.connect(self.hesapla)
self.uiWndw.btn_bolme.clicked.connect(self.hesapla)
def hesapla(self):
sender = self.sender().text()
result=0
if sender=='Toplama':
result = int(self.uiWndw.txtsayi1.text())+int(self.uiWndw.txtsayi2.text())
elif sender=='Cikarma':
result = int(self.uiWndw.txtsayi1.text())-int(self.uiWndw.txtsayi2.text())
elif sender=='Carpma':
result = int(self.uiWndw.txtsayi1.text())*int(self.uiWndw.txtsayi2.text())
elif sender=='Bolme':
result = int(self.uiWndw.txtsayi1.text())/int(self.uiWndw.txtsayi2.text())
self.uiWndw.lblToplam.setText(str(result))
def appCalcul():
appCalcul=QtWidgets.QApplication(sys.argv)
WinApp=AppWindowCLS()
WinApp.show()
sys.exit(appCalcul.exec_())
appCalcul()
#
App - Layout - ekranda zone`lar olusturma
- tek zone :
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QPalette, QColor
class ColorCls(QWidget):
def __init__(self, color):
super(ColorCls, self).__init__()
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
widget = ColorCls('blue')
self.setCentralWidget(widget)
def appColor():
appColor=QApplication(sys.argv)
winDef=MainWindow()
winDef.show()
sys.exit(appColor.exec_())
appColor()
Vertical layout
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(100,100,80,120)
layout = QtWidgets.QVBoxLayout() # vertical layout`u ilk olusturan
layout.addWidget(ColorCls('red'))
layout.addWidget(ColorCls('blue'))
layout.addWidget(ColorCls('green'))
layout.addWidget(ColorCls('yellow'))
widget=QWidget()
widget.setLayout(layout) #vertical layout yapiyor
self.setCentralWidget(widget)
Horizantal layout
self.setGeometry(100,100,120,80)
layout = QtWidgets.QHBoxLayout()
hor_layout1 = QtWidgets.QHBoxLayout()
hor_layout1.addWidget(ColorCls('red'))
hor_layout1.addWidget(ColorCls('blue'))
hor_layout1.addWidget(ColorCls('green'))
hor_layout1.addWidget(ColorCls('yellow'))
hor_layout1.setContentsMargins (30,20,0,30) #yukari asagi filan bosluk
hor_layout1.setSpacing(50) # grid elemanlari arasi bosluk
hor_layout2 = QtWidgets.QHBoxLayout()
hor_layout2.addWidget(ColorCls('red'))
hor_layout2.addWidget(ColorCls('blue'))
ver_layout1=QtWidgets.QVBoxLayout()
ver_layout1.addLayout(hor_layout1)
ver_layout1.addLayout(hor_layout2)
#11:04
widget=QWidget()
widget.setLayout(ver_layout1)
#vertical layout a horizantal`lari atip
#ekranda vertical`i gosterecegiz diyoruz
self.setCentralWidget(widget)
Grid layout
layoutGrid =QtWidgets.QGridLayout()
layoutGrid.addWidget(ColorCls('red'),0,0)
layoutGrid.addWidget(ColorCls('blue'),1,0)
layoutGrid.addWidget(ColorCls('black'),0,1)
layoutGrid.addWidget(ColorCls('purple'),2,0)
widget=QWidget()
widget.setLayout(layoutGrid)
Sonuclari ekrandaki pencereye yazdirmak
from PyQt5 import QtWidgets
import sys
from MainWindow2 import Ui_MainWindow
class AppWindowCLS(QtWidgets.QMainWindow):
def __init__(self):
super(AppWindowCLS,self).__init__()
self.uiWndw=Ui_MainWindow()
self.uiWndw.setupUi(self)
self.uiWndw.ckbx_art.stateChanged.connect(self.show_Status_cb)
self.uiWndw.ckbx_sport.stateChanged.connect(self.show_Status_cb)
self.uiWndw.ckbx_movie.stateChanged.connect(self.show_Status_cb)
self.uiWndw.btn_secal.clicked.connect(self.getAllChecked)
def getAllChecked(self):
resultChckbx =''
items = self.uiWndw.PersonActivityWidget.findChildren(QtWidgets.QCheckBox)
for ckboxWhichOne in items:
if ckboxWhichOne.isChecked():
print(f'ad: {ckboxWhichOne.text()} - durum: {ckboxWhichOne.isChecked()}')
resultChckbx += ckboxWhichOne.text()+'\n'
self.uiWndw.lbl_secal.setText(resultChckbx)
def show_Status_cb(self,value):
ckboxWhichOne=self.sender()
def appRadiobtn():
appRadiobtn=QtWidgets.QApplication(sys.argv)
WinApp=AppWindowCLS()
WinApp.show()
sys.exit(appRadiobtn.exec_())
appRadiobtn()
Comments
Post a Comment