R
R
Rishat Sultanov2016-12-08 16:56:07
Python
Rishat Sultanov, 2016-12-08 16:56:07

How to pull data from QT to Python variable?

Good evening gentlemen :)
I'm practicing with QT Designer and I wanted to implement queries to the database (SQL) GUI.
QT Designer sketched a window, punched a link to a copy when a button was pressed.
UI code in Python. (Graphic arts)

# -*- coding: utf-8 -*-



from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.backendsql = QtGui.QFrame(self.centralwidget)
        self.backendsql.setGeometry(QtCore.QRect(10, 10, 771, 461))
        self.backendsql.setFrameShape(QtGui.QFrame.StyledPanel)
        self.backendsql.setFrameShadow(QtGui.QFrame.Raised)
        self.backendsql.setObjectName(_fromUtf8("backendsql"))
        self.zapros = QtGui.QLineEdit(self.backendsql)
        self.zapros.setGeometry(QtCore.QRect(10, 10, 741, 431))
        self.zapros.setCursor(QtGui.QCursor(QtCore.Qt.IBeamCursor))
        self.zapros.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.zapros.setAutoFillBackground(False)
        self.zapros.setText(_fromUtf8(""))
        self.zapros.setObjectName(_fromUtf8("zapros"))
        self.sql = QtGui.QPushButton(self.centralwidget)
        self.sql.setGeometry(QtCore.QRect(190, 470, 401, 71))
        self.sql.setObjectName(_fromUtf8("sql"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.sql, QtCore.SIGNAL(_fromUtf8("clicked()")), self.zapros.copy)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.sql.setText(_translate("MainWindow", "Отправить запросище", None))

Output:
E2f087f5.jpg
My script ( Loading graphics and waiting for a variable )
#!/usr/bin/python
# -*- coding: utf-8 -*-
# python 2.7
# ------------------------------------------------

import sys,sqlite3
from PyQt4 import QtGui, uic,QtCore



class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        uic.loadUi('sql.ui', self)
        # sql =

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


app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
w.setWindowTitle(u"SQL Rishat")
sys.exit(app.exec_())

My script that should launch the graphical interface, get data from it on a button click. (The query that the user entered in the graphical interface)
Actually the question is this. How to implement it?
help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2016-12-08
@rishatss

"I made a connection to a copy when the button was pressed" it's not at all clear what they were trying to do.
I recommend making the onSendButtonClicked method in MainWindow, and making a connection to it instead of what you have.
Well, it already has something like text = self.uic.zapros.text()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question