Qt Python日历:直接访问选定日期

0 投票
1 回答
1632 浏览
提问于 2025-04-15 17:39

我有一个日历,运行得很好。

这是一个用来显示完整日期的函数:

def selectDate(self,date):
    self.fullDate = str(date.day()) + " / " + str(date.month()) + " / " + str(date.year())
    print "full date: %s" % self.fullDate

这是包含日历的代码:

def TabCalendar(self):
    self.calendar = QtGui.QCalendarWidget(self.tab)
    self.calendar.setGeometry(QtCore.QRect(self.x1, self.y1, self.x2, self.y2)) 

    QtCore.QObject.connect(self.calendar, QtCore.SIGNAL("selectionChanged()"), self.selectDate)
    QtCore.QObject.connect(self.calendar, QtCore.SIGNAL("clicked(QDate)"), self.selectDate)

为了能直接访问选中的日期,我根据连接事件调用了 selectDate 函数,然后使用 'date' 来获取具体的日期和天数等等——这一切都运行得不错。

唯一让我觉得尴尬的就是,它给我发出了以下警告……

TypeError: turbSchedule_selectDate() takes exactly 2 arguments (1 given)

有没有什么建议可以让我停止这个 TypeError 警告?

非常感谢所有的评论和建议。

1 个回答

2

我想说,selectdate信号调用的那个槽函数不应该有任何参数。你可以通过对应的日历方法来获取选中的日期。

可以查看C++的文档:http://doc.trolltech.com/4.3/widgets-calendarwidget.html

所以你的代码应该像这样:

def selectDate(self):
    date = self.calendar.selectedDate()
    self.fullDate = str(date.day()) + " / " + str(date.month()) + " / " + str(date.year())
    print "full date: %s" % self.fullDate

撰写回答