j4321 / tkcalendar

Calendar widget for Tkinter
https://pypi.python.org/pypi/tkcalendar
GNU General Public License v3.0
98 stars 33 forks source link

Calendar next month bug #15

Closed Guilhermeasper closed 5 years ago

Guilhermeasper commented 6 years ago

I've created a tkinter GUI, the main window contains a Calendar, and one of the register Top Level window contains the Date Entry. The bug occurs when i open my register window with the Date Entry(I don't need to do nothing on the window, just open). After this, when i try to change the month on the main window, i get a locale error and the Calendar doesn't work anymore. I've tried to find the bug by myself, but i wasn't able to. Some things i think it's important to say: If i don't change the locale to pt_BR, that is my language, doesn't happen the error, but the language of the month of the calendar change after i open the register window. I'll attach my project files here, maybe it helps. Here is the code from the Main window: https://pastebin.com/58v6UWP3 and here from the register window: https://pastebin.com/qYHdJPU7 The error that shows on the prompt is this:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Guilherme Afonso\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\Guilherme Afonso\AppData\Local\Programs\Python\Python36\lib\site-packages\tkcalendar.py", line 600, in _next_month
    self._display_calendar()
  File "C:\Users\Guilherme Afonso\AppData\Local\Programs\Python\Python36\lib\site-packages\tkcalendar.py", line 518, in _display_calendar
    header = self._cal.formatmonthname(year, month, 0, False)
  File "C:\Users\Guilherme Afonso\AppData\Local\Programs\Python\Python36\lib\calendar.py", line 529, in formatmonthname
    return s.center(width)
  File "C:\Users\Guilherme Afonso\AppData\Local\Programs\Python\Python36\lib\calendar.py", line 498, in __exit__
    _locale.setlocale(_locale.LC_TIME, self.oldlocale)
  File "C:\Users\Guilherme Afonso\AppData\Local\Programs\Python\Python36\lib\locale.py", line 598, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
Guilhermeasper commented 6 years ago

I've tried a lot of different changes on the Calendar class(where i thought the problem was), and then i realized that was everything fine on the calendar and the DateEntry it was causing the problem, so i changed the way the locale was set, and everything works great now. I've done the follow changes in the tkcalendar.py, inside the DateEntry class: I've commented the line where the "new" locale was set for the DateEntry: loc = kw.get('locale', '') #locale.setlocale(locale.LC_ALL, loc) And set the locale through the call of the Calendar: self._calendar = Calendar(self._top_cal, **kw, locale=loc)

j4321 commented 5 years ago

Thanks for reporting the issue. Usually, locale.Error: unsupported locale setting is raised when the locale you try to use in locale.setlocale(locale.LC_ALL, loc) is not installed in the system, so this is not directly an issue with my code.

I use locale.setlocale(locale.LC_ALL, loc) in the DateEntry so that the date in the entry field is displayed in the correct format but I might be able to avoid calling this line. I'll see what I can do.

Guilhermeasper commented 5 years ago

Thank you for your attention. So I said that everything worked fine, but the truth is that the names of the days of the week in the DateEntry actually stayed in the wrong language when I made that change. I've still tried to change other things, but I don't think I'll be able to put both calendars in my language and keep the date format of the DateEntry at the same time.

There is one thing I think is important to say, I realized that the problem is that after you set the locale in the DateEntry, this makes a global change in the program and ends up affecting the Calendar if it is being used in the same program, which is the my case. But as I said, I still have not found a way to keep both in the same language and the date format conforms to my language in Date Entry.

j4321 commented 5 years ago

Could you tell me if the code from branch fix-#15 fix your issues? If you want to use your system default language, just don't pass any locale and if you want to pass Brazilian Portuguese as locale, you will need to use locale='pt_BR' (instead of locale=pt-BR) because I changed the module I use to get the day and month names.

Guilhermeasper commented 5 years ago

It actually did not work out yet, but I found a way to make it work. I stopped passing the locale as you said, however the calendar was in English, so I set the locale in my own code(locale.setlocale(locale.LC_ALL, '')) before creating the calendar and everything is working fine, the languages and the date format.

I'm starting to think that the problem might be in how the locale is set in my language, because the default locale generated by python is 'Portuguese_Brazil.1252', but I can use 'pt_BR' and 'pt-BR' which also work(the language of the calendar stays in my language), however when I pass any of them as locale in the calendar, the program crashes.

j4321 commented 5 years ago

Locale names in Windows are really weird so that's why I want to use the module babel instead of locale. With the new module, locale names are easier to guess: _ ('pt_BR', ...) and there will no longer be any error locale.Error: unsupported locale setting.

Guilhermeasper commented 5 years ago

Look, I'm new here in github, so I'm still learning certain things, yesterday when you told me to do the test using the branch fix-#15, I don't know what I did, but I ended up downloading the code from the master, and I only realized today when I read your message that you used babel instead of locale. Anyway, I'm really sorry.

After I realized the error, using the correct code this time, I installed the babel module and then when I tested again, the program worked correctly without even needing to set the locale on the calendar as you said before. Thank you so much for your attention.