如何通过单击Qt Designer中的按钮来打印日期?

时间:2015-11-20 14:45:49

标签: python forms qt pyqt4

我正在尝试通过Qt设计器创建一个表单,该表单将由用户经常更新。我想允许用户选择日期并保存该日期,以便跟踪表单上次更新的时间。The image is what my form currently looks like. If the user clicks the 'save date' button, the date selected just above will be saved.

我正在使用python对此进行编程,目前这就是我的代码:

from PyQt4 import QtCore, QtGui
import sys

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_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(785, 596)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.comboBox_2 = QtGui.QComboBox(Form)
        self.comboBox_2.setObjectName(_fromUtf8("comboBox_2"))
        self.gridLayout.addWidget(self.comboBox_2, 18, 0, 1, 1)
        self.checkBox = QtGui.QCheckBox(Form)
        self.checkBox.setObjectName(_fromUtf8("checkBox"))
        self.gridLayout.addWidget(self.checkBox, 3, 0, 1, 1)
        self.comboBox = QtGui.QComboBox(Form)
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.gridLayout.addWidget(self.comboBox, 19, 0, 1, 1)
        self.lineEdit_3 = QtGui.QLineEdit(Form)
        self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
        self.gridLayout.addWidget(self.lineEdit_3, 2, 0, 1, 1)
        self.textBrowser_3 = QtGui.QTextBrowser(Form)
        self.textBrowser_3.setObjectName(_fromUtf8("textBrowser_3"))
        self.gridLayout.addWidget(self.textBrowser_3, 13, 0, 1, 2)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 4, 0, 1, 1)
        self.lineEdit_2 = QtGui.QLineEdit(Form)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.gridLayout.addWidget(self.lineEdit_2, 1, 0, 1, 1)
        self.textBrowser_2 = QtGui.QTextBrowser(Form)
        self.textBrowser_2.setObjectName(_fromUtf8("textBrowser_2"))
        self.gridLayout.addWidget(self.textBrowser_2, 10, 0, 1, 2)
        self.PrintDate_btn = QtGui.QPushButton(Form)
        self.PrintDate_btn.setObjectName(_fromUtf8("PrintDate_btn"))
        self.gridLayout.addWidget(self.PrintDate_btn, 16, 0, 1, 1)
        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.gridLayout.addWidget(self.textEdit, 7, 0, 1, 2)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 1)
        spacerItem1 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 8, 0, 1, 1)
        spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem2, 0, 1, 1, 1)
        self.label = QtGui.QLabel(Form)
        self.label.setObjectName(_fromUtf8("label"))
        self.gridLayout.addWidget(self.label, 5, 0, 1, 1)
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.gridLayout.addWidget(self.label_2, 9, 0, 1, 1)
        self.label_3 = QtGui.QLabel(Form)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.gridLayout.addWidget(self.label_3, 11, 0, 1, 1)
        spacerItem3 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem3, 12, 0, 1, 1)
        self.dateEdit = QtGui.QDateEdit(Form)
        self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
        self.gridLayout.addWidget(self.dateEdit, 14, 0, 1, 1)


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Enterprise Passport", None))
        self.checkBox.setText(_translate("Form", "1st Gen university", None))
        self.comboBox.setItemText(0, _translate("Form", "award1", None))
        self.comboBox.setItemText(1, _translate("Form", "award2", None))
        self.comboBox.setItemText(2, _translate("Form", "award3", None))
        self.PrintDate_btn.setText(_translate("Form", "SaveDate", None))
        self.label.setText(_translate("Form", "Activities", None))
        self.label_2.setText(_translate("Form", "Achievements", None))
        self.label_3.setText(_translate("Form", "Awards", None))
        self.PrintDate_btn.clicked.connect(self.PrintDate)



    def PrintDate(self):
        print(Ui_Form.setupUi(self.dateEdit))


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

我已经尝试了各种各样的东西,但我似乎无法保存甚至打印按下按钮时选择的日期。有人可以告诉我如何才能让它发挥作用吗?

1 个答案:

答案 0 :(得分:0)

  def PrintDate(self):
        print self.dateEdit.text()