Şifre Girme İşlemi

Merhaba.

Aşağıdaki gibi bir bankamatik pencerem var.
Kartı tak dediğim zaman şifre girmek için linedit elemanı geliyor ekrana.
Takıldığım nokta her numerik tuş için clicked.connect() mi atamam gerekiyor.
Bunun daha kolay bir yolu var mıdır, nasıl yaparız?

main.py

import sys
import Bankamatik
from PyQt5.QtWidgets import *

def main():
    uygulama = QApplication(sys.argv)
    pencere = Bankamatik.Pencere()
    sys.exit(uygulama.exec_())

if __name__ == "__main__":
    main()

Bankamatik.py

from PyQt5.QtWidgets import *
from BankamatikUI import *

class Pencere(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.Init_Ui()

    def Init_Ui(self):
        self.labelSifreBaslik.hide()
        self.lineEditSifre.hide()
        self.btnKartTak.clicked.connect(self.KartTak)
        self.show()

    def KartTak(self):
        self.labelSifreBaslik.show()
        self.lineEditSifre.show()
        self.btn1.clicked.connect(self.SifreGir)
        self.btn2.clicked.connect(self.SifreGir)
        self.btn3.clicked.connect(self.SifreGir)
        #...#

    def SifreGir(self):
        self.lineEditSifre.setText()

BankamatikUI.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(476, 778)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.frameCenter = QtWidgets.QFrame(self.centralwidget)
        self.frameCenter.setStyleSheet("background-color:#242426;")
        self.frameCenter.setFrameShape(QtWidgets.QFrame.Box)
        self.frameCenter.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.frameCenter.setObjectName("frameCenter")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.frameCenter)
        self.verticalLayout.setContentsMargins(-1, -1, 9, -1)
        self.verticalLayout.setObjectName("verticalLayout")
        self.frameScreen = QtWidgets.QFrame(self.frameCenter)
        self.frameScreen.setMaximumSize(QtCore.QSize(16777215, 505))
        self.frameScreen.setStyleSheet("background-color:#175772;")
        self.frameScreen.setFrameShape(QtWidgets.QFrame.Panel)
        self.frameScreen.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.frameScreen.setObjectName("frameScreen")
        self.layoutWidget = QtWidgets.QWidget(self.frameScreen)
        self.layoutWidget.setGeometry(QtCore.QRect(60, 120, 331, 91))
        self.layoutWidget.setObjectName("layoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.layoutWidget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setObjectName("formLayout")
        self.lineEditSifre = QtWidgets.QLineEdit(self.layoutWidget)
        self.lineEditSifre.setMinimumSize(QtCore.QSize(0, 50))
        self.lineEditSifre.setMaximumSize(QtCore.QSize(16777215, 50))
        font = QtGui.QFont()
        font.setFamily("Bahnschrift")
        font.setPointSize(14)
        self.lineEditSifre.setFont(font)
        self.lineEditSifre.setStyleSheet("background-color:transparent;color:#fff; border:0;border-bottom:2px solid red;")
        self.lineEditSifre.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEditSifre.setObjectName("lineEditSifre")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.lineEditSifre)
        self.labelSifreBaslik = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Bahnschrift")
        font.setPointSize(20)
        self.labelSifreBaslik.setFont(font)
        self.labelSifreBaslik.setStyleSheet("color:#fff;")
        self.labelSifreBaslik.setAlignment(QtCore.Qt.AlignCenter)
        self.labelSifreBaslik.setObjectName("labelSifreBaslik")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.SpanningRole, self.labelSifreBaslik)
        self.verticalLayout.addWidget(self.frameScreen)
        self.frameNumers = QtWidgets.QFrame(self.frameCenter)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frameNumers.sizePolicy().hasHeightForWidth())
        self.frameNumers.setSizePolicy(sizePolicy)
        self.frameNumers.setMaximumSize(QtCore.QSize(16777215, 350))
        self.frameNumers.setStyleSheet("background-color:#aaa;")
        self.frameNumers.setFrameShape(QtWidgets.QFrame.Box)
        self.frameNumers.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.frameNumers.setObjectName("frameNumers")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.frameNumers)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.frame = QtWidgets.QFrame(self.frameNumers)
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.gridLayout = QtWidgets.QGridLayout(self.frame)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.setObjectName("gridLayout")
        self.btn5 = QtWidgets.QPushButton(self.frame)
        self.btn5.setMinimumSize(QtCore.QSize(80, 50))
        self.btn5.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn5.setFont(font)
        self.btn5.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn5.setObjectName("btn5")
        self.gridLayout.addWidget(self.btn5, 2, 1, 1, 1)
        self.btn7 = QtWidgets.QPushButton(self.frame)
        self.btn7.setMinimumSize(QtCore.QSize(80, 50))
        self.btn7.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn7.setFont(font)
        self.btn7.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn7.setObjectName("btn7")
        self.gridLayout.addWidget(self.btn7, 3, 0, 1, 1)
        self.btnDuzelt = QtWidgets.QPushButton(self.frame)
        self.btnDuzelt.setMinimumSize(QtCore.QSize(120, 50))
        self.btnDuzelt.setMaximumSize(QtCore.QSize(120, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.btnDuzelt.setFont(font)
        self.btnDuzelt.setStyleSheet("border-radius:3px;background-color:darkorange;")
        self.btnDuzelt.setObjectName("btnDuzelt")
        self.gridLayout.addWidget(self.btnDuzelt, 2, 3, 1, 1)
        self.btn6 = QtWidgets.QPushButton(self.frame)
        self.btn6.setMinimumSize(QtCore.QSize(80, 50))
        self.btn6.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn6.setFont(font)
        self.btn6.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn6.setObjectName("btn6")
        self.gridLayout.addWidget(self.btn6, 2, 2, 1, 1)
        self.btn3 = QtWidgets.QPushButton(self.frame)
        self.btn3.setMinimumSize(QtCore.QSize(80, 50))
        self.btn3.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn3.setFont(font)
        self.btn3.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn3.setObjectName("btn3")
        self.gridLayout.addWidget(self.btn3, 1, 2, 1, 1)
        self.btnArti = QtWidgets.QPushButton(self.frame)
        self.btnArti.setMinimumSize(QtCore.QSize(80, 50))
        self.btnArti.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btnArti.setFont(font)
        self.btnArti.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btnArti.setObjectName("btnArti")
        self.gridLayout.addWidget(self.btnArti, 4, 2, 1, 1)
        self.btnYardim = QtWidgets.QPushButton(self.frame)
        self.btnYardim.setMinimumSize(QtCore.QSize(120, 50))
        self.btnYardim.setMaximumSize(QtCore.QSize(120, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.btnYardim.setFont(font)
        self.btnYardim.setStyleSheet("border-radius:3px;background-color:darkorange;")
        self.btnYardim.setObjectName("btnYardim")
        self.gridLayout.addWidget(self.btnYardim, 3, 3, 1, 1)
        self.btnGiris = QtWidgets.QPushButton(self.frame)
        self.btnGiris.setMinimumSize(QtCore.QSize(120, 50))
        self.btnGiris.setMaximumSize(QtCore.QSize(120, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.btnGiris.setFont(font)
        self.btnGiris.setStyleSheet("border-radius:3px;background-color:darkorange;")
        self.btnGiris.setObjectName("btnGiris")
        self.gridLayout.addWidget(self.btnGiris, 4, 3, 1, 1)
        self.btnEksi = QtWidgets.QPushButton(self.frame)
        self.btnEksi.setMinimumSize(QtCore.QSize(80, 50))
        self.btnEksi.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btnEksi.setFont(font)
        self.btnEksi.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btnEksi.setObjectName("btnEksi")
        self.gridLayout.addWidget(self.btnEksi, 4, 0, 1, 1)
        self.btn0 = QtWidgets.QPushButton(self.frame)
        self.btn0.setMinimumSize(QtCore.QSize(80, 50))
        self.btn0.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn0.setFont(font)
        self.btn0.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn0.setObjectName("btn0")
        self.gridLayout.addWidget(self.btn0, 4, 1, 1, 1)
        self.btn9 = QtWidgets.QPushButton(self.frame)
        self.btn9.setMinimumSize(QtCore.QSize(80, 50))
        self.btn9.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn9.setFont(font)
        self.btn9.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn9.setObjectName("btn9")
        self.gridLayout.addWidget(self.btn9, 3, 2, 1, 1)
        self.btn8 = QtWidgets.QPushButton(self.frame)
        self.btn8.setMinimumSize(QtCore.QSize(80, 50))
        self.btn8.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn8.setFont(font)
        self.btn8.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn8.setObjectName("btn8")
        self.gridLayout.addWidget(self.btn8, 3, 1, 1, 1)
        self.btn1 = QtWidgets.QPushButton(self.frame)
        self.btn1.setMinimumSize(QtCore.QSize(80, 50))
        self.btn1.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn1.setFont(font)
        self.btn1.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn1.setObjectName("btn1")
        self.gridLayout.addWidget(self.btn1, 1, 0, 1, 1)
        self.btn4 = QtWidgets.QPushButton(self.frame)
        self.btn4.setMinimumSize(QtCore.QSize(80, 50))
        self.btn4.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn4.setFont(font)
        self.btn4.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn4.setObjectName("btn4")
        self.gridLayout.addWidget(self.btn4, 2, 0, 1, 1)
        self.btn2 = QtWidgets.QPushButton(self.frame)
        self.btn2.setMinimumSize(QtCore.QSize(80, 50))
        self.btn2.setMaximumSize(QtCore.QSize(80, 50))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.btn2.setFont(font)
        self.btn2.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btn2.setObjectName("btn2")
        self.gridLayout.addWidget(self.btn2, 1, 1, 1, 1)
        self.btnIptal = QtWidgets.QPushButton(self.frame)
        self.btnIptal.setMinimumSize(QtCore.QSize(120, 50))
        self.btnIptal.setMaximumSize(QtCore.QSize(120, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.btnIptal.setFont(font)
        self.btnIptal.setStyleSheet("border-radius:3px;background-color:darkorange;")
        self.btnIptal.setObjectName("btnIptal")
        self.gridLayout.addWidget(self.btnIptal, 1, 3, 1, 1)
        self.btnKartTak = QtWidgets.QPushButton(self.frame)
        self.btnKartTak.setMaximumSize(QtCore.QSize(16777215, 50))
        font = QtGui.QFont()
        font.setFamily("MS UI Gothic")
        font.setPointSize(20)
        self.btnKartTak.setFont(font)
        self.btnKartTak.setStyleSheet("border-radius:3px;background-color:#ccc;")
        self.btnKartTak.setObjectName("btnKartTak")
        self.gridLayout.addWidget(self.btnKartTak, 0, 0, 1, 4)
        self.gridLayout.setColumnMinimumWidth(0, 100)
        self.gridLayout.setColumnMinimumWidth(1, 100)
        self.gridLayout.setColumnMinimumWidth(2, 110)
        self.gridLayout.setRowMinimumHeight(0, 100)
        self.gridLayout.setRowMinimumHeight(1, 100)
        self.gridLayout.setRowMinimumHeight(2, 100)
        self.gridLayout.setRowMinimumHeight(3, 100)
        self.gridLayout.setRowMinimumHeight(4, 100)
        self.horizontalLayout_2.addWidget(self.frame, 0, QtCore.Qt.AlignHCenter)
        self.verticalLayout.addWidget(self.frameNumers)
        self.horizontalLayout.addWidget(self.frameCenter)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Bankamatik"))
        self.labelSifreBaslik.setText(_translate("MainWindow", "Şifrenizi Girin"))
        self.btn5.setText(_translate("MainWindow", "5"))
        self.btn7.setText(_translate("MainWindow", "7"))
        self.btnDuzelt.setText(_translate("MainWindow", "DÜZELTME"))
        self.btn6.setText(_translate("MainWindow", "6"))
        self.btn3.setText(_translate("MainWindow", "3"))
        self.btnArti.setText(_translate("MainWindow", "+"))
        self.btnYardim.setText(_translate("MainWindow", "YARDIM"))
        self.btnGiris.setText(_translate("MainWindow", "GİRİŞ"))
        self.btnEksi.setText(_translate("MainWindow", "-"))
        self.btn0.setText(_translate("MainWindow", "0"))
        self.btn9.setText(_translate("MainWindow", "9"))
        self.btn8.setText(_translate("MainWindow", "8"))
        self.btn1.setText(_translate("MainWindow", "1"))
        self.btn4.setText(_translate("MainWindow", "4"))
        self.btn2.setText(_translate("MainWindow", "2"))
        self.btnIptal.setText(_translate("MainWindow", "İPTAL"))
        self.btnKartTak.setText(_translate("MainWindow", "KARTI TAK"))

Merhaba, bütün buton nesnelerini bir listede toplayabilirsiniz. Yani mesela şöyle

buton_sayisi = 0
butonlar

for i in range(buton_sayisi):
    butonlar.append(QPushButton())

tıklandığında fonksiyon ataması falan yapmak istiyorsanız aşağıda yazdığım yöntemi de kullanabilirsiniz.


def f(x):
    def t():
        print (x, "den selamlar")
    return t
butonlar = []
for i in range(buton_sayisi):
    btn = QPushButton()
    btn.clicked.connect(f(i))
    butonlar.append(btn)

Belki functools modülünün partial fonksiyonu ile çok daha kolay yapılabilir.

Bu arada incelemek isterseniz önceki zamanlarda yazmış olduğum kodu inceleyebilirsiniz. kod

İyi akşamlar.


(Konudan bağımsız olarak)
Bu arada SSS’e baktım da bu konuyu göremedim. Müsait bir zamanda SSS`e bunu ekleyeyim.

4 Beğeni

Her tus icin tek bir fonksiyon cagirmaktan daha kolay bir sey dusunemiyorum.
Tuslari bir fonksiyon yardimiyla yaratiyor olsaydik tum koda sadece bir satir eklememiz yetecekti.

@Cihat_Altiparmak’in da gosterdigi gibi fonksiyon kullanmak lazim. Fakat bu noktada functools'a bakmayi tavsiye etmiyorum.

        self.btn1.setMinimumSize(QtCore.QSize(80, 50))
        self.btn1.setMaximumSize(QtCore.QSize(80, 50))

Sunun gibi tekrar eden satirlar yerine self.btnX'i parametre olarak alip bu islemleri yapacak bir setupButton fonksiyonuyla baslanabilir.

2 Beğeni

Fazlaca buton oluşturduğunuz bir durumda, buton oluşturmanızı sağlayacak bir fonksiyon tanımlamanızda fayda var.
Şöyle bir örnek bırakayım,

from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
import sys

class UI_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setFixedSize(340, 325)
        self.setWindowTitle("Deneme")
        self.buttons = {}
        self.show()
        self.setup()

    def setup(self):
        font = QFont()
        font.setFamily("Bahnschrift")
        font.setPointSize(20)
        self.LineEdit = QLineEdit(self)
        self.LineEdit.setGeometry(QRect(25, 25, 290, 50))
        self.LineEdit.setFont(font)
        self.LineEdit.show()
        names = [f"btn{n}" for n in range(1, 10)]
        texts = [str(n) for n in range(1, 10)]
        geometries = [(25, 100, 80, 50), (130, 100, 80, 50), (235, 100, 80, 50),
                      (25, 175, 80, 50), (130, 175, 80, 50), (235, 175, 80, 50),
                      (25, 250, 80, 50), (130, 250, 80, 50), (235, 250, 80, 50)]
        for i in range(9):
            self.create_button(names[i], texts[i], self, geometries[i],
                               "background-color:#ccc;",
                               font, self.clicked)

    def create_button(self, name, text, parent, geometry,
                      style_sheet, font, target_function=None):
        x, y, width, height = geometry
        button = QPushButton(text=text, parent=parent)
        button.setObjectName(name)
        button.setFont(font)
        button.setGeometry(QRect(x, y, width, height))
        button.setStyleSheet(style_sheet)
        if target_function is not None:
            button.clicked.connect(target_function)
        button.show()
        self.buttons.update({name: button})

    def clicked(self):
        value = (self.sender()).text()
        text = self.LineEdit.text()
        self.LineEdit.setText(text + value)
        

app = QApplication(sys.argv)
ui = UI_MainWindow()
app.exec_()

resim_2021-12-27_110748

2 Beğeni

Hepsini bir yere doldurmana gerek yok şu şekilde yapabilirsin.

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class BankamatikButon(QPushButton):

    def __init__(self, parent):
        super(QWidget, self).__init__(parent.tab)
        self.clicked.connect(self.btn_click)

    def btn_click:
       print("button clicked")

Bu şekilde bir class yaparsan tek yapman gereken BankamatikButon(parent) demen yeterli olur.
super(QWidget, self).__init__(parent.tab) bu satırda tabın içinde olduğu için onu da çağırmam gerekiyorsu sen de de belki böyle gerekebilir ya da denersin, buraya butonun parent nesnesini yolarsın.

2 Beğeni