2016-07-22 15 views
0

我寫了IronPython的GUI和我試圖創建一個文本文件,用戶可在文本框命名的文本文件。該文本文件正被用來給我一個矢量文件的Python腳本。任何建議試圖讓IronPython的創建,我可以存儲信息的

+0

歡迎使用StackOverflow!你試過什麼了 ? – Fabich

+0

謝謝。我一直在使用SafeFileDialog()和Spotfire中(http://stackoverflow.com/questions/32243732/output-spotfire-print-to-text-file)嘗試。 – Cian

回答

0

具有以下我能夠建立一個檔案,IronPython的

ScriptEngine m_engine = Python.CreateEngine(); 
ScriptScope m_scope = m_engine.CreateScope(); 
String code = @"file = open('myfile.txt', 'w+')"; 
ScriptSource source = m_engine.CreateScriptSourceFromString(code, SourceCodeKind.SingleStatement); 
source.Execute(m_scope); 

這裏的Python documentation有關w+。它打開文件進行寫入,首先截斷文件。

所以我猜你只需要插入腳本文件的名稱...

+0

謝謝。現在正在工作 – Cian

+0

您能否將問題標記爲已回答?謝謝! –

0

我把了,對我工作時,我在蟒蛇在寫代碼。希望能幫助到你。

def on_save_as(self, sender, e): 

    dlg = SaveFileDialog() 
    dlg.Filter = "Text File (*.txt)|*.txt" 
    dlg.Title = "SaveFile" 
    if dlg.ShowDialog(): 
     self.save_file(dlg.FileName) 

def save_file(self, filename): 
    sw = StreamWriter(filename) 
    try: 
     buf = self.textbox.Text 
     sw.Write(buf) 
     self.statusbar_item.Content = Path.GetFileName(filename) 
     self.openfile = filename 
    except Exception, ex: 
     MessageBox.Show(ex.Message) 
    finally: 
     sw.Close()