Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add pure-Python WPF sample.

  • Loading branch information...
commit 28b8b20889549327c23d752766455e99556dee3d 1 parent 04e5a22
Jeff Hardy authored
8 IronPythonSamples.sln
View
@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleSample", "ConsoleSam
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormsSample", "WinFormsSample\WinFormsSample.csproj", "{1940BB9C-9F27-417B-A227-2E69C0A8C172}"
EndProject
+Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PyWpfSample", "PyWpfSample\PyWpfSample.pyproj", "{2F62608C-652C-406B-9775-4CE34E151D45}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -49,6 +51,12 @@ Global
{1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|Mixed Platforms.Build.0 = Release|x86
{1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|x86.ActiveCfg = Release|x86
{1940BB9C-9F27-417B-A227-2E69C0A8C172}.Release|x86.Build.0 = Release|x86
+ {2F62608C-652C-406B-9775-4CE34E151D45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {2F62608C-652C-406B-9775-4CE34E151D45}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {2F62608C-652C-406B-9775-4CE34E151D45}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {2F62608C-652C-406B-9775-4CE34E151D45}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {2F62608C-652C-406B-9775-4CE34E151D45}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {2F62608C-652C-406B-9775-4CE34E151D45}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
78 PyWpfSample/PyWpfSample.py
View
@@ -0,0 +1,78 @@
+import wpf
+
+import FlippingGame
+
+from System.Windows import Application, Window, Visibility
+from System.Windows.Media import Brushes
+
+class WpfSampleWindow(Window):
+ def __init__(self):
+ wpf.LoadComponent(self, 'PyWpfSample.xaml')
+ self.game = FlippingGame.FlippingGame()
+
+ def flipButton_Click(self, sender, e):
+ wager = self._getWager()
+ if not wager:
+ return
+
+ guess = "H" if self.guessHeadsButton.IsChecked else "T"
+ won, toss = self.game.flip(guess, wager)
+
+ self._showToss(won, toss)
+ self._showBankroll()
+ self._maybeEndGame()
+
+ def Window_Loaded(self, sender, e):
+ self._showBankroll()
+
+ def _getWager(self):
+ try:
+ wager = int(self.wagerBox.Text)
+ except ValueError as v:
+ self.wagerBox.Foreground = Brushes.Red
+ self._showError("Wager must be a number.")
+ return
+ else:
+ self._hideError()
+ self.wagerBox.Foreground = Brushes.Black
+
+ if wager < 1:
+ self.wagerBox.Foreground = Brushes.Red
+ self._showError("Wager must be at least 1 credit.")
+ return
+
+ if wager > self.game.bankroll:
+ self.wagerBox.Foreground = Brushes.Red
+ self._showError("Wager cannot be more than your bankroll.")
+ return
+
+ return wager
+
+ def _showError(self, error):
+ self.errorLabel.Content = error
+ self.errorLabel.Visibility = Visibility.Visible
+
+ def _hideError(self):
+ self.errorLabel.Visibility = Visibility.Collapsed
+
+ def _showToss(self, won, toss):
+ self.resultLabel.Content = toss
+ self.resultLabel.Foreground = Brushes.Green if won else Brushes.Red
+
+ def _showBankroll(self):
+ self.bankrollLabel.Content = str(self.game.bankroll)
+
+ def _maybeEndGame(self):
+ if self.game.bankroll <= 0:
+ self._showToss(False, 'X')
+
+ self.flipButton.IsEnabled = False
+ self.wagerBox.IsEnabled = False
+ self.guessHeadsButton.IsEnabled = False
+ self.guessTailsButton.IsEnabled = False
+
+ def wagerBox_GotFocus(self, sender, e):
+ sender.Foreground = Brushes.Black
+
+if __name__ == '__main__':
+ Application().Run(WpfSampleWindow())
52 PyWpfSample/PyWpfSample.pyproj
View
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{2f62608c-652c-406b-9775-4ce34e151d45}</ProjectGuid>
+ <ProjectHome>.</ProjectHome>
+ <StartupFile>PyWpfSample.py</StartupFile>
+ <SearchPath>..\FlippingGame\</SearchPath>
+ <WorkingDirectory>.</WorkingDirectory>
+ <IsWindowsApplication>True</IsWindowsApplication>
+ <InterpreterId>dc002bdb-400d-4430-b1a8-b419d62bd6ef</InterpreterId>
+ <LaunchProvider>IronPython (.NET) launcher</LaunchProvider>
+ <InterpreterVersion>2.7</InterpreterVersion>
+ <OutputPath>.</OutputPath>
+ <Name>PyWpfSample</Name>
+ <RootNamespace>PyWpfSample</RootNamespace>
+ <CommandLineArguments />
+ <InterpreterPath />
+ <DebugStdLib>False</DebugStdLib>
+ <InterpreterArguments>-X:Debug</InterpreterArguments>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
+ <DebugSymbols>true</DebugSymbols>
+ <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
+ <DebugSymbols>true</DebugSymbols>
+ <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
+ </PropertyGroup>
+ <ItemGroup>
+ <None Include="PyWpfSample.py" />
+ <None Include="PyWpfSample.xaml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Folder Include="FlippingGame\" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="C:\Users\Jeff\Documents\Repositories\IronPythonSamples\FlippingGame\FlippingGame.py">
+ <Link>FlippingGame\FlippingGame.py</Link>
+ </Compile>
+ </ItemGroup>
+ <ItemGroup>
+ <Reference Include="PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
+ <Name>PresentationCore</Name>
+ <AssemblyName>PresentationCore.dll</AssemblyName>
+ <HintPath>..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll</HintPath>
+ <Private>True</Private>
+ </Reference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
+</Project>
42 PyWpfSample/PyWpfSample.xaml
View
@@ -0,0 +1,42 @@
+<Window
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ Title="Coin Flipping" Loaded="Window_Loaded" ResizeMode="CanMinimize">
+ <Grid>
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="Auto" />
+ </Grid.RowDefinitions>
+ <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition Width="Auto" />
+ <ColumnDefinition Width="Auto" />
+ </Grid.ColumnDefinitions>
+ <Grid.RowDefinitions>
+ <RowDefinition Height="*" />
+ </Grid.RowDefinitions>
+ <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="Auto" />
+ </Grid.RowDefinitions>
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition Width="Auto" />
+ <ColumnDefinition Width="Auto" />
+ </Grid.ColumnDefinitions>
+ <Label Content="Bankroll:" HorizontalAlignment="Right" VerticalAlignment="Center" />
+ <Label Content="Wager:" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center" />
+ <Label Content="Guess:" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center" />
+ <RadioButton Content="Heads" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" IsChecked="True" GroupName="guessButtons" Name="guessHeadsButton" />
+ <RadioButton Content="Tails" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="55,0,0,0" GroupName="guessButtons" Name="guessTailsButton" />
+ <Label Content="?" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Name="bankrollLabel" />
+ <TextBox Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="95" Name="wagerBox" GotFocus="wagerBox_GotFocus" Text="50" TextAlignment="Right" Margin="0,2" />
+ </Grid>
+ <Button Content="Flip!" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Name="flipButton" MinHeight="28" MinWidth="75" Click="flipButton_Click" Margin="8,0" />
+ </Grid>
+ <Label Content="?" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" FontWeight="Bold" Name="resultLabel" />
+ <Label Content="Error" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Red" Visibility="Collapsed" Name="errorLabel"></Label>
+ </Grid>
+</Window>
4 WinFormsSample/packages.config
View
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="IronPython" version="2.7.2-final1" />
+</packages>
Please sign in to comment.
Something went wrong with that request. Please try again.