Note that there are some explanatory texts on larger screens.

plurals
  1. POControlling a C# Winforms GUI with IronPython
    primarykey
    data
    text
    <p>So I made a Winforms GUI in Visual Studio using C#, but for the project I am working on I want the majority of the code to be written in Python. I am hoping to have the "engine" written in python (for portability), and then have the application interface be something I can swap out.</p> <p>I made the C# project compile to a .dll, and was able to import the classes into an IronPython script and start the GUI fine. </p> <p>The problem is that running the GUI stops the execution of the Python script unless I put it into a separate thread. However, if I put the GUI into a separate thread and try and use the original python thread to change state information, I get an exception about modifying a control from a different thread than what created it.</p> <p>Is there any good way to communicate with the GUI thread or a way to accomplish what I am trying to do?</p> <p>The C# driver of the GUI:</p> <pre><code>public class Program { private static MainWindow window; [STAThread] static void Main() { Program.RunGUI(); } public static void RunGUI() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); window = new MainWindow(); Application.Run(window); } public static void SetState(GameState state) { window.State = state; } } </code></pre> <p>And the python script:</p> <pre><code>import clr clr.AddReferenceToFile("TG.Model.dll") clr.AddReferenceToFile("TG.UI.dll") from TG.Model import GameState from TG.UI import Program import thread import time def main(): print "Hello!" state = GameState() print state.CharacterName print dir(Program) thread.start_new_thread(Program.RunGUI, ()) #Program.RunGUI() time.sleep(2) Program.SetState(state) raw_input() if __name__ == "__main__": main() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload