From 52dcd3b28a0995b9a82a645455d1fa355899f447 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 14 Sep 2020 15:16:26 +0100 Subject: [PATCH] Use tkinter.filedialog on win32, because we now fix locale encoding tkinter ends up calling something where utf8 characters won't work because of the windows encoding, e.g. cp1252. We can't set encoding just for the dialogs, as it's not thread safe. So we'll just set it at startup instead. Utilising: locale.setlocale(locale.LC_ALL, '') to get things set up initially, so we can properly retrieve the language to go with the encoding on the subsequent setlocale() call. --- EDMarketConnector.py | 9 +++++++++ prefs.py | 31 +++++++------------------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index f2f8fbb4..4d25fe17 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -1046,6 +1046,15 @@ Locale LC_NUMERIC: {locale.getlocale(locale.LC_NUMERIC)} Locale LC_TIME: {locale.getlocale(locale.LC_TIME)}''' ) + # Change locale to a utf8 one + # First make sure the local is actually set as per locale's idea of defaults + locale.setlocale(locale.LC_ALL, '') + # Now find out the current locale, mostly the language + locale_startup = locale.getlocale(locale.LC_ALL) + # Now set that same language, but utf8 encoding (it was probably cp1252 + # or equivalent for other languages). + locale.setlocale(locale.LC_ALL, (locale_startup[0], 'utf8')) + # TODO: unittests in place of these # logger.debug('Test from __main__') # test_logging() diff --git a/prefs.py b/prefs.py index 6b2e824c..cdc0ab3c 100644 --- a/prefs.py +++ b/prefs.py @@ -498,30 +498,13 @@ class PreferencesDialog(tk.Toplevel): self.outdir_entry['state'] = local and 'readonly' or tk.DISABLED def filebrowse(self, title, pathvar): - if platform != 'win32': - import tkinter.filedialog - d = tkinter.filedialog.askdirectory(parent=self, initialdir=expanduser(pathvar.get()), title=title, mustexist=tk.TRUE) - else: - def browsecallback(hwnd, uMsg, lParam, lpData): - # set initial folder - if uMsg==BFFM_INITIALIZED and lpData: - ctypes.windll.user32.SendMessageW(hwnd, BFFM_SETSELECTION, 1, lpData); - return 0 - - browseInfo = BROWSEINFO() - browseInfo.lpszTitle = title - browseInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_USENEWUI - browseInfo.lpfn = BrowseCallbackProc(browsecallback) - browseInfo.lParam = pathvar.get().startswith('~') and join(config.home, pathvar.get()[2:]) or pathvar.get() - ctypes.windll.ole32.CoInitialize(None) - pidl = ctypes.windll.shell32.SHBrowseForFolderW(ctypes.byref(browseInfo)) - if pidl: - path = ctypes.create_unicode_buffer(MAX_PATH) - ctypes.windll.shell32.SHGetPathFromIDListW(pidl, path) - ctypes.windll.ole32.CoTaskMemFree(pidl) - d = path.value - else: - d = None + import tkinter.filedialog + d = tkinter.filedialog.askdirectory( + parent=self, + initialdir=expanduser(pathvar.get()), + title=title, + mustexist=tk.TRUE + ) if d: pathvar.set(d)