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

Tony Roberts <[email protected]>
Newsgroups gmane.comp.python.dotnet
Message-ID <CA+XbcsaDfdU1kh0NBhHG=UPGFJtE9s5jOJBOTj1TJZ4297t3iQ@mail.gmail.com>
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 <
[email protected]> 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 - [email protected]
> https://mail.python.org/mailman/listinfo/pythondotnet
>

_________________________________________________
Python.NET mailing list - [email protected]
https://mail.python.org/mailman/listinfo/pythondotnet
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.