2016-04-07 34 views
0

Ich habe folgendes Beispiel für Code. Ich versuche OpenFileDialog in Iron Python, aber das Programm friert nur ein, anstatt das Dialogfenster zu öffnen.OpenFileDialog in IronPython

#!/usr/bin/ipy 

import clr 
clr.AddReference("System.Windows.Forms") 
clr.AddReference("System.Drawing") 

from System.Windows.Forms import Application, Form, TextBox 
from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog 
from System.Windows.Forms import DialogResult, ScrollBars, DockStyle 


class IForm(Form): 

    def __init__(self): 
     self.Text = "OpenDialog" 

     toolbar = ToolBar() 
     toolbar.Parent = self 
     openb = ToolBarButton() 


     self.textbox = TextBox() 
     self.textbox.Parent = self 
     self.textbox.Multiline = True 
     self.textbox.ScrollBars = ScrollBars.Both 
     self.textbox.WordWrap = False 
     self.textbox.Parent = self 
     self.textbox.Dock = DockStyle.Fill 


     toolbar.Buttons.Add(openb) 
     toolbar.ButtonClick += self.OnClicked 


     self.CenterToScreen() 

    def OnClicked(self, sender, event): 
     dialog = OpenFileDialog() 
     dialog.Filter = "C# files (*.cs)|*.cs" 

     if dialog.ShowDialog(self) == DialogResult.OK: 
      f = open(dialog.FileName) 
      data = f.read() 
      f.Close() 
      self.textbox.Text = data 


Application.Run(IForm()) 

Der Code ist von http://zetcode.com/tutorials/ironpythontutorial/dialogs/

Ich bin mit Ironpython 2.7.5

Was mache ich falsch? Und wie kann ich eigentlich OpenFileDialog und die Datei lesen?

Vielen Dank im Voraus)

Antwort

0

So anscheinend alle meine Probleme mit Ironpython waren, weil ich einige Python-Version auf meinem Computer installiert hatte. Ich löschte sie alle außer IronPython und fügte dann IronPython dem Pfad manuell hinzu. Danach funktioniert es einwandfrei ohne Abstürze.