PyQt和按钮

时间:2016-02-25 13:39:59

标签: python qt pyqt

我想通过PyQt在文本字段上分配def和打印结果。该脚本已经在终端上工作,所以我的问题是在Qt部分。这是我的代码:

# -*- 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_())

所以我有2个日期字段和1个按钮。当我点击' sorgula'按钮它应该采取写在' tarih1'文本字段并运行def lme():通过该日期信息并将sql查询结果返回到' resultsView'文本字段。

谢谢

1 个答案:

答案 0 :(得分:2)

我认为你以错误的方式调用你的函数而你也没有连接信号。要么你可以在你内部移动你的功能,你可以将它保持在外面,并将你的日期作为参数传递。我刚搬进了gui班。

#!/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_())