forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFrameEditor.py
More file actions
101 lines (79 loc) · 3.23 KB
/
SimpleFrameEditor.py
File metadata and controls
101 lines (79 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.stc
import os
if wx.VERSION_STRING >= '4.0':
import wx.adv
wx.FutureCall = wx.CallLater
wx.SAVE = wx.FD_SAVE
wx.OPEN = wx.FD_OPEN
wx.DEFAULT_STYLE = wx.FD_DEFAULT_STYLE
wx.MULTIPLE = wx.FD_MULTIPLE
wx.CHANGE_DIR = wx.FD_CHANGE_DIR
wx.OVERWRITE_PROMPT = wx.FD_OVERWRITE_PROMPT
class SimpleEditor(wx.stc.StyledTextCtrl):
def __init__ (self, parent, id = wx.NewIdRef(), \
pos = wx.DefaultPosition, \
size = wx.DefaultSize,\
style = 0,\
name = "editor"):
wx.stc.StyledTextCtrl.__init__ (self, parent, id, pos, size, style, name)
class FrameEditor(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.title = args[2]
self.flag = 0
# Menu Bar
self.frame_1_menubar = wx.MenuBar()
self.SetMenuBar(self.frame_1_menubar)
self.File = wx.Menu()
self.Save = wx.MenuItem(self.File, wx.NewIdRef(), "Save &As", "", wx.ITEM_NORMAL)
self.Save.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'save_as.png')))
self.File.Append(self.Save)
self.Open = wx.MenuItem(self.File, wx.NewIdRef(), "&Open", "", wx.ITEM_NORMAL)
self.Open.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH,'open.png')))
self.File.Append(self.Open)
self.frame_1_menubar.Append(self.File, "&File")
# Menu Bar end
self.frame_1_statusbar = self.CreateStatusBar(1, 0)
sizer = wx.BoxSizer(wx.VERTICAL)
self.editor = SimpleEditor(self)
sizer.Add(self.editor, 1, flag=wx.EXPAND)
self.Bind(wx.EVT_MENU, self.file_save, self.Save)
self.Bind(wx.EVT_MENU, self.open_file, self.Open)
self.SetSizer(sizer)
self.Layout()
def file_save(self, event): # wxGlade: MyFrame.<event_handler>
label = self.title.split(' ')[0]
if label in ('Rapport','logger'):
label = self.title.split(' ')[1]
dialog = wx.FileDialog( None, "Save %s file"%label, style = wx.SAVE, defaultFile="%s.dat"%label )
# Show the dialog and get user input
if dialog.ShowModal() == wx.ID_OK:
file_path = dialog.GetPath()
file = open(file_path,'w')
file_content = self.editor.GetValue()
file.write(file_content)
# Destroy the dialog
dialog.Destroy()
def open_file(self, event): # wxGlade: MyFrame.<event_handler>
filters = 'Text files (*.txt)|*.txt'
dialog = wx.FileDialog ( None, message = 'Open something....', wildcard = filters, style = wx.OPEN|wx.MULTIPLE )
if dialog.ShowModal() == wx.ID_OK:
filename = dialog.GetPath()
file = open(filename,'r')
file_content = file.read()
wx.CallAfter(self.editor.SetValue,file_content)
dialog.Destroy()
def AddText(self,txt):
self.editor.AddText(txt)
if __name__ == "__main__":
app = wx.App(0)
wx.InitAllImageHandlers()
frame_1 = FrameEditor(None, -1, "Test")
frame_1.AddText("Test \n zzeze")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()