QCalendar 示例中的全名月份格式
Full name month format in QCalendar sample
我在 PyQt5 中编写的应用程序需要一个日历小部件,我找到了这个示例源代码:
import sys
from PyQt5 import *
from PyQt5.QtWidgets import QApplication, QCalendarWidget, QWidget, QLabel
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
my_calendar = QCalendarWidget(self)
my_calendar.setGridVisible(True)
my_calendar.move(10, 20)
my_calendar.clicked[QDate].connect(self.show_date)
self.my_label = QLabel(self)
date = my_calendar.selectedDate()
self.my_label.setText(date.toString())
self.my_label.move(10, 220)
self.setGeometry(100,100,600,270)
self.setWindowTitle('Calendar')
self.show()
def show_date(self, date):
self.my_label.setText(date.toString())
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
并且结果应该像开发者所说的这张照片:
但是当我 运行 我的系统中的这段代码时,我得到了除月份格式之外的所有内容,我怎么能有全名月份格式,如 May,June,...而不是 M01,M02,.. .
这是我 运行 我系统中的代码时得到的结果:
正如 docs 指出的那样:
QString QDate::toString(Qt::DateFormat format = Qt::TextDate
This is an overloaded function.
Returns the date as a string. The format parameter determines the
format of the string.
If the format is Qt::TextDate, the string is formatted in the default
way. QDate::shortDayName() and QDate::shortMonthName() are used to
generate the string, so the day and month names will be localized
names using the system locale, i.e. QLocale::system(). An example of
this formatting is "Sat May 20 1995".
If the format is Qt::ISODate, the string format corresponds to the ISO
8601 extended specification for representations of dates and times,
taking the form yyyy-MM-dd, where yyyy is the year, MM is the month of
the year (between 01 and 12), and dd is the day of the month between
01 and 31.
If the format is Qt::SystemLocaleShortDate or
Qt::SystemLocaleLongDate, the string format depends on the locale
settings of the system. Identical to calling
QLocale::system().toString(date, QLocale::ShortFormat) or
QLocale::system().toString(date, QLocale::LongFormat).
If the format is Qt::DefaultLocaleShortDate or
Qt::DefaultLocaleLongDate, the string format depends on the default
application locale. This is the locale set with QLocale::setDefault(),
or the system locale if no default locale has been set. Identical to
calling QLocale().toString(date, QLocale::ShortFormat) or
QLocale().toString(date, QLocale::LongFormat).
If the format is Qt::RFC2822Date, the string is formatted in an RFC
2822 compatible way. An example of this formatting is "20 May 1995".
If the date is invalid, an empty string will be returned.
Warning: The Qt::ISODate format is only valid for years in the range 0
to 9999. This restriction may apply to locale-aware formats as well,
depending on the locale settings.
(强调我的)
如前所述,格式取决于 QLocale,在您的情况下,您的 QLocale 可能与开发人员不同,因此有 2 种解决方案:
在应用程序中设置 QLocale:
app = QApplication(sys.argv)
QLocale.setDefault(QLocale(QLocale.English)) # <---
# ...
在 QCalendarWidget 中设置 QLocale:
my_calendar.setLocale(QLocale(QLocale.English))
我在 PyQt5 中编写的应用程序需要一个日历小部件,我找到了这个示例源代码:
import sys
from PyQt5 import *
from PyQt5.QtWidgets import QApplication, QCalendarWidget, QWidget, QLabel
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
my_calendar = QCalendarWidget(self)
my_calendar.setGridVisible(True)
my_calendar.move(10, 20)
my_calendar.clicked[QDate].connect(self.show_date)
self.my_label = QLabel(self)
date = my_calendar.selectedDate()
self.my_label.setText(date.toString())
self.my_label.move(10, 220)
self.setGeometry(100,100,600,270)
self.setWindowTitle('Calendar')
self.show()
def show_date(self, date):
self.my_label.setText(date.toString())
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
并且结果应该像开发者所说的这张照片:
但是当我 运行 我的系统中的这段代码时,我得到了除月份格式之外的所有内容,我怎么能有全名月份格式,如 May,June,...而不是 M01,M02,.. . 这是我 运行 我系统中的代码时得到的结果:
正如 docs 指出的那样:
QString QDate::toString(Qt::DateFormat format = Qt::TextDate
This is an overloaded function.
Returns the date as a string. The format parameter determines the format of the string.
If the format is Qt::TextDate, the string is formatted in the default way. QDate::shortDayName() and QDate::shortMonthName() are used to generate the string, so the day and month names will be localized names using the system locale, i.e. QLocale::system(). An example of this formatting is "Sat May 20 1995".
If the format is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form yyyy-MM-dd, where yyyy is the year, MM is the month of the year (between 01 and 12), and dd is the day of the month between 01 and 31.
If the format is Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(date, QLocale::ShortFormat) or QLocale::system().toString(date, QLocale::LongFormat).
If the format is Qt::DefaultLocaleShortDate or Qt::DefaultLocaleLongDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(date, QLocale::ShortFormat) or QLocale().toString(date, QLocale::LongFormat).
If the format is Qt::RFC2822Date, the string is formatted in an RFC 2822 compatible way. An example of this formatting is "20 May 1995".
If the date is invalid, an empty string will be returned.
Warning: The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to locale-aware formats as well, depending on the locale settings.
(强调我的)
如前所述,格式取决于 QLocale,在您的情况下,您的 QLocale 可能与开发人员不同,因此有 2 种解决方案:
在应用程序中设置 QLocale:
app = QApplication(sys.argv) QLocale.setDefault(QLocale(QLocale.English)) # <--- # ...
在 QCalendarWidget 中设置 QLocale:
my_calendar.setLocale(QLocale(QLocale.English))