0
我正在嘗試通過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.如何通過單擊Qt Designer中的按鈕來打印日期?
我使用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_())
我已經嘗試了各種各樣的事情,但我似乎無法得到它的保存或甚至打印按下按鈕時所選的日期。有人能告訴我如何讓這個工作?
如果您添加了幾個解釋詞,您的答案會更好。此外,你給出的代碼是正確的,但它只能用於python2(它看起來像OP可能使用python3)。 – ekhumoro
對不起,我沒有注意到OP正在使用python3。 解釋是 - 文件不完整。檢查對象功能的最簡單方法是調用dir() – user5509884