Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The calling thread must be STA #990

Closed
jz499 opened this issue Nov 20, 2019 · 5 comments
Closed

The calling thread must be STA #990

jz499 opened this issue Nov 20, 2019 · 5 comments

Comments

@jz499
Copy link

jz499 commented Nov 20, 2019

Environment

  • Pythonnet version: 2.4
  • Python version: 3.6.5
  • Operating System: Windows 10

Details

Hi Everyone;
I'm trying to run a basic wpf application from within Python using VS code. When the script executes I get the error listed at the bottom of this page. Has anyone seen this error before?

My Code:

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")

from System.Windows import (Application, SizeToContent, Thickness, Window)
from System.Windows.Controls import (Button, Label, StackPanel)
from System.Windows.Media.Effects import DropShadowBitmapEffect

window = Window()
window.Title = 'Welcome'
window.SizeToContent = SizeToContent.Height
window.Width = 450

stack = StackPanel()
stack.Margin = Thickness(15)
window.Content = stack

button = Button()
button.Content = 'Button1'
button.FontSize = 24
button.BitmapEffect = DropShadowBitmapEffect()

def onClick(sender, event):
message = Label()
message.FontSize = 36
message.Content = 'Welcome'
stack.Children.Add(message)

button.Click += onClick
stack.Children.Add(button)

app = Application()
app.Run(window)

Error Output:

File "c:/Users/WIN-VMMV970AOGL/pyfolder/test.py", line 10, in
window = Window()
System.InvalidOperationException: The calling thread must be STA, because many UI components require this.
at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at System.Windows.Window..ctor()

@lostmsu
Copy link
Member

lostmsu commented Nov 20, 2019

@lostmsu
Copy link
Member

lostmsu commented Nov 21, 2019

@jz499 I got more questions in email, but I don't see them in this thread. Have you been able to get it working? Should we close the topic?

@jz499
Copy link
Author

jz499 commented Nov 21, 2019

Hi @lostmsu , I got it partially working. I created an apartment thread in Python, the code executes with no errors, but it does not display the window or controls. Here's the threading I added to the above code.

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

@lostmsu
Copy link
Member

lostmsu commented Nov 21, 2019

@jz499 I think that happens, because Python can only execute 1 thread at time. E.g. while main Python interpreter is busy in thread.Join(), the thread is unable to run any of your Python code in window.

Probably you need to call BeginAllowThreads somehow.

@jz499
Copy link
Author

jz499 commented Nov 22, 2019

Hi @lostmsu,
Thanks, adding an apartment thread did resolve my problem. To make my code work I simply used a Xaml file rather than defining each control directly in python code.

stream = StreamReader("my_app.xaml")
window = XamlReader.Load(stream.BaseStream)
Application().Run(window)

@jz499 jz499 closed this as completed Nov 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants