Simple wx file dialog test
This commit is contained in:
86
tests/test_locale/file_dialog_test.py
Normal file
86
tests/test_locale/file_dialog_test.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import wx
|
||||
import locale_functions
|
||||
import sys
|
||||
|
||||
|
||||
class MyForm(wx.Frame):
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))
|
||||
|
||||
# Add a panel so it looks the correct on all platforms
|
||||
panel = wx.Panel(self, wx.ID_ANY)
|
||||
|
||||
SAVE_FILE_ID = wx.NewId()
|
||||
self.Bind(wx.EVT_MENU, self.saveFile, id=SAVE_FILE_ID)
|
||||
|
||||
LOAD_FILE_ID = wx.NewId()
|
||||
self.Bind(wx.EVT_MENU, self.loadFile, id=LOAD_FILE_ID)
|
||||
|
||||
accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('O'), LOAD_FILE_ID ),
|
||||
(wx.ACCEL_CTRL, ord('S'), SAVE_FILE_ID )]
|
||||
)
|
||||
self.SetAcceleratorTable(accel_tbl)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def loadFile(self, event):
|
||||
openFileDialog = wx.FileDialog(self, "Open", "", "",
|
||||
"Python files (*.py)|*.py",
|
||||
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
|
||||
openFileDialog.ShowModal()
|
||||
path = openFileDialog.GetPath()
|
||||
try:
|
||||
os_walk_without_codec = locale_functions.GetPath(path)
|
||||
except (UnicodeEncodeError, UnicodeTranslateError, UnicodeError, UnicodeDecodeError, UnicodeWarning, TypeError) as e:
|
||||
os_walk_without_codec = e
|
||||
|
||||
try:
|
||||
os_walk_with_system_codec = locale_functions.GetPath(path, None, sys.getdefaultencoding())
|
||||
except (UnicodeEncodeError, UnicodeTranslateError, UnicodeError, UnicodeDecodeError, UnicodeWarning, TypeError) as e:
|
||||
os_walk_with_system_codec = e
|
||||
|
||||
try:
|
||||
os_walk_unicode_without_codec = locale_functions.GetUnicodePath(path)
|
||||
except (UnicodeEncodeError, UnicodeTranslateError, UnicodeError, UnicodeDecodeError, UnicodeWarning, TypeError) as e:
|
||||
os_walk_unicode_without_codec = e
|
||||
|
||||
try:
|
||||
os_walk_unicode_with_system_codec = locale_functions.GetUnicodePath(path, None, sys.getdefaultencoding())
|
||||
except (UnicodeEncodeError, UnicodeTranslateError, UnicodeError, UnicodeDecodeError, UnicodeWarning, TypeError) as e:
|
||||
os_walk_unicode_with_system_codec = e
|
||||
|
||||
print("Simple print:")
|
||||
print(path)
|
||||
|
||||
print("Type:")
|
||||
print(type(path))
|
||||
|
||||
print("OS Walk: No Codec:")
|
||||
print(os_walk_without_codec)
|
||||
|
||||
print("OS Walk: Default System Codec:")
|
||||
print(os_walk_with_system_codec)
|
||||
|
||||
print("OS Unicode Walk: No Codec:")
|
||||
print(os_walk_unicode_without_codec)
|
||||
|
||||
print("OS Unicode Walk: Default System Codec:")
|
||||
print(os_walk_unicode_with_system_codec)
|
||||
openFileDialog.Destroy()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def saveFile(self, event):
|
||||
saveFileDialog = wx.FileDialog(self, "Save As", "", "",
|
||||
"Python files (*.py)|*.py",
|
||||
wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
|
||||
saveFileDialog.ShowModal()
|
||||
saveFileDialog.GetPath()
|
||||
saveFileDialog.Destroy()
|
||||
|
||||
# Run the program
|
||||
if __name__ == "__main__":
|
||||
app = wx.App(False)
|
||||
frame = MyForm()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
@@ -74,8 +74,28 @@ system_names = {
|
||||
}
|
||||
|
||||
|
||||
def GetPath(root, file, codec):
|
||||
def GetPath(root, file=None, codec=None):
|
||||
# Replace this with the function we actually use for this
|
||||
base_path = os.path.realpath(os.path.abspath(root))
|
||||
path = os.path.join(base_path, file).decode(codec)
|
||||
path = os.path.realpath(os.path.abspath(root))
|
||||
|
||||
if file:
|
||||
path = os.path.join(path, file)
|
||||
|
||||
if codec:
|
||||
path = path.decode(codec)
|
||||
|
||||
return path
|
||||
|
||||
def GetUnicodePath(root, file=None, codec=None):
|
||||
# Replace this with the function we actually use for this
|
||||
path = os.path.realpath(os.path.abspath(root))
|
||||
|
||||
if file:
|
||||
path = os.path.join(path, file)
|
||||
|
||||
if codec:
|
||||
path = unicode(path, codec)
|
||||
else:
|
||||
path = unicode(path)
|
||||
|
||||
return path
|
||||
|
||||
Reference in New Issue
Block a user