R
R
Rishat Sultanov2016-12-09 17:31:56
Python
Rishat Sultanov, 2016-12-09 17:31:56

How to correctly call a class method and pass arguments to it from another class?

Good evening gentlemen.
I can't make a window. into which you enter a SQL query and it works.
I made a window and a button for sending a request, but there were problems in the OOP style.
How to solve this error.

# -*- coding: utf-8 -*-
import sys,sqlite3
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout

class Obrabotka:

    def sql(sqltext):
        con = sqlite3.connect("students.db")
        cur = con.cursor()  # Создаем объект-курсор
        try:
            cur.execute(str(sqltext))
        except sqlite3.DatabaseError, err:
            print u"Ошибка:", err
        else:
            print u"Запрос успешно выполнен"
        cur.close()
        con.close()

class Form(QDialog):
    def __init__(self, parent=None):

        super(Form, self).__init__(parent)

        self.le = QLineEdit()
        self.le.setObjectName("host")
        self.le.setText("")

        self.pb = QPushButton()
        self.pb.setObjectName("connect")
        self.pb.setText(u"Отправить")

        layout = QFormLayout()
        layout.addWidget(self.le)
        layout.addWidget(self.pb)

        self.setLayout(layout)
        self.connect(self.pb, SIGNAL("clicked()"),self.button_click)
        self.setWindowTitle(u"Запрос")



    def button_click(self):
        # shost is a QString object
        sqltext = self.le.text()
        Obrabotka.sql(sqltext)

ERROR
D:\Program\Python27\python.exe D:/workspace/pycharm/Project/Qt/Project3/test.py
Traceback (most recent call last):
  File "D:/workspace/pycharm/Project/Qt/Project3/test.py", line 46, in button_click
    Obrabotka.sql(sqltext)
TypeError: unbound method sql() must be called with Obrabotka instance as first argument (got QString instance instead)

Process finished with exit code 0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil Biryukov-Romanov, 2016-12-09
@rishatss

Create an instance of the class:

def button_click(self):
        # shost is a QString object
        sqltext = self.le.text()
        obr = Obrabotka()
        obr.sql(sqltext)

A
ape364, 2016-12-11
@ape364

No need to call the Processing class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question