IronPython
Apparence
Liens
Utilisation
Utilisation des assemblages .NET standard.
from System.Collections.Generic import List
# liste de string
maListe = List[str]()
maListe.Add("un")
maListe.Add("deux")
for element in maListe:
print(element)
|
Possibilité de faire des références vers d'autres assemblages .NET
import clr
# ajout d'une référence à l'assemblage System.Xml.dll
clr.AddReference('System.Xml')
# ajout d'une référence au fichier MonAssemblage.dll
clr.AddReferenceToFile("MonAssemblage.dll")
from System.Xml import * # import des espaces de nommage
print dir() # affiche l'ensemble des espaces de nommage chargé
|
Word
import clr
clr.AddReference('System')
clr.AddReference('Microsoft.Office.Interop.Word')
from System.Reflection import Missing
from Microsoft.Office.Interop.Word import ApplicationClass
missing = Missing.Value
# lancement de Word
word = ApplicationClass()
word.Visible = True
# création d'un nouveau document vierge
doc = word.Documents.Add(missing, missing, missing, missing)
file_path_to_insert = "chemin vers une archive zip"
class_type = "Package"
icon_file_name = r"C:\Windows\System32\zipfldr.dll"
icon_label = "texte à afficher sous l'icône"
oTrue = True
oFalse = False
start = 0
range_beginning_of_doc = doc.Range(start, missing)
# insertion de l'archive sous forme d'icône
range_beginning_of_doc.InlineShapes.AddOLEObject(
class_type,
file_path_to_insert, # chemin complet
oFalse,
oTrue,
icon_file_name, # chemin vers un fichier contenant une icône
missing,
icon_label,
missing)
# l'utilisation de la méthode avec tous ces arguments cause l'erreur :
# ValueError: Could not convert argument 0 for call to SaveAs.
doc.SaveAs("chemin complet .docx")
doc.Close(missing, missing, missing)
word.Quit(missing, missing, missing)
|
WinForm
Copier le script « winforms.py » dans le dossier « Lib ».
import winforms
from System.Windows.Forms import *
from System.Drawing import *
def click(button, arg): # event handler pour l'événement Click
MessageBox.Show("Hello")
b = Button()
b.Location = Point(150, 150)
b.Text = "Click Me !!!"
b.Click += click # abonnement d'un délégué à l'événement Click
f = Form()
f.Text = "I love Python"
f.Controls.Add(b) # ajout du bouton à la fenêtre
f.ShowDialog()
|
WPF
Copier le script « avalon.py » dans le dossier « Lib ».
from avalon import *
w = Window()
# il est possible de charger un fichier xaml
w.Content = LoadXaml("fichier.xaml")
w.ShowDialog()
|