gpt4 book ai didi

python - 如何在 PyQt4 日历小部件中为日期着色?

转载 作者:太空宇宙 更新时间:2023-11-03 18:59:02 25 4
gpt4 key购买 nike

我想在点击它时绘制日期。这是代码:日期 - 我想绘制的对象。希望得到帮助...

class Example(QtGui.QWidget):   
def __init__(self):
super(Example, self).__init__()

self.initUI()

def initUI(self):

cal = QtGui.QCalendarWidget(self)
cal.setGridVisible(True)
cal.clicked[QtCore.QDate].connect(self.showDate)
self.lbl = QtGui.QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(130, 150)
self.setGeometry(50, 50, 500, 400)
self.setWindowTitle('Calendar')
self.resize(500,400)
self.show()

def showDate(self, date):
self.lbl.setText(date.toString())
print date
print str(date.day()) + "/" + str(date.month()) + "/" + str(date.year()

最佳答案

我不太确定你在这里想做什么,但由于我自己正在修改 zetcode.com 上的 PyQt4 教程,所以我想我可以提出一些建议。

您可以通过添加样式表来更改lbl的字体颜色。如果您单击日历中的日期,打印的标签将变成绿色:

def showDate(self, date):
self.label.setText(date.toString())
self.label.setStyleSheet('color: green')

如果您想为标签的背景着色,请按以下方式操作:

def showDate(self, date):
self.label.setText(date.toString())
self.label.setStyleSheet('background-color: red')
<小时/>

我突然想到您可能指的是打印而不是绘画。因此,这里有一种以您的样式格式打印日期的方法:

# in your initUI method:
self.dateLabel = QtGui.QLabel(self)
date = cal.selectedDate()
self.dateLabel.setText("{}/{}/{}".format(date.day(),
date.month(), date.year())) # output: 20/9/2013

# change your showDate method to this:
def showDate(self, date):

myDateFormat = "{}/{}/{}".format(date.day(), date.month(),
date.year())
self.dateLabel.setText(myDateFormat) # output: 1/1/2013
self.dateLabel.adjustSize()

关于python - 如何在 PyQt4 日历小部件中为日期着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com