1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-14 08:17:13 +03:00

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.
This commit is contained in:
Athanasius 2020-09-14 15:16:26 +01:00
parent 44353b668e
commit 52dcd3b28a
2 changed files with 16 additions and 24 deletions

View File

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

View File

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