[Python.NET] Python .net and wpf: it's possible?

Tony Roberts tony at pyxll.com
Mon Jun 22 11:43:07 CEST 2015


Hi Germano,

It helps to look at the full exception and stack trace, not just the
exception type. In this case you probably got this error
"System.InvalidOperationException: The calling thread must be STA, because
many UI components require this." which tells you that the main thread
you're calling Application.Run from is not an STA thread.

The following code shows how to create and show a WPF Window.

Regards,
Tony

// window.xaml

<Window

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Title="Test Window"
    Height="300"
    Width="300" />


import clr
clr.AddReference(r"wpf\PresentationFramework")

from System.IO import StreamReader
from System.Windows.Markup import XamlReader
from System.Windows import Application, Window
from System.Threading import Thread, ThreadStart, ApartmentState


def app_thread():
    stream = StreamReader("window.xaml")
    window = XamlReader.Load(stream.BaseStream)
    Application().Run(window)

thread = Thread(ThreadStart(app_thread))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()





On Sat, Jun 20, 2015 at 2:34 PM germano carella <
germanocarella.list at gmail.com> wrote:

> Hi,
> I'm Germano From Italy.
>
> I'm trying to write a wpf application using python for .net.
> import clr
> clr.AddReference("wpf\PresentationFramework.Classic")
> clr.AddReference("wpf\PresentationCore")
> from System.Windows import Window,Application
>
> class MyWindow(Window):
>   def __init__(self):
>    Window.__init__(self)
>    self.LoadComponent("page.xaml")
>
> Application.Run(MyWindow())
>
> InvalidOperationException!
>
> There is something I heve to do?
> What is wrong?
> Thanks!
> Germano
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> https://mail.python.org/mailman/listinfo/pythondotnet
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20150622/3dd49060/attachment.html>


More information about the PythonDotNet mailing list