from PyQt6 import QtCore, QtGui, QtWidgets import pyodbc def connect(): connstr =…
- User
from PyQt6 import QtCore, QtGui, QtWidgets 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 EditDialog(QtWidgets.QDialog): def init(self, parent=None): super(EditDialog, self).init(parent) self.setWindowTitle("Değerleri Düzenle")
self.layout = QtWidgets.QVBoxLayout() self.label_1 = QtWidgets.QLabel("Müşteri Adı:") self.lineEdit_1 = QtWidgets.QLineEdit() self.label_2 = QtWidgets.QLabel("Müşteri No:") self.lineEdit_2 = QtWidgets.QLineEdit() self.label_3 = QtWidgets.QLabel("Şehir:") self.lineEdit_3 = QtWidgets.QLineEdit() self.label_4 = QtWidgets.QLabel("Müşteri Sektör:") self.lineEdit_4 = QtWidgets.QLineEdit() self.buttonBox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) self.layout.addWidget(self.label_1) self.layout.addWidget(self.lineEdit_1) self.layout.addWidget(self.label_2) self.layout.addWidget(self.lineEdit_2) self.layout.addWidget(self.label_3) self.layout.addWidget(self.lineEdit_3) self.layout.addWidget(self.label_4) self.layout.addWidget(self.lineEdit_4) self.layout.addWidget(self.buttonBox) self.setLayout(self.layout) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) def setValues(self, ad, id, sehir, sektor): self.lineEdit_1.setText(ad) self.lineEdit_2.setText(id) self.lineEdit_3.setText(sehir) self.lineEdit_4.setText(sektor) def getValues(self): ad = self.lineEdit_1.text() id = self.lineEdit_2.text() sehir = self.lineEdit_3.text() sektor = self.lineEdit_4.text() return ad, id, sehir, sektorclass 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.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)
- Assistant
The given code is a PyQt6 application that connects to a SQL Server database and allows the user to add, update, and delete data from the database.
The code consists of three classes:
-
EditDialog: This class is a QDialog subclass that creates a dialog window for editing data. It has four QLineEdit fields for entering values and a QDialogButtonBox for accepting or rejecting the changes. -
Ui_MainWindow: This class is responsible for setting up the main window of the application. It includes QLineEdit fields for entering values, QPushButtons for performing actions such as adding, updating, or deleting data, and a QTableWidget for displaying the data from the database. It also handles the events triggered by the buttons. -
connect: This function establishes a connection to the SQL Server database using the pyodbc library. It returns the connection object.
The
Ui_MainWindowclass has methods for setting up the UI, updating the table widget, adding data to the database, and deleting data from the database.The code also includes the main part that initializes the application, creates an instance of the class, and runs the application.
-
- User
editdialog classını çalıştırıp güncelleme işlemnii yapacak bir guncelle fonksiyonu yazar mısın
- Assistant
Tablolardan bir satır seçildiğinde güncelleme işlemini gerçekleştirecek bir
guncellefonksiyonu aşağıdaki gibi olabilir:def guncelle(self): selected_items = self.tableWidget.selectedItems() if selected_items: selected_row = selected_items[0].row() ad = self.tableWidget.item(selected_row, 0).text() id = self.tableWidget.item(selected_row, 1).text() sehir = self.tableWidget.item(selected_row, 2).text() sektor = self.tableWidget.item(selected_row, 3).text() dialog = EditDialog() dialog.setValues(ad, id, sehir, sektor) if dialog.exec() == QtWidgets.QDialog.Accepted: new_ad, new_id, new_sehir, new_sektor = dialog.getValues() try: connection = connect() with connection: cursor = connection.cursor() query = "UPDATE Musteriler SET ad = ?, id = ?, sehir = ?, sektor = ? WHERE id = ?" cursor.execute(query, (new_ad, new_id, new_sehir, new_sektor, id)) connection.commit() print(f"Güncelleme işlemi tamamlandı.") self.tabloyu_guncelle() except pyodbc.Error as error: print("Hata:", error) - User
File "c:\Users\harit\OneDrive\Masa³st³\Python\Deneme\proje.py", line 53, in setValues self.lineEdit_1.setText(ad) ^^^^^^^^^^^^^^^ AttributeError: 'EditDialog' object has no attribute 'lineEdit_1'
- Assistant
class EditDialog'in
initmetodunda hata yapmışım, QLineEdit nesnelerine doğru isimleri atamamışım. Aşağıda düzeltmiş hali verilmiştir:class EditDialog(QtWidgets.QDialog): def __init__(self, parent=None): super(EditDialog, self).__init__(parent) self.setWindowTitle("Değerleri Düzenle") self.layout = QtWidgets.QVBoxLayout() self.label_1 = QtWidgets.QLabel("Müşteri Adı:") self.lineEdit_1 = QtWidgets.QLineEdit() self.label_2 = QtWidgets.QLabel("Müşteri No:") self.lineEdit_2 = QtWidgets.QLineEdit() self.label_3 = QtWidgets.QLabel("Şehir:") self.lineEdit_3 = QtWidgets.QLineEdit() self.label_4 = QtWidgets.QLabel("Müşteri Sektör:") self.lineEdit_4 = QtWidgets.QLineEdit() self.buttonBox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) self.layout.addWidget(self.label_1) self.layout.addWidget(self.lineEdit_1) self.layout.addWidget(self.label_2) self.layout.addWidget(self.lineEdit_2) self.layout.addWidget(self.label_3) self.layout.addWidget(self.lineEdit_3) self.layout.addWidget(self.label_4) self.layout.addWidget(self.lineEdit_4) self.layout.addWidget(self.buttonBox) self.setLayout(self.layout) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) - User
hala aynı hata
- Assistant
Eksiklik için özür dilerim,
EditDialogsınıfındaki__init__metodunu düzelttim. Şimdi doğru bir şekilde çalışması gerekiyor. İşte düzeltilmişEditDialogsınıfı:class EditDialog(QtWidgets.QDialog): def __init__(self, parent=None): super(EditDialog, self).__init__(
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.