简体   繁体   English

PyQt和按钮

[英]PyQt and Buttons

I want to assign a def and print results on a text field via PyQt. 我想分配一个def并通过PyQt在文本字段上打印结果。 The script is already working on terminal so my issue is on Qt part. 该脚本已在终端上运行,所以我的问题在Qt部分。 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. 所以我有2个日期字段和1个按钮。 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. 当我单击“ sorgula”按钮时,它应该采用写在“ tarih1”文本字段上的日期并运行def lme():通过该日期信息并将sql查询结果返回到“ resultsView”文本字段。

Thank you 谢谢

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. 您可以将函数移到gui内部,也可以将其保留在外部并将日期作为参数传递。 I just moved inside the gui class. 我刚搬进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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM