Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

#-- coding: utf-8 -- from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6.QtWidgets…

  1. User

    #-- coding: utf-8 -- from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6.QtWidgets import QMessageBox import pyodbc

    def connect(): conn_str = 'DRIVER={SQL Server};SERVER=SAVITAR\SQLEXPRESS01;DATABASE=kayit;Trusted_Connection=yes;' connection = pyodbc.connect(conn_str) return connection

    class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(616, 779) font = QtGui.QFont() font.setBold(True) font.setItalic(False) font.setUnderline(False) font.setWeight(75) font.setStrikeOut(False) MainWindow.setFont(font) self.centralwidget = QtWidgets.QWidget(parent=MainWindow) self.centralwidget.setObjectName("centralwidget") self.lineEdit = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(160, 60, 251, 31)) self.lineEdit.setText("") self.lineEdit.setObjectName("lineEdit") self.pushButton = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(420, 160, 91, 81)) self.pushButton.setObjectName("pushButton") self.pushButton.clicked.connect(self.guncelle) self.label = QtWidgets.QLabel(parent=self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 60, 121, 31)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label.setFont(font) self.label.setAutoFillBackground(False) self.label.setTextFormat(QtCore.Qt.TextFormat.AutoText) self.label.setWordWrap(False) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(parent=self.centralwidget) self.label_2.setGeometry(QtCore.QRect(0, 100, 121, 51)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.lineEdit_2 = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit_2.setGeometry(QtCore.QRect(160, 110, 251, 31)) self.lineEdit_2.setText("") self.lineEdit_2.setObjectName("lineEdit_2") self.label_3 = QtWidgets.QLabel(parent=self.centralwidget) self.label_3.setGeometry(QtCore.QRect(0, 150, 121, 51)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.lineEdit_3 = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit_3.setGeometry(QtCore.QRect(160, 160, 251, 31)) self.lineEdit_3.setText("") self.lineEdit_3.setObjectName("lineEdit_3") self.label_4 = QtWidgets.QLabel(parent=self.centralwidget) self.label_4.setGeometry(QtCore.QRect(0, 210, 151, 31)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label_4.setFont(font) self.label_4.setAutoFillBackground(False) self.label_4.setTextFormat(QtCore.Qt.TextFormat.AutoText) self.label_4.setWordWrap(False) self.label_4.setObjectName("label_4") self.lineEdit_4 = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit_4.setGeometry(QtCore.QRect(160, 210, 251, 31)) self.lineEdit_4.setText("") self.lineEdit_4.setObjectName("lineEdit_4") self.pushButton_2 = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(420, 60, 91, 81)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.clicked.connect(self.ekle) self.pushButton_3 = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(520, 60, 91, 81)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_3.clicked.connect(self.sil) self.pushButton_4 = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton_4.setGeometry(QtCore.QRect(520, 160, 91, 81)) self.pushButton_4.setObjectName("pushButton_4") self.tableWidget = QtWidgets.QTableWidget(parent=self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(0, 270, 611, 481)) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(0) self.tableWidget.setRowCount(0) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(parent=MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) try: # Veritabanına bağlantı oluşturma connection = connect() # Bağlantı üzerinden bir cursor (imleç) oluşturma cursor = connection.cursor() # SQL sorgusuyla tablodan verileri alma cursor.execute("SELECT ad, id, sehir, sektor FROM Musteriler") data = cursor.fetchall() # Veritabanı bağlantısını ve cursoru kapatma cursor.close() connection.close() # Tabloya verileri ekleme self.tabloyu_guncelle() # Tablo başlıkları header_labels = ["Müşteri Adı", "Müşteri No", "Şehir", "Sektor"] self.tableWidget.setHorizontalHeaderLabels(header_labels) except pyodbc.Error as error: print("Hata:", error)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "GÜNCELLE"))
        self.label.setText(_translate("MainWindow", "MUSTERİ ADI:"))
        self.label_2.setText(_translate("MainWindow", "MÜŞTERİ NO:"))
        self.label_3.setText(_translate("MainWindow", "ŞEHİR:"))
        self.label_4.setText(_translate("MainWindow", "MÜŞTERİ SEKTÖR:"))
        self.pushButton_2.setText(_translate("MainWindow", "EKLE"))
        self.pushButton_3.setText(_translate("MainWindow", "SİL"))
        self.pushButton_4.setText(_translate("MainWindow", "GÖRÜNTÜLE"))
    
    
    def tabloyu_guncelle(self):
        try:
            connection = connect()
    
            # Tablo widget’ını temizle
            self.tableWidget.clearContents()
            self.tableWidget.setRowCount(0)
    
            # Veritabanından verileri getir
            with connection:
                cursor = connection.cursor()
                cursor.execute("SELECT ad, id, sehir, sektor FROM Musteriler")
                data = cursor.fetchall()
    
            # Tablo widget’ını yeni verilerle güncelle
            self.tableWidget.setRowCount(len(data))
            self.tableWidget.setColumnCount(4 if len(data)==0 else len(data[0]))
            for row_num, row_data in enumerate(data):
                for col_num, col_data in enumerate(row_data):
                    item = QtWidgets.QTableWidgetItem(str(col_data))
                    self.tableWidget.setItem(row_num, col_num, item)
    
        except pyodbc.Error as error:
            print("Hata:", error)
    
    
    
    def ekle(self):
        try:
            connection = connect()
            musteri_adi = self.lineEdit.text()
            musteri_no = self.lineEdit_2.text()
            sehir = self.lineEdit_3.text()
            musteri_sektor = self.lineEdit_4.text()
           
            # Veritabanına bağlantı oluşturma ve with bloğu içinde bağlantıyı kullanma
            with connection:
               
                # Bağlantı üzerinden bir cursor oluşturma
                cursor = connection.cursor()
                # Müşteri numarası mevcut ise hata mesajı göster
                cursor.execute("SELECT COUNT(*) FROM Musteriler WHERE id = ?", (musteri_no,))
                result = cursor.fetchone()
                if result and result[0] > 0:  
                    QtWidgets.QMessageBox.critical(self.centralwidget, "HATA YAPTIN AHMETCİĞİM", "Böyle bir müşteri numarası var zaten. Ne yapmaya çalışıyorsun abi ???")
                    return 
                # Müşteri verilerini eklemek için INSERT INTO sorgusu
                query = "INSERT INTO Musteriler (ad, id, sehir, sektor) VALUES (?, ?, ?, ?)"
                cursor.execute(query, (musteri_adi, musteri_no, sehir, musteri_sektor))
                
                # Değişiklikleri kaydetme ve işlemi tamamlama
                connection.commit()
                print("Müşteri başarıyla eklendi.")
           
            # Yeni müşteri ekledikten sonra tabloya verileri ekleme kısmı
            self.tabloyu_guncelle()
           
            # Bağlantıyı kapatma
            connection.close()
        
        except pyodbc.Error as error:
            print("Hata:", error)
    
    def sil(self):
         try:
            # Bağlantıyı al
            connection = connect()
            selected_items = self.tableWidget.selectedItems()
            
            if selected_items:
                selected_row = selected_items[0].row()
                musteri_no = self.tableWidget.item(selected_row, 1).text()
    
                with connection:
                    cursor = connection.cursor()
                    query = "DELETE FROM Musteriler WHERE id = ?"
                    cursor.execute(query, (musteri_no,))
                    connection.commit()
                    print(f"Seçilen satırın müşteri numarası {musteri_no} olan veri başarıyla silindi.")
                    self.tableWidget.removeRow(selected_row)
            
            # Tablo Güncelleme
            self.tabloyu_guncelle()
         
         
         except pyodbc.Error as error:
            print("Hata:", error)
    
    def guncelle(self):
        try:
            # Seçilen satırı al
            selected_items = self.tableWidget.selectedItems()
            
            if selected_items:
                selected_row = selected_items[0].row()
                musteri_adi = self.tableWidget.item(selected_row, 0).text()
                musteri_no = self.tableWidget.item(selected_row, 1).text()
                sehir = self.tableWidget.item(selected_row, 2).text()
                musteri_sektor = self.tableWidget.item(selected_row, 3).text()
    
                # Yeni pencere oluşturma
                dialog = QtWidgets.QDialog()
                dialog.setWindowTitle("Değerleri Güncelle")
                
                # Yeni pencereye eklenecek bileşenleri oluşturma
                label_adi = QtWidgets.QLabel("Müşteri Adı:", dialog)
                lineEdit_adi = QtWidgets.QLineEdit(dialog)
                lineEdit_adi.setText(musteri_adi)
                
                label_no = QtWidgets.QLabel("Müşteri No:", dialog)
                lineEdit_no = QtWidgets.QLineEdit(dialog)
                lineEdit_no.setText(musteri_no)
                
                label_sehir = QtWidgets.QLabel("Şehir:", dialog)
                lineEdit_sehir = QtWidgets.QLineEdit(dialog)
                lineEdit_sehir.setText(sehir)
                
                label_sektor = QtWidgets.QLabel("Müşteri Sektör:", dialog)
                lineEdit_sektor = QtWidgets.QLineEdit(dialog)
                lineEdit_sektor.setText(musteri_sektor)
                
                button = QtWidgets.QPushButton("Tamam", dialog)
                
                # Yeni penceredeki bileşenleri düzenleme
                layout = QtWidgets.QVBoxLayout(dialog)
                layout.addWidget(label_adi)
                layout.addWidget(lineEdit_adi)
                layout.addWidget(label_no)
                layout.addWidget(lineEdit_no)
                layout.addWidget(label_sehir)
                layout.addWidget(lineEdit_sehir)
                layout.addWidget(label_sektor)
                layout.addWidget(lineEdit_sektor)
                layout.addWidget(button)
                
                # Tamam düğmesine tıklandığında verileri güncelleme işlemini yap
                button.clicked.connect(lambda: self.verileri_guncelle(selected_row, lineEdit_adi.text(), lineEdit_no.text(), lineEdit_sehir.text(), lineEdit_sektor.text(), dialog))
                
                # Yeni pencereyi modala getir
                dialog.exec()
                
        except pyodbc.Error as error:
            print("Hata:", error)
    
    def verileri_guncelle(self, row, ad, no, sehir, sektor, dialog):
        try:
            connection = connect()
            
            with connection:
                cursor = connection.cursor()
                query = "UPDATE Musteriler SET ad = ?, id = ?, sehir = ?, sektor = ? WHERE id = ?"
                cursor.execute(query, (ad, no, sehir, sektor, no))
                connection.commit()
                print(f"Satır güncellendi: {ad} - {no} - {sehir} - {sektor}")
            
            self.tabloyu_guncelle()    
            dialog.close()
            
            # Pop-up mesajı göster
            QMessageBox.information(self.centralwidget, "Başarılı", "Güncelleme başarıyla tamamlandı.")
            
            
        except pyodbc.Error as error:
            print("Hata:", error)
    

    if name == "main": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec())

  2. Assistant

    The given code is a GUI application that allows users to manage customer information in a database. The application uses PyQt6 framework and connects to a SQL Server database using pyodbc module.

    The main functionalities of the application are:

    • Add a new customer by entering their name, ID, city, and sector.
    • Delete a selected customer from the database.
    • Update the information of a selected customer.
    • View the list of customers in a table.

    The code consists of a main window class (Ui_MainWindow) that sets up the user interface and handles the events triggered by user actions. The class has methods for setting up the UI, connecting to the database, updating the table, adding a new customer, deleting a customer, updating a customer, and displaying a message box.

    The setupUi() method sets up the main window, including the labels, input fields, buttons, and table widget. It also establishes a connection to the database, retrieves the customer data from the database, and populates the table widget with the data.

    The tabloyu_guncelle() method is used to update the table widget with the latest customer data from the database.

    The ekle() method is triggered when the "EKLE" button is clicked. It retrieves the customer information from the input fields, checks if the customer ID already exists in the database, and inserts the new customer into the database. It then updates the table widget with the new data.

    The sil() method is triggered when the "SİL" button is clicked. It retrieves the selected customer from the table widget, deletes the customer from the database, and updates the table widget with the updated data.

    The guncelle() method is triggered when the "GÜNCELLE" button is clicked. It retrieves the selected customer from the table widget, opens a new dialog window to edit the customer information, and updates the customer's information in the database.

    The verileri_guncelle() method is called when the "Tamam" button in the edit dialog window is clicked. It updates the customer's information in the database with the edited information and updates the table widget with the new data.

    The main block at the bottom of the code creates an instance of the main window class, sets up the UI, and starts the application's event loop.

    Note: The code connects to a specific SQL Server instance and database. You may need to modify the connection string in the connect() method to match your own environment.

  3. User

    müşteri adını yazıp entere bastığımda müşteri noya geçsin onuda yazıp entere bastığımda şehire geçsin onuda yazıp entere bastığımda sektöre geçsin onuda yazıp entere bastığımda bir popout açılsın müşteriyi eklemek istiyor musunuz diye . evete bastığımda müşteriyi veritabanına eklesin ve lineların içi boşalsın türkçe anlat

  4. Assistant

    Eklenmek istenen özelliği kullanabilmek için UI_MainWindow sınıfının setupUi metodunda birkaç değişiklik yapmanız gerekmektedir. Aşağıda Türkçe açıklamalar ile yapılan değişiklikleri görebilirsiniz:

    def setupUi(self, MainWindow):
        # ...
        
        # QLineEdit bileşenlerinin enter tuşuna basıldığında sıradaki QLineEdit bileşene odaklanması için 
        # returnPressed sinyallerini slot metotlarıyla bağlayın.
        self.lineEdit.returnPressed.connect(self.lineEdit_2.setFocus)
        self.lineEdit_2.returnPressed.connect(self.lineEdit_3.setFocus)
        self.lineEdit_3.returnPressed.connect(self.lineEdit_4.setFocus)
        self.lineEdit_4.returnPressed.connect(self.popout_goster)
    
        # ...
    
    def popout_goster(self):
        # QLineEdit bileşenlerindeki verileri alın
        musteri_adi = self.lineEdit.text()
        musteri_no = self.lineEdit_2.text()
        sehir = self.lineEdit_3.text()
        musteri_sektor = self.lineEdit_4.text()
        
        # Alınan veriler ile bir pop-out dialog penceresi oluşturun
        popout = QMessageBox()
        popout.setWindowTitle("Müşteri Ekle")
        popout.setText(f"Müşteri Adı: {musteri_adi}\nMüşteri No: {musteri_no}\nŞehir: {sehir}\nMüşteri Sektör: {musteri_sektor}")
        popout.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
        
        # Pop-out penceresindeki Evet düğmesine basılması durumunda veritabanına müşteriyi ekleme işlemini yapın
        if popout.exec_() == QMessageBox.StandardButton.Yes:
            self.ekle()
            self.lineEdit.clear()  # QLineEdit bileşen