├── .gitignore
├── .nuget
├── nuget.config
├── nuget.exe
└── nuget.targets
├── AndroidSample
├── AndroidSample.csproj
├── Assets
│ ├── AboutAssets.txt
│ └── FlippingGameActivity.py
├── FlippingGameActivity.cs
├── Properties
│ └── AssemblyInfo.cs
└── Resources
│ ├── AboutResources.txt
│ ├── Drawable
│ └── Icon.png
│ ├── Layout
│ └── Main.axml
│ ├── Resource.Designer.cs
│ └── Values
│ └── Strings.xml
├── ConsoleGame.py
├── ConsoleSample
├── ConsoleSample.csproj
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── packages.config
├── FlippingGame
├── FlippingGame.py
├── FlippingGame.pyproj
└── __init__.py
├── IronPythonSamples.sln
├── PyGtkSample
└── PyGtkSample.py
├── PyWpfSample
├── PyWpfSample.py
├── PyWpfSample.pyproj
└── PyWpfSample.xaml
├── README
├── WinFormsSample
├── FlippingGame.Designer.cs
├── FlippingGame.cs
├── FlippingGame.resx
├── Program.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
├── Resources
│ └── ajax-loader.gif
├── WinFormsSample.csproj
└── packages.config
└── WpfSample
├── App.xaml
├── App.xaml.cs
├── Properties
├── AssemblyInfo.cs
├── Resources.Designer.cs
├── Resources.resx
├── Settings.Designer.cs
└── Settings.settings
├── WpfFlippingGame.xaml
├── WpfFlippingGame.xaml.cs
├── WpfSample.csproj
└── packages.config
/.gitignore:
--------------------------------------------------------------------------------
1 | *.suo
2 | bin
3 | obj
4 | packages
5 |
--------------------------------------------------------------------------------
/.nuget/nuget.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.nuget/nuget.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdhardy/IronPythonSamples/ccab7c4a12f37296cf18ef42ffea626facdeb4b9/.nuget/nuget.exe
--------------------------------------------------------------------------------
/.nuget/nuget.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
8 | $([System.IO.Path]::Combine($(ProjectDir), "packages.config"))
9 | $([System.IO.Path]::Combine($(SolutionDir), "packages"))
10 |
11 |
12 | $(SolutionDir).nuget
13 | packages.config
14 | $(SolutionDir)packages
15 |
16 |
17 | $(NuGetToolsPath)\nuget.exe
18 | "$(NuGetExePath)"
19 | mono --runtime=v4.0.30319 $(NuGetExePath)
20 |
21 | $(TargetDir.Trim('\\'))
22 |
23 |
24 | ""
25 |
26 |
27 | false
28 |
29 |
30 | false
31 |
32 |
33 | $(NuGetCommand) install "$(PackagesConfig)" -source $(PackageSources) -o "$(PackagesDir)"
34 | $(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols
35 |
36 |
37 |
38 | RestorePackages;
39 | $(BuildDependsOn);
40 |
41 |
42 |
43 |
44 | $(BuildDependsOn);
45 | BuildPackage;
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
57 |
58 |
61 |
62 |
63 |
64 |
66 |
67 |
70 |
71 |
--------------------------------------------------------------------------------
/AndroidSample/AndroidSample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.30703
7 | 2.0
8 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}
9 | {EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
10 | Library
11 | Properties
12 | AndroidSample
13 | AndroidSample
14 | 512
15 | true
16 | Resources\Resource.Designer.cs
17 | Off
18 |
19 |
20 | true
21 | full
22 | false
23 | bin\Debug\
24 | DEBUG;TRACE
25 | prompt
26 | 4
27 | True
28 | None
29 |
30 |
31 | pdbonly
32 | true
33 | bin\Release\
34 | TRACE
35 | prompt
36 | 4
37 | False
38 | SdkOnly
39 |
40 |
41 |
42 | bin\Debug\IronPython.dll
43 |
44 |
45 | bin\Debug\IronPython.Modules.dll
46 |
47 |
48 | bin\Debug\Microsoft.Dynamic.dll
49 |
50 |
51 | bin\Debug\Microsoft.Scripting.dll
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 | Assets\FlippingGame\FlippingGame.py
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
90 |
--------------------------------------------------------------------------------
/AndroidSample/Assets/AboutAssets.txt:
--------------------------------------------------------------------------------
1 | Any raw assets you want to be deployed with your application can be placed in
2 | this directory (and child directories) and given a Build Action of "AndroidAsset".
3 |
4 | These files will be deployed with you package and will be accessible using Android's
5 | AssetManager, like this:
6 |
7 | public class ReadAsset : Activity
8 | {
9 | protected override void OnCreate (Bundle bundle)
10 | {
11 | base.OnCreate (bundle);
12 |
13 | InputStream input = Assets.Open ("my_asset.txt");
14 | }
15 | }
16 |
17 | Additionally, some Android functions will automatically load asset files:
18 |
19 | Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
--------------------------------------------------------------------------------
/AndroidSample/Assets/FlippingGameActivity.py:
--------------------------------------------------------------------------------
1 | from AndroidSample import Resource
2 |
3 | class FlippingGameActivity(object):
4 | def __init__(self, main):
5 | self.main = main
6 | self.count = 1
7 |
8 | def OnCreate(self, bundle):
9 | # Set our view from the "main" layout resource
10 | self.main.SetContentView(Resource.Layout.Main);
11 |
12 | self.resultLabel = self.main.FindViewById(Resource.Id.resultLabel)
13 | self.resultLabel.Text = str(self.count)
14 |
15 | # Get our button from the layout resource,
16 | # and attach an event to it
17 | self.flipButton = self.main.FindViewById(Resource.Id.flipButton)
18 | self.flipButton.Click += lambda sender, e: self.button_Click(sender, e)
19 |
20 | def button_Click(self, sender, e):
21 | self.count += 1
22 | self.resultLabel.Text = str(self.count)
23 |
--------------------------------------------------------------------------------
/AndroidSample/FlippingGameActivity.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | using Android.App;
4 | using Android.Content;
5 | using Android.Runtime;
6 | using Android.Views;
7 | using Android.Widget;
8 | using Android.OS;
9 | using Microsoft.Scripting.Hosting;
10 | using IronPython.Hosting;
11 | using System.IO;
12 |
13 | namespace AndroidSample {
14 | [Activity(Label = "AndroidSample", MainLauncher = true, Icon = "@drawable/icon")]
15 | public class FlippingGameActivity : Activity {
16 | int count = 1;
17 |
18 | ScriptEngine engine = Python.CreateEngine();
19 | ScriptScope scope = null;
20 | object shadow;
21 |
22 | public FlippingGameActivity() {
23 | this.scope = this.engine.CreateScope();
24 | this.engine.Runtime.LoadAssembly(typeof(Resource).Assembly);
25 | }
26 |
27 | protected override void OnCreate(Bundle bundle) {
28 | base.OnCreate(bundle);
29 |
30 | InitShadow();
31 | this.engine.Operations.InvokeMember(this.shadow, "OnCreate", bundle);
32 | }
33 |
34 | private void InitShadow() {
35 | string code = null;
36 | using(var shadowStream = new StreamReader(this.Assets.Open("FlippingGameActivity.py"))) {
37 | code = shadowStream.ReadToEnd();
38 | }
39 |
40 | this.engine.Execute(code, this.scope);
41 |
42 | object shadowClass = this.scope.GetVariable("FlippingGameActivity");
43 | this.shadow = this.engine.Operations.CreateInstance(shadowClass, this);
44 | }
45 | }
46 | }
47 |
48 |
--------------------------------------------------------------------------------
/AndroidSample/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 | using Android.App;
5 |
6 | // General Information about an assembly is controlled through the following
7 | // set of attributes. Change these attribute values to modify the information
8 | // associated with an assembly.
9 | [assembly: AssemblyTitle("AndroidSample")]
10 | [assembly: AssemblyDescription("")]
11 | [assembly: AssemblyConfiguration("")]
12 | [assembly: AssemblyCompany("")]
13 | [assembly: AssemblyProduct("AndroidSample")]
14 | [assembly: AssemblyCopyright("Copyright © 2012")]
15 | [assembly: AssemblyTrademark("")]
16 | [assembly: AssemblyCulture("")]
17 |
18 | // Setting ComVisible to false makes the types in this assembly not visible
19 | // to COM components. If you need to access a type in this assembly from
20 | // COM, set the ComVisible attribute to true on that type.
21 | [assembly: ComVisible(false)]
22 |
23 | // The following GUID is for the ID of the typelib if this project is exposed to COM
24 | [assembly: Guid("a557ce8c-9dbe-4b93-8fc4-95ffc126cf14")]
25 |
26 | // Version information for an assembly consists of the following four values:
27 | //
28 | // Major Version
29 | // Minor Version
30 | // Build Number
31 | // Revision
32 | //
33 | // You can specify all the values or you can default the Build and Revision Numbers
34 | // by using the '*' as shown below:
35 | // [assembly: AssemblyVersion("1.0.*")]
36 | [assembly: AssemblyVersion("1.0.0.0")]
37 | [assembly: AssemblyFileVersion("1.0.0.0")]
38 |
39 | // Add some common permissions, these can be removed if not needed
40 | [assembly: UsesPermission(Android.Manifest.Permission.Internet)]
41 | [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
42 |
--------------------------------------------------------------------------------
/AndroidSample/Resources/AboutResources.txt:
--------------------------------------------------------------------------------
1 | Images, layout descriptions, binary blobs and string dictionaries can be included
2 | in your application as resource files. Various Android APIs are designed to
3 | operate on the resource IDs instead of dealing with images, strings or binary blobs
4 | directly.
5 |
6 | For example, a sample Android app that contains a user interface layout (Main.xml),
7 | an internationalization string table (Strings.xml) and some icons (drawable/Icon.png)
8 | would keep its resources in the "Resources" directory of the application:
9 |
10 | Resources/
11 | Drawable/
12 | Icon.png
13 |
14 | Layout/
15 | Main.axml
16 |
17 | Values/
18 | Strings.xml
19 |
20 | In order to get the build system to recognize Android resources, the build action should be set
21 | to "AndroidResource". The native Android APIs do not operate directly with filenames, but
22 | instead operate on resource IDs. When you compile an Android application that uses resources,
23 | the build system will package the resources for distribution and generate a class called
24 | "Resource" that contains the tokens for each one of the resources included. For example,
25 | for the above Resources layout, this is what the Resource class would expose:
26 |
27 | public class Resource {
28 | public class Drawable {
29 | public const int Icon = 0x123;
30 | }
31 |
32 | public class Layout {
33 | public const int Main = 0x456;
34 | }
35 |
36 | public class String {
37 | public const int FirstString = 0xabc;
38 | public const int SecondString = 0xbcd;
39 | }
40 | }
41 |
42 | You would then use Resource.Drawable.Icon to reference the Drawable/Icon.png file, or
43 | Resource.Layout.Main to reference the Layout/Main.axml file, or Resource.String.FirstString
44 | to reference the first string in the dictionary file Values/Strings.xml.
--------------------------------------------------------------------------------
/AndroidSample/Resources/Drawable/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdhardy/IronPythonSamples/ccab7c4a12f37296cf18ef42ffea626facdeb4b9/AndroidSample/Resources/Drawable/Icon.png
--------------------------------------------------------------------------------
/AndroidSample/Resources/Layout/Main.axml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
14 |
19 |
24 |
29 |
30 |
35 |
40 |
45 |
46 |
51 |
56 |
62 |
67 |
68 |
73 |
82 |
83 |
88 |
93 |
98 |
99 |
100 |
101 |
--------------------------------------------------------------------------------
/AndroidSample/Resources/Resource.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.269
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace AndroidSample
12 | {
13 |
14 |
15 | public partial class Resource
16 | {
17 |
18 | public partial class Attribute
19 | {
20 |
21 | private Attribute()
22 | {
23 | }
24 | }
25 |
26 | public partial class Drawable
27 | {
28 |
29 | // aapt resource value: 0x7f020000
30 | public const int Icon = 2130837504;
31 |
32 | private Drawable()
33 | {
34 | }
35 | }
36 |
37 | public partial class Id
38 | {
39 |
40 | // aapt resource value: 0x7f050004
41 | public const int bankrollLabel = 2131034116;
42 |
43 | // aapt resource value: 0x7f05000f
44 | public const int flipButton = 2131034127;
45 |
46 | // aapt resource value: 0x7f05000a
47 | public const int guessHeads = 2131034122;
48 |
49 | // aapt resource value: 0x7f05000b
50 | public const int guessTails = 2131034123;
51 |
52 | // aapt resource value: 0x7f050010
53 | public const int resetButton = 2131034128;
54 |
55 | // aapt resource value: 0x7f05000d
56 | public const int resultLabel = 2131034125;
57 |
58 | // aapt resource value: 0x7f050007
59 | public const int wagerBox = 2131034119;
60 |
61 | // aapt resource value: 0x7f050000
62 | public const int widget36 = 2131034112;
63 |
64 | // aapt resource value: 0x7f050001
65 | public const int widget37 = 2131034113;
66 |
67 | // aapt resource value: 0x7f050002
68 | public const int widget42 = 2131034114;
69 |
70 | // aapt resource value: 0x7f050005
71 | public const int widget43 = 2131034117;
72 |
73 | // aapt resource value: 0x7f050008
74 | public const int widget44 = 2131034120;
75 |
76 | // aapt resource value: 0x7f050003
77 | public const int widget45 = 2131034115;
78 |
79 | // aapt resource value: 0x7f050006
80 | public const int widget46 = 2131034118;
81 |
82 | // aapt resource value: 0x7f050009
83 | public const int widget47 = 2131034121;
84 |
85 | // aapt resource value: 0x7f05000c
86 | public const int widget53 = 2131034124;
87 |
88 | // aapt resource value: 0x7f05000e
89 | public const int widget55 = 2131034126;
90 |
91 | private Id()
92 | {
93 | }
94 | }
95 |
96 | public partial class Layout
97 | {
98 |
99 | // aapt resource value: 0x7f030000
100 | public const int Main = 2130903040;
101 |
102 | private Layout()
103 | {
104 | }
105 | }
106 |
107 | public partial class String
108 | {
109 |
110 | // aapt resource value: 0x7f040001
111 | public const int ApplicationName = 2130968577;
112 |
113 | // aapt resource value: 0x7f040000
114 | public const int Hello = 2130968576;
115 |
116 | private String()
117 | {
118 | }
119 | }
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/AndroidSample/Resources/Values/Strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World, Click Me!
4 | AndroidSample
5 |
6 |
--------------------------------------------------------------------------------
/ConsoleGame.py:
--------------------------------------------------------------------------------
1 | from FlippingGame import FlippingGame, GameError
2 | from System.Threading import Thread
3 |
4 | def greeting():
5 | print "Welcome to Coin Flipping"
6 | print "------------------------"
7 | print
8 |
9 | return True
10 |
11 | def get_wager(game):
12 | while True:
13 | wager = raw_input("Wager: ")
14 | try:
15 | w = int(wager)
16 | except ValueError:
17 | print "Wager must be a number (was '%s')." % wager
18 | else:
19 | return w
20 |
21 | def get_guess():
22 | while True:
23 | guess = raw_input("Guess [T/H]: ")
24 | guess = guess.upper()
25 | if len(guess) != 1 or guess not in "TH":
26 | print "Enter only T or H."
27 | else:
28 | return guess
29 |
30 | def spin():
31 | for i in "|\-/" * 4:
32 | print i, '\r',
33 | Thread.Sleep(60)
34 |
35 | print '\r'
36 |
37 | def turn(game):
38 | print "Bankroll:", game.bankroll
39 |
40 | while True:
41 | wager = get_wager(game)
42 | msg = game.check_wager(wager)
43 | if msg:
44 | print msg
45 | else:
46 | break
47 |
48 | guess = get_guess()
49 |
50 | result, toss = game.flip(guess, wager)
51 |
52 | spin()
53 |
54 | print "Toss:", toss
55 | if result:
56 | print "YOU WIN!"
57 | else:
58 | print "Sorry, better luck next time."
59 |
60 | print
61 |
62 | if game.bankroll == 0:
63 | return False
64 |
65 | again = raw_input("Play again? [Y/n]")
66 | print
67 | return len(again) == 0 or again[0] in "Yy"
68 |
69 | def main():
70 | playing = greeting()
71 |
72 | game = FlippingGame()
73 |
74 | while playing:
75 | playing = turn(game)
76 |
77 | print "Thanks for playing!"
78 |
79 | if __name__ == '__main__':
80 | main()
81 |
--------------------------------------------------------------------------------
/ConsoleSample/ConsoleSample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | 8.0.30703
7 | 2.0
8 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}
9 | Exe
10 | Properties
11 | ConsoleSample
12 | ConsoleSample
13 | v4.0
14 | Client
15 | 512
16 | ..\..\IronPythonSamples\
17 | true
18 |
19 |
20 | x86
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | x86
31 | pdbonly
32 | true
33 | bin\Release\
34 | TRACE
35 | prompt
36 | 4
37 |
38 |
39 |
40 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.dll
41 |
42 |
43 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.Modules.dll
44 |
45 |
46 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Dynamic.dll
47 |
48 |
49 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Scripting.dll
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | FlippingGame\FlippingGame.py
62 | PreserveNewest
63 |
64 |
65 | PreserveNewest
66 |
67 |
68 |
69 |
70 |
71 |
78 |
--------------------------------------------------------------------------------
/ConsoleSample/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using IronPython.Hosting;
6 | using System.Reflection;
7 | using System.IO;
8 |
9 | namespace ConsoleSample {
10 | class Program {
11 | static void Main(string[] args) {
12 | var engine = Python.CreateEngine();
13 | engine.SetSearchPaths(new[] { "FlippingGame" });
14 | var scope = engine.ExecuteFile("ConsoleGame.py");
15 | dynamic main = scope.GetVariable("main");
16 | main();
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/ConsoleSample/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("ConsoleSample")]
9 | [assembly: AssemblyDescription("IronPython Console Sample")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("ConsoleSample")]
13 | [assembly: AssemblyCopyright("Copyright © 2012")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8719f336-4e88-4837-8e8d-9cda07afe6a5")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/ConsoleSample/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/FlippingGame/FlippingGame.py:
--------------------------------------------------------------------------------
1 | from System import Random
2 |
3 | class GameError(Exception):
4 | pass
5 |
6 | class FlippingGame(object):
7 | def __init__(self, bankroll=500, history=[], seed=None):
8 | self.history = history
9 | self.bankroll = self.initial_bankroll = bankroll
10 | self.rand = Random(seed) if seed is not None else Random()
11 |
12 | def check_wager(self, wager):
13 | if wager < 1:
14 | return "You must wager at least one credit."
15 |
16 | if wager > self.bankroll:
17 | return "You cannot wager more than your bankroll."
18 |
19 | def flip(self, guess, wager):
20 | assert 1 <= wager <= self.bankroll
21 | assert guess in "TH"
22 |
23 | result = self._do_flip()
24 | self.history.append((result, guess, wager))
25 | if result == guess:
26 | self.bankroll += wager
27 | return True, result
28 | else:
29 | self.bankroll -= wager
30 | return False, result
31 |
32 | def _do_flip(self):
33 | return "TH"[self.rand.Next(2)]
34 |
--------------------------------------------------------------------------------
/FlippingGame/FlippingGame.pyproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | 2.0
6 | {2f477831-52b5-4184-b579-b5e3a0db2d66}
7 | .
8 |
9 |
10 |
11 |
12 | .
13 | .
14 | FlippingGame
15 | FlippingGame
16 |
17 |
18 | true
19 | false
20 |
21 |
22 | true
23 | false
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/FlippingGame/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdhardy/IronPythonSamples/ccab7c4a12f37296cf18ef42ffea626facdeb4b9/FlippingGame/__init__.py
--------------------------------------------------------------------------------
/IronPythonSamples.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{CD4B2C36-A95B-482A-A9DE-9B62E12B93FD}"
5 | ProjectSection(SolutionItems) = preProject
6 | .nuget\NuGet.exe = .nuget\NuGet.exe
7 | .nuget\NuGet.targets = .nuget\NuGet.targets
8 | EndProjectSection
9 | EndProject
10 | Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "FlippingGame", "FlippingGame\FlippingGame.pyproj", "{2F477831-52B5-4184-B579-B5E3A0DB2D66}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleSample", "ConsoleSample\ConsoleSample.csproj", "{40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}"
13 | EndProject
14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormsSample", "WinFormsSample\WinFormsSample.csproj", "{1940BB9C-9F27-417B-A227-2E69C0A8C172}"
15 | EndProject
16 | Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PyWpfSample", "PyWpfSample\PyWpfSample.pyproj", "{2F62608C-652C-406B-9775-4CE34E151D45}"
17 | EndProject
18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfSample", "WpfSample\WpfSample.csproj", "{0399791B-E075-4F41-AD68-4EA4A5602B00}"
19 | EndProject
20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9232B8F-B5CB-4F29-B713-19135C602D16}"
21 | ProjectSection(SolutionItems) = preProject
22 | README = README
23 | EndProjectSection
24 | EndProject
25 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndroidSample", "AndroidSample\AndroidSample.csproj", "{F97F76A2-D298-488B-93B0-C4F93B63ECA5}"
26 | EndProject
27 | Global
28 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
29 | Debug|Any CPU = Debug|Any CPU
30 | Debug|Mixed Platforms = Debug|Mixed Platforms
31 | Debug|x86 = Debug|x86
32 | Release|Any CPU = Release|Any CPU
33 | Release|Mixed Platforms = Release|Mixed Platforms
34 | Release|x86 = Release|x86
35 | EndGlobalSection
36 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
37 | {2F477831-52B5-4184-B579-B5E3A0DB2D66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38 | {2F477831-52B5-4184-B579-B5E3A0DB2D66}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
39 | {2F477831-52B5-4184-B579-B5E3A0DB2D66}.Debug|x86.ActiveCfg = Debug|Any CPU
40 | {2F477831-52B5-4184-B579-B5E3A0DB2D66}.Release|Any CPU.ActiveCfg = Release|Any CPU
41 | {2F477831-52B5-4184-B579-B5E3A0DB2D66}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
42 | {2F477831-52B5-4184-B579-B5E3A0DB2D66}.Release|x86.ActiveCfg = Release|Any CPU
43 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Debug|Any CPU.ActiveCfg = Debug|x86
44 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
45 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Debug|Mixed Platforms.Build.0 = Debug|x86
46 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Debug|x86.ActiveCfg = Debug|x86
47 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Debug|x86.Build.0 = Debug|x86
48 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Release|Any CPU.ActiveCfg = Release|x86
49 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Release|Mixed Platforms.ActiveCfg = Release|x86
50 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Release|Mixed Platforms.Build.0 = Release|x86
51 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Release|x86.ActiveCfg = Release|x86
52 | {40BC8BDE-81E0-48EC-9AC2-FB2A739E5144}.Release|x86.Build.0 = Release|x86
53 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Debug|Any CPU.ActiveCfg = Debug|x86
54 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
55 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Debug|Mixed Platforms.Build.0 = Debug|x86
56 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Debug|x86.ActiveCfg = Debug|x86
57 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Debug|x86.Build.0 = Debug|x86
58 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|Any CPU.ActiveCfg = Release|x86
59 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|Mixed Platforms.ActiveCfg = Release|x86
60 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|Mixed Platforms.Build.0 = Release|x86
61 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|x86.ActiveCfg = Release|x86
62 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|x86.Build.0 = Release|x86
63 | {2F62608C-652C-406B-9775-4CE34E151D45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64 | {2F62608C-652C-406B-9775-4CE34E151D45}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
65 | {2F62608C-652C-406B-9775-4CE34E151D45}.Debug|x86.ActiveCfg = Debug|Any CPU
66 | {2F62608C-652C-406B-9775-4CE34E151D45}.Release|Any CPU.ActiveCfg = Release|Any CPU
67 | {2F62608C-652C-406B-9775-4CE34E151D45}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
68 | {2F62608C-652C-406B-9775-4CE34E151D45}.Release|x86.ActiveCfg = Release|Any CPU
69 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Debug|Any CPU.ActiveCfg = Debug|x86
70 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
71 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Debug|Mixed Platforms.Build.0 = Debug|x86
72 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Debug|x86.ActiveCfg = Debug|x86
73 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Debug|x86.Build.0 = Debug|x86
74 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Release|Any CPU.ActiveCfg = Release|x86
75 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Release|Mixed Platforms.ActiveCfg = Release|x86
76 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Release|Mixed Platforms.Build.0 = Release|x86
77 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Release|x86.ActiveCfg = Release|x86
78 | {0399791B-E075-4F41-AD68-4EA4A5602B00}.Release|x86.Build.0 = Release|x86
79 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
80 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
81 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
82 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
83 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Debug|x86.ActiveCfg = Debug|Any CPU
84 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
85 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Release|Any CPU.Build.0 = Release|Any CPU
86 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
87 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Release|Mixed Platforms.Build.0 = Release|Any CPU
88 | {F97F76A2-D298-488B-93B0-C4F93B63ECA5}.Release|x86.ActiveCfg = Release|Any CPU
89 | EndGlobalSection
90 | GlobalSection(SolutionProperties) = preSolution
91 | HideSolutionNode = FALSE
92 | EndGlobalSection
93 | EndGlobal
94 |
--------------------------------------------------------------------------------
/PyGtkSample/PyGtkSample.py:
--------------------------------------------------------------------------------
1 | # IronPython sample using Gtk#
2 | # Released to Public Domain, 2012
3 | # Doug Blank
4 | # -------------------------------------------
5 | # To run on Linux:
6 | # mono --runtime=v4.0 ipy.exe PyGtkSample.py
7 |
8 | import clr
9 | clr.AddReference("gtk-sharp")
10 | clr.AddReference("gdk-sharp") # for colors
11 | import Gtk
12 | import Gdk # for colors
13 |
14 | from FlippingGame import FlippingGame
15 |
16 | class GtkSampleWindow(Gtk.Window):
17 | def __init__(self, title):
18 | self.game = FlippingGame.FlippingGame()
19 | self.table = Gtk.Table(7, 2, True)
20 | self.table.Attach( Gtk.Label("Bankroll:"), 0, 1, 0, 1)
21 | self.bankrollLabel = Gtk.Label("")
22 | self.table.Attach( self.bankrollLabel, 1, 2, 0, 1)
23 | self.table.Attach( Gtk.Label("Wager:"), 0, 1, 1, 2)
24 | self.wagerBox = Gtk.Entry()
25 | self.table.Attach( self.wagerBox, 1, 2, 1, 2)
26 | self.table.Attach( Gtk.Label("Guess:"), 0, 1, 2, 3)
27 | self.guessHeadsButton = Gtk.RadioButton("Heads")
28 | self.table.Attach(self.guessHeadsButton, 1, 2, 2, 3)
29 | self.guessTailsButton = Gtk.RadioButton(self.guessHeadsButton, "Tails")
30 | self.table.Attach(self.guessTailsButton, 1, 2, 3, 4)
31 | self.flipButton = Gtk.Button("Flip!")
32 | self.flipButton.Clicked += self.flipButton_Click
33 | self.table.Attach( self.flipButton, 0, 2, 4, 5)
34 | self.resultLabel = Gtk.Label("")
35 | self.table.Attach( self.resultLabel, 0, 2, 5, 6)
36 | self.errorLabel = Gtk.Label("")
37 | self.table.Attach( self.errorLabel, 0, 2, 6, 7)
38 | self.Add(self.table)
39 | self.table.ShowAll()
40 | self._showBankroll()
41 |
42 | def flipButton_Click(self, sender, e):
43 | wager = self._getWager()
44 | if not wager:
45 | return
46 |
47 | guess = "H" if self.guessHeadsButton.Active else "T"
48 | won, toss = self.game.flip(guess, wager)
49 |
50 | self._showToss(won, toss)
51 | self._showBankroll()
52 | self._maybeEndGame()
53 |
54 | def _getWager(self):
55 | try:
56 | wager = int(self.wagerBox.Text)
57 | except ValueError as v:
58 | self.wagerBox.ModifyBase(Gtk.StateType.Normal, Gdk.Color(255, 0, 0))
59 | self._showError("Wager must be a number.")
60 | return
61 | else:
62 | self._hideError()
63 | self.wagerBox.ModifyBase(Gtk.StateType.Normal, Gdk.Color(255, 255, 255))
64 |
65 | if wager < 1:
66 | self.wagerBox.ModifyBase(Gtk.StateType.Normal, Gdk.Color(255, 0, 0))
67 | self._showError("Wager must be at least 1 credit.")
68 | return
69 |
70 | if wager > self.game.bankroll:
71 | self.wagerBox.ModifyBase(Gtk.StateType.Normal, Gdk.Color(255, 0, 0))
72 | self._showError("Wager cannot be more than your bankroll.")
73 | return
74 |
75 | return wager
76 |
77 | def _showError(self, error):
78 | self.errorLabel.Text = error
79 | self.errorLabel.ModifyFg(Gtk.StateType.Normal, Gdk.Color(255, 0, 0))
80 | self.errorLabel.Visible = True
81 |
82 | def _hideError(self):
83 | self.errorLabel.Visible = False
84 |
85 | def _showToss(self, won, toss):
86 | self.resultLabel.Text = toss
87 | self.resultLabel.ModifyFg(Gtk.StateType.Normal,
88 | Gdk.Color(0, 255, 0) if won else Gdk.Color(255, 0, 0))
89 |
90 | def _showBankroll(self):
91 | self.bankrollLabel.Text = str(self.game.bankroll)
92 |
93 | def _maybeEndGame(self):
94 | if self.game.bankroll <= 0:
95 | self._showToss(False, 'X')
96 |
97 | self.flipButton.Sensitive = False
98 | self.wagerBox.Sensitive = False
99 | self.guessHeadsButton.Sensitive = False
100 | self.guessTailsButton.Sensitive = False
101 |
102 | def OnDeleteEvent(self, event):
103 | Gtk.Application.Quit()
104 |
105 | if __name__ == '__main__':
106 | Gtk.Application.Init()
107 | win = GtkSampleWindow("Coin Flipping")
108 | win.Show()
109 | Gtk.Application.Run()
110 |
--------------------------------------------------------------------------------
/PyWpfSample/PyWpfSample.py:
--------------------------------------------------------------------------------
1 | import wpf
2 |
3 | import FlippingGame
4 |
5 | from System.Windows import Application, Window, Visibility
6 | from System.Windows.Media import Brushes
7 |
8 | class WpfSampleWindow(Window):
9 | def __init__(self):
10 | wpf.LoadComponent(self, 'PyWpfSample.xaml')
11 | self.game = FlippingGame.FlippingGame()
12 |
13 | def flipButton_Click(self, sender, e):
14 | wager = self._getWager()
15 | if not wager:
16 | return
17 |
18 | guess = "H" if self.guessHeadsButton.IsChecked else "T"
19 | won, toss = self.game.flip(guess, wager)
20 |
21 | self._showToss(won, toss)
22 | self._showBankroll()
23 | self._maybeEndGame()
24 |
25 | def Window_Loaded(self, sender, e):
26 | self._showBankroll()
27 |
28 | def _getWager(self):
29 | try:
30 | wager = int(self.wagerBox.Text)
31 | except ValueError as v:
32 | self.wagerBox.Foreground = Brushes.Red
33 | self._showError("Wager must be a number.")
34 | return
35 | else:
36 | self._hideError()
37 | self.wagerBox.Foreground = Brushes.Black
38 |
39 | if wager < 1:
40 | self.wagerBox.Foreground = Brushes.Red
41 | self._showError("Wager must be at least 1 credit.")
42 | return
43 |
44 | if wager > self.game.bankroll:
45 | self.wagerBox.Foreground = Brushes.Red
46 | self._showError("Wager cannot be more than your bankroll.")
47 | return
48 |
49 | return wager
50 |
51 | def _showError(self, error):
52 | self.errorLabel.Content = error
53 | self.errorLabel.Visibility = Visibility.Visible
54 |
55 | def _hideError(self):
56 | self.errorLabel.Visibility = Visibility.Collapsed
57 |
58 | def _showToss(self, won, toss):
59 | self.resultLabel.Content = toss
60 | self.resultLabel.Foreground = Brushes.Green if won else Brushes.Red
61 |
62 | def _showBankroll(self):
63 | self.bankrollLabel.Content = str(self.game.bankroll)
64 |
65 | def _maybeEndGame(self):
66 | if self.game.bankroll <= 0:
67 | self._showToss(False, 'X')
68 |
69 | self.flipButton.IsEnabled = False
70 | self.wagerBox.IsEnabled = False
71 | self.guessHeadsButton.IsEnabled = False
72 | self.guessTailsButton.IsEnabled = False
73 |
74 | def wagerBox_GotFocus(self, sender, e):
75 | sender.Foreground = Brushes.Black
76 |
77 | if __name__ == '__main__':
78 | Application().Run(WpfSampleWindow())
79 |
--------------------------------------------------------------------------------
/PyWpfSample/PyWpfSample.pyproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | 2.0
6 | {2f62608c-652c-406b-9775-4ce34e151d45}
7 | .
8 | PyWpfSample.py
9 | ..\FlippingGame\
10 | .
11 | True
12 | dc002bdb-400d-4430-b1a8-b419d62bd6ef
13 | IronPython (.NET) launcher
14 | 2.7
15 | .
16 | PyWpfSample
17 | PyWpfSample
18 |
19 |
20 | False
21 | -X:Debug
22 |
23 |
24 | true
25 | false
26 |
27 |
28 | true
29 | false
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | FlippingGame\FlippingGame.py
41 |
42 |
43 |
44 |
45 | PresentationCore
46 | PresentationCore.dll
47 | ..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll
48 | True
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/PyWpfSample/PyWpfSample.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | ==================
2 | IronPython Samples
3 | ==================
4 |
5 | This is a collection of smaples that demonstrate how to use IronPython in various enviornments.
6 |
7 | All of them implement a very simple coin flipping game, the "business logic" of which is written in Python.
8 |
9 | FlippingGame
10 | ------------
11 | This is the core piece of Python code that implements the game logic and is used by all other samples.
12 |
13 | ConsoleSample
14 | -------------
15 | The console version of the game. There is a very small C# program that runs the game, which is written in Python.
16 | This is about the simplest possible form of host.
17 |
18 | WinFormsSample
19 | --------------
20 | A more complex sample that uses the dynamic features of C# to call into the Python game code, with the UI and handlers
21 | implemented entirely in C#. This is a more complex host that has to retrieve values from Python code.
22 |
23 | WpfSample
24 | ---------
25 | This is a WPF version of WinFormsSample, but it implements hosting in the same way.
26 |
27 | PyWpfSample
28 | -----------
29 | This is another WPF implementation of the game, but this time implemented entirely in Python. It uses the exact same XAML
30 | as WpfSample.
31 |
32 | There seems to be an issue running this smaple from PTVS, but running it from the command line (`ipyw PyWpfSample.py`) works.
33 |
--------------------------------------------------------------------------------
/WinFormsSample/FlippingGame.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace WinFormsSample {
2 | partial class FlippingGame {
3 | ///
4 | /// Required designer variable.
5 | ///
6 | private System.ComponentModel.IContainer components = null;
7 |
8 | ///
9 | /// Clean up any resources being used.
10 | ///
11 | /// true if managed resources should be disposed; otherwise, false.
12 | protected override void Dispose(bool disposing) {
13 | if (disposing && (components != null)) {
14 | components.Dispose();
15 | }
16 | base.Dispose(disposing);
17 | }
18 |
19 | #region Windows Form Designer generated code
20 |
21 | ///
22 | /// Required method for Designer support - do not modify
23 | /// the contents of this method with the code editor.
24 | ///
25 | private void InitializeComponent() {
26 | this.label1 = new System.Windows.Forms.Label();
27 | this.label2 = new System.Windows.Forms.Label();
28 | this.label4 = new System.Windows.Forms.Label();
29 | this.bankrollLabel = new System.Windows.Forms.Label();
30 | this.wagerBox = new System.Windows.Forms.NumericUpDown();
31 | this.guessHeadsRadio = new System.Windows.Forms.RadioButton();
32 | this.guessTailsRadio = new System.Windows.Forms.RadioButton();
33 | this.flipButton = new System.Windows.Forms.Button();
34 | this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
35 | this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
36 | this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel();
37 | this.resultLabel = new System.Windows.Forms.Label();
38 | this.flippingBox = new System.Windows.Forms.PictureBox();
39 | this.resultLayout = new System.Windows.Forms.TableLayoutPanel();
40 | ((System.ComponentModel.ISupportInitialize)(this.wagerBox)).BeginInit();
41 | this.tableLayoutPanel1.SuspendLayout();
42 | this.tableLayoutPanel2.SuspendLayout();
43 | this.tableLayoutPanel3.SuspendLayout();
44 | ((System.ComponentModel.ISupportInitialize)(this.flippingBox)).BeginInit();
45 | this.resultLayout.SuspendLayout();
46 | this.SuspendLayout();
47 | //
48 | // label1
49 | //
50 | this.label1.Anchor = System.Windows.Forms.AnchorStyles.Left;
51 | this.label1.AutoSize = true;
52 | this.label1.Location = new System.Drawing.Point(3, 25);
53 | this.label1.Name = "label1";
54 | this.label1.Size = new System.Drawing.Size(42, 13);
55 | this.label1.TabIndex = 0;
56 | this.label1.Text = "Wager:";
57 | //
58 | // label2
59 | //
60 | this.label2.Anchor = System.Windows.Forms.AnchorStyles.Left;
61 | this.label2.AutoSize = true;
62 | this.label2.Location = new System.Drawing.Point(3, 50);
63 | this.label2.Name = "label2";
64 | this.label2.Size = new System.Drawing.Size(40, 13);
65 | this.label2.TabIndex = 0;
66 | this.label2.Text = "Guess:";
67 | //
68 | // label4
69 | //
70 | this.label4.Anchor = System.Windows.Forms.AnchorStyles.Left;
71 | this.label4.AutoSize = true;
72 | this.label4.Location = new System.Drawing.Point(3, 3);
73 | this.label4.Name = "label4";
74 | this.label4.Size = new System.Drawing.Size(48, 13);
75 | this.label4.TabIndex = 4;
76 | this.label4.Text = "Bankroll:";
77 | //
78 | // bankrollLabel
79 | //
80 | this.bankrollLabel.Anchor = System.Windows.Forms.AnchorStyles.Left;
81 | this.bankrollLabel.AutoSize = true;
82 | this.tableLayoutPanel3.SetColumnSpan(this.bankrollLabel, 2);
83 | this.bankrollLabel.Location = new System.Drawing.Point(57, 3);
84 | this.bankrollLabel.Margin = new System.Windows.Forms.Padding(3);
85 | this.bankrollLabel.Name = "bankrollLabel";
86 | this.bankrollLabel.Size = new System.Drawing.Size(13, 13);
87 | this.bankrollLabel.TabIndex = 5;
88 | this.bankrollLabel.Text = "?";
89 | //
90 | // wagerBox
91 | //
92 | this.wagerBox.Anchor = System.Windows.Forms.AnchorStyles.Left;
93 | this.tableLayoutPanel3.SetColumnSpan(this.wagerBox, 2);
94 | this.wagerBox.Location = new System.Drawing.Point(57, 22);
95 | this.wagerBox.Minimum = new decimal(new int[] {
96 | 1,
97 | 0,
98 | 0,
99 | 0});
100 | this.wagerBox.Name = "wagerBox";
101 | this.wagerBox.Size = new System.Drawing.Size(75, 20);
102 | this.wagerBox.TabIndex = 6;
103 | this.wagerBox.Value = new decimal(new int[] {
104 | 1,
105 | 0,
106 | 0,
107 | 0});
108 | //
109 | // guessHeadsRadio
110 | //
111 | this.guessHeadsRadio.AutoSize = true;
112 | this.guessHeadsRadio.Checked = true;
113 | this.guessHeadsRadio.Location = new System.Drawing.Point(57, 48);
114 | this.guessHeadsRadio.Name = "guessHeadsRadio";
115 | this.guessHeadsRadio.Size = new System.Drawing.Size(56, 17);
116 | this.guessHeadsRadio.TabIndex = 7;
117 | this.guessHeadsRadio.TabStop = true;
118 | this.guessHeadsRadio.Text = "Heads";
119 | this.guessHeadsRadio.UseVisualStyleBackColor = true;
120 | //
121 | // guessTailsRadio
122 | //
123 | this.guessTailsRadio.AutoSize = true;
124 | this.guessTailsRadio.Location = new System.Drawing.Point(119, 48);
125 | this.guessTailsRadio.Name = "guessTailsRadio";
126 | this.guessTailsRadio.Size = new System.Drawing.Size(47, 17);
127 | this.guessTailsRadio.TabIndex = 7;
128 | this.guessTailsRadio.Text = "Tails";
129 | this.guessTailsRadio.UseVisualStyleBackColor = true;
130 | //
131 | // flipButton
132 | //
133 | this.flipButton.Anchor = System.Windows.Forms.AnchorStyles.None;
134 | this.flipButton.Location = new System.Drawing.Point(178, 25);
135 | this.flipButton.Name = "flipButton";
136 | this.flipButton.Size = new System.Drawing.Size(59, 23);
137 | this.flipButton.TabIndex = 2;
138 | this.flipButton.Text = "Flip!";
139 | this.flipButton.UseVisualStyleBackColor = true;
140 | this.flipButton.Click += new System.EventHandler(this.flipButton_Click);
141 | //
142 | // tableLayoutPanel1
143 | //
144 | this.tableLayoutPanel1.AutoSize = true;
145 | this.tableLayoutPanel1.ColumnCount = 1;
146 | this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
147 | this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 0);
148 | this.tableLayoutPanel1.Controls.Add(this.resultLayout, 0, 1);
149 | this.tableLayoutPanel1.Location = new System.Drawing.Point(13, 13);
150 | this.tableLayoutPanel1.Name = "tableLayoutPanel1";
151 | this.tableLayoutPanel1.RowCount = 2;
152 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
153 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
154 | this.tableLayoutPanel1.Size = new System.Drawing.Size(246, 133);
155 | this.tableLayoutPanel1.TabIndex = 8;
156 | //
157 | // tableLayoutPanel2
158 | //
159 | this.tableLayoutPanel2.AutoSize = true;
160 | this.tableLayoutPanel2.ColumnCount = 2;
161 | this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
162 | this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
163 | this.tableLayoutPanel2.Controls.Add(this.flipButton, 1, 0);
164 | this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 0, 0);
165 | this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 3);
166 | this.tableLayoutPanel2.Name = "tableLayoutPanel2";
167 | this.tableLayoutPanel2.RowCount = 1;
168 | this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle());
169 | this.tableLayoutPanel2.Size = new System.Drawing.Size(240, 74);
170 | this.tableLayoutPanel2.TabIndex = 0;
171 | //
172 | // tableLayoutPanel3
173 | //
174 | this.tableLayoutPanel3.AutoSize = true;
175 | this.tableLayoutPanel3.ColumnCount = 3;
176 | this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
177 | this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
178 | this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
179 | this.tableLayoutPanel3.Controls.Add(this.guessHeadsRadio, 1, 2);
180 | this.tableLayoutPanel3.Controls.Add(this.label1, 0, 1);
181 | this.tableLayoutPanel3.Controls.Add(this.label4, 0, 0);
182 | this.tableLayoutPanel3.Controls.Add(this.guessTailsRadio, 2, 2);
183 | this.tableLayoutPanel3.Controls.Add(this.bankrollLabel, 1, 0);
184 | this.tableLayoutPanel3.Controls.Add(this.wagerBox, 1, 1);
185 | this.tableLayoutPanel3.Controls.Add(this.label2, 0, 2);
186 | this.tableLayoutPanel3.Location = new System.Drawing.Point(3, 3);
187 | this.tableLayoutPanel3.Name = "tableLayoutPanel3";
188 | this.tableLayoutPanel3.RowCount = 3;
189 | this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle());
190 | this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle());
191 | this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle());
192 | this.tableLayoutPanel3.Size = new System.Drawing.Size(169, 68);
193 | this.tableLayoutPanel3.TabIndex = 3;
194 | //
195 | // resultLabel
196 | //
197 | this.resultLabel.Anchor = System.Windows.Forms.AnchorStyles.None;
198 | this.resultLabel.AutoSize = true;
199 | this.resultLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
200 | this.resultLabel.Location = new System.Drawing.Point(41, 5);
201 | this.resultLabel.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5);
202 | this.resultLabel.Name = "resultLabel";
203 | this.resultLabel.Size = new System.Drawing.Size(35, 37);
204 | this.resultLabel.TabIndex = 1;
205 | this.resultLabel.Text = "?";
206 | //
207 | // flippingBox
208 | //
209 | this.flippingBox.Anchor = System.Windows.Forms.AnchorStyles.None;
210 | this.flippingBox.Image = global::WinFormsSample.Properties.Resources.ajax_loader;
211 | this.flippingBox.Location = new System.Drawing.Point(3, 7);
212 | this.flippingBox.Name = "flippingBox";
213 | this.flippingBox.Size = new System.Drawing.Size(32, 32);
214 | this.flippingBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
215 | this.flippingBox.TabIndex = 9;
216 | this.flippingBox.TabStop = false;
217 | //
218 | // resultLayout
219 | //
220 | this.resultLayout.Anchor = System.Windows.Forms.AnchorStyles.None;
221 | this.resultLayout.AutoSize = true;
222 | this.resultLayout.ColumnCount = 2;
223 | this.resultLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
224 | this.resultLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
225 | this.resultLayout.Controls.Add(this.flippingBox, 0, 0);
226 | this.resultLayout.Controls.Add(this.resultLabel, 1, 0);
227 | this.resultLayout.Location = new System.Drawing.Point(83, 83);
228 | this.resultLayout.Name = "resultLayout";
229 | this.resultLayout.RowCount = 1;
230 | this.resultLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
231 | this.resultLayout.Size = new System.Drawing.Size(79, 47);
232 | this.resultLayout.TabIndex = 1;
233 | //
234 | // FlippingGame
235 | //
236 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
237 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
238 | this.AutoSize = true;
239 | this.ClientSize = new System.Drawing.Size(288, 156);
240 | this.Controls.Add(this.tableLayoutPanel1);
241 | this.Name = "FlippingGame";
242 | this.Text = "Coin Flipping Game";
243 | this.Load += new System.EventHandler(this.FlippingGame_Load);
244 | ((System.ComponentModel.ISupportInitialize)(this.wagerBox)).EndInit();
245 | this.tableLayoutPanel1.ResumeLayout(false);
246 | this.tableLayoutPanel1.PerformLayout();
247 | this.tableLayoutPanel2.ResumeLayout(false);
248 | this.tableLayoutPanel2.PerformLayout();
249 | this.tableLayoutPanel3.ResumeLayout(false);
250 | this.tableLayoutPanel3.PerformLayout();
251 | ((System.ComponentModel.ISupportInitialize)(this.flippingBox)).EndInit();
252 | this.resultLayout.ResumeLayout(false);
253 | this.resultLayout.PerformLayout();
254 | this.ResumeLayout(false);
255 | this.PerformLayout();
256 |
257 | }
258 |
259 | #endregion
260 |
261 | private System.Windows.Forms.Label label1;
262 | private System.Windows.Forms.Label label2;
263 | private System.Windows.Forms.Label label4;
264 | private System.Windows.Forms.Label bankrollLabel;
265 | private System.Windows.Forms.NumericUpDown wagerBox;
266 | private System.Windows.Forms.RadioButton guessHeadsRadio;
267 | private System.Windows.Forms.RadioButton guessTailsRadio;
268 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3;
269 | private System.Windows.Forms.Button flipButton;
270 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
271 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
272 | private System.Windows.Forms.Label resultLabel;
273 | private System.Windows.Forms.PictureBox flippingBox;
274 | private System.Windows.Forms.TableLayoutPanel resultLayout;
275 | }
276 | }
277 |
278 |
--------------------------------------------------------------------------------
/WinFormsSample/FlippingGame.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Drawing;
3 | using System.Windows.Forms;
4 | using IronPython.Hosting;
5 | using Microsoft.Scripting.Hosting;
6 |
7 | namespace WinFormsSample {
8 | public partial class FlippingGame : Form {
9 | ScriptEngine engine;
10 | ScriptScope scope;
11 | dynamic game;
12 | Random rand = new Random();
13 |
14 | public FlippingGame() {
15 | InitializeComponent();
16 |
17 | this.engine = Python.CreateEngine();
18 | this.engine.SetSearchPaths(new[] { "FlippingGame" });
19 | this.scope = this.engine.CreateScope();
20 | }
21 |
22 | private void FlippingGame_Load(object sender, EventArgs e) {
23 | this.scope.ImportModule("FlippingGame");
24 | this.engine.Execute("game = FlippingGame.FlippingGame()", this.scope);
25 | this.game = this.scope.GetVariable("game");
26 |
27 | ConstrainWager();
28 | ShowBankroll();
29 |
30 | ShowResult();
31 | }
32 |
33 | private void ShowBankroll() {
34 | bankrollLabel.Text = game.bankroll.ToString();
35 | }
36 |
37 | private void flipButton_Click(object sender, EventArgs e) {
38 | var wager = wagerBox.Value;
39 | var guess = guessHeadsRadio.Checked ? "H" : "T";
40 |
41 | var result = game.flip(guess, wager);
42 | var won = result[0];
43 | var toss = result[1];
44 |
45 | ShowSpinner();
46 |
47 | var timer = new Timer() {Interval = rand.Next(350, 1000)};
48 | timer.Tick += (t_sender, t_e) => {
49 | flippingBox.Visible = false;
50 | SetToss(toss.ToString(), (bool)won);
51 | ShowResult();
52 |
53 | ConstrainWager();
54 | ShowBankroll();
55 |
56 | ((Timer)t_sender).Stop();
57 | };
58 |
59 | timer.Start();
60 | }
61 |
62 | private void ShowResult() {
63 | resultLabel.Visible = true;
64 | flippingBox.Visible = false;
65 | }
66 |
67 | private void ShowSpinner() {
68 | resultLabel.Visible = false;
69 | flippingBox.Visible = true;
70 | }
71 |
72 | private void SetToss(string toss, bool won) {
73 | resultLabel.Visible = true;
74 | resultLabel.Text = toss;
75 | resultLabel.ForeColor = won ? Color.Green : Color.Red;
76 | }
77 |
78 | private void ConstrainWager() {
79 | var bankroll = game.bankroll;
80 |
81 | if (bankroll > 0) {
82 | wagerBox.Maximum = bankroll;
83 | } else {
84 | wagerBox.Enabled = false;
85 | flipButton.Enabled = false;
86 | guessHeadsRadio.Enabled = guessTailsRadio.Enabled = false;
87 | resultLabel.Text = "X";
88 | ShowResult();
89 | }
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/WinFormsSample/FlippingGame.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
--------------------------------------------------------------------------------
/WinFormsSample/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Windows.Forms;
5 |
6 | namespace WinFormsSample {
7 | static class Program {
8 | ///
9 | /// The main entry point for the application.
10 | ///
11 | [STAThread]
12 | static void Main() {
13 | Application.EnableVisualStyles();
14 | Application.SetCompatibleTextRenderingDefault(false);
15 | Application.Run(new FlippingGame());
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/WinFormsSample/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("WinFormsSample")]
9 | [assembly: AssemblyDescription("IronPython WinForms Sample")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("WinFormsSample")]
13 | [assembly: AssemblyCopyright("Copyright © 2012")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("1ff8ae58-12a7-49c1-a02d-7a6843193371")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/WinFormsSample/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.261
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WinFormsSample.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// Returns the cached ResourceManager instance used by this class.
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WinFormsSample.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// Overrides the current thread's CurrentUICulture property for all
51 | /// resource lookups using this strongly typed resource class.
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 |
63 | internal static System.Drawing.Bitmap ajax_loader {
64 | get {
65 | object obj = ResourceManager.GetObject("ajax_loader", resourceCulture);
66 | return ((System.Drawing.Bitmap)(obj));
67 | }
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/WinFormsSample/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
121 |
122 | ..\Resources\ajax-loader.gif;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
123 |
124 |
--------------------------------------------------------------------------------
/WinFormsSample/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.261
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WinFormsSample.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/WinFormsSample/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/WinFormsSample/Resources/ajax-loader.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdhardy/IronPythonSamples/ccab7c4a12f37296cf18ef42ffea626facdeb4b9/WinFormsSample/Resources/ajax-loader.gif
--------------------------------------------------------------------------------
/WinFormsSample/WinFormsSample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | 8.0.30703
7 | 2.0
8 | {1940BB9C-9F27-417B-A227-2E69C0A8C172}
9 | WinExe
10 | Properties
11 | WinFormsSample
12 | WinFormsSample
13 | v4.0
14 | Client
15 | 512
16 | ..\..\IronPythonSamples\
17 | true
18 |
19 |
20 | x86
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | x86
31 | pdbonly
32 | true
33 | bin\Release\
34 | TRACE
35 | prompt
36 | 4
37 |
38 |
39 |
40 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.dll
41 |
42 |
43 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.Modules.dll
44 |
45 |
46 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.SQLite.dll
47 |
48 |
49 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.Wpf.dll
50 |
51 |
52 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Dynamic.dll
53 |
54 |
55 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Scripting.dll
56 |
57 |
58 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Scripting.AspNet.dll
59 |
60 |
61 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Scripting.Metadata.dll
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | Form
77 |
78 |
79 | FlippingGame.cs
80 |
81 |
82 |
83 |
84 | FlippingGame.cs
85 |
86 |
87 | ResXFileCodeGenerator
88 | Resources.Designer.cs
89 | Designer
90 |
91 |
92 | True
93 | Resources.resx
94 | True
95 |
96 |
97 | FlippingGame\FlippingGame.py
98 | PreserveNewest
99 |
100 |
101 |
102 | SettingsSingleFileGenerator
103 | Settings.Designer.cs
104 |
105 |
106 | True
107 | Settings.settings
108 | True
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
126 |
--------------------------------------------------------------------------------
/WinFormsSample/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/WpfSample/App.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/WpfSample/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Windows;
7 |
8 | namespace WpfSample {
9 | ///
10 | /// Interaction logic for App.xaml
11 | ///
12 | public partial class App : Application {
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/WpfSample/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // General Information about an assembly is controlled through the following
8 | // set of attributes. Change these attribute values to modify the information
9 | // associated with an assembly.
10 | [assembly: AssemblyTitle("WpfSample")]
11 | [assembly: AssemblyDescription("IronPython WPF Sample")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("WpfSample")]
15 | [assembly: AssemblyCopyright("Copyright © 2012")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // Setting ComVisible to false makes the types in this assembly not visible
20 | // to COM components. If you need to access a type in this assembly from
21 | // COM, set the ComVisible attribute to true on that type.
22 | [assembly: ComVisible(false)]
23 |
24 | //In order to begin building localizable applications, set
25 | //CultureYouAreCodingWith in your .csproj file
26 | //inside a . For example, if you are using US english
27 | //in your source files, set the to en-US. Then uncomment
28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
29 | //the line below to match the UICulture setting in the project file.
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36 | //(used if a resource is not found in the page,
37 | // or application resource dictionaries)
38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39 | //(used if a resource is not found in the page,
40 | // app, or any theme specific resource dictionaries)
41 | )]
42 |
43 |
44 | // Version information for an assembly consists of the following four values:
45 | //
46 | // Major Version
47 | // Minor Version
48 | // Build Number
49 | // Revision
50 | //
51 | // You can specify all the values or you can default the Build and Revision Numbers
52 | // by using the '*' as shown below:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/WpfSample/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.261
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WpfSample.Properties {
12 |
13 |
14 | ///
15 | /// A strongly-typed resource class, for looking up localized strings, etc.
16 | ///
17 | // This class was auto-generated by the StronglyTypedResourceBuilder
18 | // class via a tool like ResGen or Visual Studio.
19 | // To add or remove a member, edit your .ResX file then rerun ResGen
20 | // with the /str option, or rebuild your VS project.
21 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
22 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
23 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
24 | internal class Resources {
25 |
26 | private static global::System.Resources.ResourceManager resourceMan;
27 |
28 | private static global::System.Globalization.CultureInfo resourceCulture;
29 |
30 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
31 | internal Resources() {
32 | }
33 |
34 | ///
35 | /// Returns the cached ResourceManager instance used by this class.
36 | ///
37 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
38 | internal static global::System.Resources.ResourceManager ResourceManager {
39 | get {
40 | if ((resourceMan == null)) {
41 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WpfSample.Properties.Resources", typeof(Resources).Assembly);
42 | resourceMan = temp;
43 | }
44 | return resourceMan;
45 | }
46 | }
47 |
48 | ///
49 | /// Overrides the current thread's CurrentUICulture property for all
50 | /// resource lookups using this strongly typed resource class.
51 | ///
52 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
53 | internal static global::System.Globalization.CultureInfo Culture {
54 | get {
55 | return resourceCulture;
56 | }
57 | set {
58 | resourceCulture = value;
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/WpfSample/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/WpfSample/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.261
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WpfSample.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/WpfSample/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/WpfSample/WpfFlippingGame.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/WpfSample/WpfFlippingGame.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.Windows.Media;
3 | using IronPython.Hosting;
4 | using Microsoft.Scripting.Hosting;
5 |
6 | namespace WpfSample {
7 | ///
8 | /// Interaction logic for MainWindow.xaml
9 | ///
10 | public partial class WpfFlippingGame : Window {
11 | ScriptEngine engine;
12 | ScriptScope scope;
13 | dynamic game;
14 |
15 | public WpfFlippingGame() {
16 | InitializeComponent();
17 |
18 | this.engine = Python.CreateEngine();
19 | this.engine.SetSearchPaths(new[] { "FlippingGame" });
20 | this.scope = this.engine.CreateScope();
21 | }
22 |
23 | private void Window_Loaded(object sender, RoutedEventArgs e) {
24 | this.scope.ImportModule("FlippingGame");
25 | this.engine.Execute("game = FlippingGame.FlippingGame()", this.scope);
26 | this.game = this.scope.GetVariable("game");
27 |
28 | ConstrainWager();
29 | ShowBankroll();
30 |
31 | ShowResult();
32 | }
33 |
34 | private void flipButton_Click(object sender, RoutedEventArgs e) {
35 | var wager = GetWager();
36 | if (wager <= 0)
37 | return;
38 |
39 | var guess = guessHeadsButton.IsChecked.Value ? "H" : "T";
40 |
41 | var result = game.flip(guess, wager);
42 | var won = result[0];
43 | var toss = result[1];
44 |
45 | SetToss(toss.ToString(), (bool)won);
46 | ShowResult();
47 |
48 | ConstrainWager();
49 | ShowBankroll();
50 |
51 | //ShowSpinner();
52 |
53 | //var timer = new Timer() { Interval = rand.Next(350, 1000) };
54 | //timer.Tick += (t_sender, t_e) => {
55 | // flippingBox.Visible = false;
56 | // SetToss(toss.ToString(), (bool)won);
57 | // ShowResult();
58 |
59 | // ConstrainWager();
60 | // ShowBankroll();
61 |
62 | // ((Timer)t_sender).Stop();
63 | //};
64 |
65 | //timer.Start();
66 | }
67 |
68 | private void wagerBox_GotFocus(object sender, RoutedEventArgs e) {
69 | wagerBox.Foreground = Brushes.Black;
70 | }
71 |
72 | private int GetWager() {
73 | int wager;
74 | if (!int.TryParse(wagerBox.Text, out wager)) {
75 | wagerBox.Foreground = Brushes.Red;
76 | ShowError("Wager must be a number.");
77 |
78 | return -1;
79 | }
80 |
81 | if (wager < 1) {
82 | wagerBox.Foreground = Brushes.Red;
83 | ShowError("Wager must be at least one credit.");
84 |
85 | return -1;
86 | }
87 |
88 | if (wager > game.bankroll) {
89 | wagerBox.Foreground = Brushes.Red;
90 | ShowError("Wager cannot be more than your bankroll.");
91 |
92 | return -1;
93 | }
94 |
95 | HideError();
96 | return wager;
97 | }
98 |
99 | private void ShowError(string error) {
100 | errorLabel.Content = error;
101 | errorLabel.Visibility = System.Windows.Visibility.Visible;
102 | }
103 |
104 | private void HideError() {
105 | errorLabel.Visibility = System.Windows.Visibility.Collapsed;
106 | }
107 |
108 | private void ShowBankroll() {
109 | bankrollLabel.Content = game.bankroll.ToString();
110 | }
111 |
112 | private void ShowResult() {
113 | //resultLabel.Visible = true;
114 | //flippingBox.Visible = false;
115 | }
116 |
117 | private void ShowSpinner() {
118 | //resultLabel.Visible = false;
119 | //flippingBox.Visible = true;
120 | }
121 |
122 | private void SetToss(string toss, bool won) {
123 | //resultLabel.Visibility = Visibility.Visible;
124 | resultLabel.Content = toss;
125 | resultLabel.Foreground = won ? Brushes.Green : Brushes.Red;
126 | }
127 |
128 | private void ConstrainWager() {
129 | var bankroll = game.bankroll;
130 |
131 | if (bankroll <= 0) {
132 | wagerBox.IsEnabled = false;
133 | flipButton.IsEnabled = false;
134 | guessHeadsButton.IsEnabled = guessTailsButton.IsEnabled = false;
135 |
136 | SetToss("X", false);
137 | ShowResult();
138 | }
139 | }
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/WpfSample/WpfSample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | 8.0.30703
7 | 2.0
8 | {0399791B-E075-4F41-AD68-4EA4A5602B00}
9 | WinExe
10 | Properties
11 | WpfSample
12 | WpfSample
13 | v4.0
14 | Client
15 | 512
16 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
17 | 4
18 | ..\..\IronPythonSamples\
19 | true
20 |
21 |
22 | x86
23 | true
24 | full
25 | false
26 | bin\Debug\
27 | DEBUG;TRACE
28 | prompt
29 | 4
30 |
31 |
32 | x86
33 | pdbonly
34 | true
35 | bin\Release\
36 | TRACE
37 | prompt
38 | 4
39 |
40 |
41 |
42 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.dll
43 |
44 |
45 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.Modules.dll
46 |
47 |
48 | ..\packages\IronPython.2.7.2-final1\lib\Net40\IronPython.Wpf.dll
49 |
50 |
51 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Dynamic.dll
52 |
53 |
54 | ..\packages\IronPython.2.7.2-final1\lib\Net40\Microsoft.Scripting.dll
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | 4.0
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | MSBuild:Compile
73 | Designer
74 |
75 |
76 | MSBuild:Compile
77 | Designer
78 |
79 |
80 | App.xaml
81 | Code
82 |
83 |
84 | WpfFlippingGame.xaml
85 | Code
86 |
87 |
88 |
89 |
90 | Code
91 |
92 |
93 | True
94 | True
95 | Resources.resx
96 |
97 |
98 | True
99 | Settings.settings
100 | True
101 |
102 |
103 | ResXFileCodeGenerator
104 | Resources.Designer.cs
105 |
106 |
107 | FlippingGame\FlippingGame.py
108 | PreserveNewest
109 |
110 |
111 |
112 | SettingsSingleFileGenerator
113 | Settings.Designer.cs
114 |
115 |
116 |
117 |
118 |
119 |
126 |
--------------------------------------------------------------------------------
/WpfSample/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------