Python .net and wpf: it's possible?
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
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@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@python.org https://mail.python.org/mailman/listinfo/pythondotnet
participants (2)
-
germano carella
-
Tony Roberts