1

I want to assign a def and print results on a text field via PyQt. The script is already working on terminal so my issue is on Qt part. Here is my code:

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

# Form implementation generated from reading ui file '2Tarih.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import pyodbc

def lme():

    cnxn = pyodbc.connect('DSN=;UID=;PWD=')
    cursor = cnxn.cursor()
    cursor.execute("select CONVERT(date,Tarih,104) as Tarih, LME, USD, EURO from dbo.DovizLme where Tarih = REPLACE(CONVERT(char(10), PARSE('%s' AS datetime USING 'tr-TR'),101),'/','.')" % self.tarih1)
    rows = cursor.fetchall()
    for row in rows:
        print("Tarih:", row[0], "LME:", row[1], "USD:", row[2], "EUR:", row[3])

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_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(786, 520)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(440, 480, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.resultsView = QtGui.QPlainTextEdit(Dialog)
        self.resultsView.setGeometry(QtCore.QRect(10, 70, 771, 401))
        self.resultsView.setObjectName(_fromUtf8("resultsView"))
        self.sorgula = QtGui.QPushButton(Dialog)
        self.sorgula.setGeometry(QtCore.QRect(690, 10, 84, 33))
        self.sorgula.setObjectName(_fromUtf8("sorgula"))
        self.tarih1 = QtGui.QLineEdit(Dialog)
        self.tarih1.setGeometry(QtCore.QRect(10, 20, 151, 31))
        self.tarih1.setObjectName(_fromUtf8("tarih1"))
        self.tarih2 = QtGui.QLineEdit(Dialog)
        self.tarih2.setGeometry(QtCore.QRect(170, 20, 151, 31))
        self.tarih2.setObjectName(_fromUtf8("tarih2"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.sorgula.setText(_translate("Dialog", "Sorgula", None))
        self.tarih1.setPlaceholderText(_translate("Dialog", "Başlangıç Tarihi Giriniz", None))
        self.tarih2.setPlaceholderText(_translate("Dialog", "Bitiş Tarihi Giriniz", None))





if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

So I have 2 date field and 1 button. When i click the 'sorgula' button it should take the date written on 'tarih1' text field and run the def lme(): via that date info and return sql query results to 'resultsView' text field.

Thank you

1 Answer 1

2

I think you calling your function in wrong way and you are not connecting signals also. Either you can move the function inside you gui or you can keep it outside and pass your date as argument. I just moved inside the gui class.

#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
# import pyodbc

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_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(786, 520)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(440, 480, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.resultsView = QtGui.QPlainTextEdit(Dialog)
        self.resultsView.setGeometry(QtCore.QRect(10, 70, 771, 401))
        self.resultsView.setObjectName(_fromUtf8("resultsView"))
        self.sorgula = QtGui.QPushButton(Dialog)
        self.sorgula.setGeometry(QtCore.QRect(690, 10, 84, 33))
        self.sorgula.setObjectName(_fromUtf8("sorgula"))
        self.tarih1 = QtGui.QLineEdit(Dialog)
        self.tarih1.setGeometry(QtCore.QRect(10, 20, 151, 31))
        self.tarih1.setObjectName(_fromUtf8("tarih1"))
        self.tarih2 = QtGui.QLineEdit(Dialog)
        self.tarih2.setGeometry(QtCore.QRect(170, 20, 151, 31))
        self.tarih2.setObjectName(_fromUtf8("tarih2"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        self.sorgula.clicked.connect(self.lme)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def lme(self):
        textData = str(self.tarih1.text())
        cnxn = pyodbc.connect('DSN=;UID=;PWD=')
        cursor = cnxn.cursor()
        cursor.execute("select CONVERT(date,Tarih,104) as Tarih, LME, USD, EURO from dbo.DovizLme where Tarih = REPLACE(CONVERT(char(10), PARSE('%s' AS datetime USING 'tr-TR'),101),'/','.')" % textData)
        rows = cursor.fetchall()
        for row in rows:
            print("Tarih:", row[0], "LME:", row[1], "USD:", row[2], "EUR:", row[3])


    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.sorgula.setText(_translate("Dialog", "Sorgula", None))
        self.tarih1.setPlaceholderText(_translate("Dialog", "Başlangıç Tarihi Giriniz", None))
        self.tarih2.setPlaceholderText(_translate("Dialog", "Bitiş Tarihi Giriniz", None))





if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.