简体   繁体   中英

Full name month format in QCalendar sample

I need a calendar widget for the app I am writing in PyQt5 and I found this sample source code:

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()

and In the result should be like as this pic as the developer says:

月格式是全名

but When i run this code in my system i get everything write except month format, how can I have full name month format like May,June,... and not M01,M02,... this is result i get when i run the code in my system:

在此处输入图片说明

As the docs point out:

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, ie 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.

(emphasis mine)

As noted, the formats depend on the QLocale, in your case you probably have a QLocale different from the developer, so there are 2 solutions:

  • Set QLocale in the application:

     app = QApplication(sys.argv) QLocale.setDefault(QLocale(QLocale.English)) # <--- # ... 
  • Set QLocale in QCalendarWidget:

     my_calendar.setLocale(QLocale(QLocale.English)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM