In the previous part we added some Kivy properties to the Bets screen. In this part we’ll add some to the Results screen.
But before we delve into the topic, here’s some info for you.
*****
Book Info
I just published my Kivy book, GUI Programming with Python and Kivy. It’s pretty long (over 800 pages) and comprehensive. And, which also counts, easy to read. The book contains lots of illustrations.
This book covers all the basics that you need to know to start programming GUI applications with Python and Kivy. Throughout the book we are building a GUI application from scratch, a fully functional game using all kinds of tools that Kivy has to offer. It’s our Slugrace project, but covered in a much more in-depth manner.
Each part of the book starts with a theoretical introduction of a topic or idea that we then implement in the project. I assume you have no prior knowledge of the Kivy library, but you should have at least some basic knowledge of the Python programming language, including the object-oriented programming paradigm as this is what we will be using a lot in this book.
The book covers all the basic elements of Kivy that you have to know, like widgets, layouts, Kivy ids and properties, graphics, screens, animation, sound. Finally we’ll deploy the app to Windows. It is pretty comprehensive and after you finish it, I’m sure you’ll be able to create your own awesome GUI apps of any kind, not just games.
I hope you will have at least as much fun reading the book as I had writing it.
As far as this Kivy series is concerned, the following parts will contain the most important elements covered in the book. However, some elements will be presented in a simplified way here on my blog or omitted completely.
____________
If you are interested, you can purchase the book in four versions. Here are the links:
1) ebook – pdf version on my website – in full color
Here you can see the description of the book, sample graphics from the book and the full table of contents.
2) ebook – Kindle version on Amazon – in full color
3) paperback version on Amazon – in black and white
4) paperback version on Amazon – in full color
*****
And Now Let’s Move On…
Look at this part of the results.kv file:
# File name: results.kv
...
<ResultsScreen>:
...
### PLAYER RESULTS ###
BoxLayout:
orientation: 'vertical'
# player 1
BoxLayout:
RegularLabel:
text: 'Player 1'
BoxLayout:
RegularLabel:
text: 'had'
size_hint: (.4, 1)
RegularLabel:
text: '$1000'
BoxLayout:
RegularLabel:
text: 'bet'
size_hint: (.4, 1)
RegularLabel:
text: '$300'
RegularLabel:
text: 'on Speedster'
BoxLayout:
RegularLabel:
text: '- won'
size_hint: (.5, 1)
RegularLabel:
text: '$400'
BoxLayout:
RegularLabel:
text: 'now has'
RegularLabel:
text: '$1400'
RegularLabel:
text: 'The odds were 2.54'
# player 2
BoxLayout:
RegularLabel:
text: 'Player 2'
BoxLayout:
RegularLabel:
text: 'had'
size_hint: (.4, 1)
RegularLabel:
text: '$1000'
BoxLayout:
RegularLabel:
text: 'bet'
size_hint: (.4, 1)
RegularLabel:
text: '$300'
RegularLabel:
text: 'on Speedster'
BoxLayout:
RegularLabel:
text: '- lost'
size_hint: (.5, 1)
RegularLabel:
text: '$400'
BoxLayout:
RegularLabel:
text: 'now has'
RegularLabel:
text: '$600'
RegularLabel:
text: 'The odds were 1.59'
# player 3
BoxLayout:
RegularLabel:
text: 'Player 3'
BoxLayout:
RegularLabel:
text: 'had'
size_hint: (.4, 1)
RegularLabel:
text: '$1000'
BoxLayout:
RegularLabel:
text: 'bet'
size_hint: (.4, 1)
RegularLabel:
text: '$300'
RegularLabel:
text: 'on Trusty'
BoxLayout:
RegularLabel:
text: '- won'
size_hint: (.5, 1)
RegularLabel:
text: '$400'
BoxLayout:
RegularLabel:
text: 'now has'
RegularLabel:
text: '$1400'
RegularLabel:
text: 'The odds were 2.24'
# player 4
BoxLayout:
RegularLabel:
text: 'Player 4'
BoxLayout:
RegularLabel:
text: 'had'
size_hint: (.4, 1)
RegularLabel:
text: '$1000'
BoxLayout:
RegularLabel:
text: 'bet'
size_hint: (.4, 1)
RegularLabel:
text: '$300'
RegularLabel:
text: 'on Slowpoke'
BoxLayout:
RegularLabel:
text: '- lost'
size_hint: (.5, 1)
RegularLabel:
text: '$400'
BoxLayout:
RegularLabel:
text: 'now has'
RegularLabel:
text: '$600'
RegularLabel:
text: 'The odds were 1.85'
### NEXT RACE BUTTON ###
...
We’ll refactor this code to make it more readable and less repetitive. First, what are the differences? Here they are:
– The name of the player in the first RegularLabel will be different for each player.
– All the RegularLabels that display amounts of money will differ.
– The name of the slug on which the bet was put will be different.
– One of the RegularLabels informs us whether the player won or lost. So, there are two options possible here.
– The RegularLabel that displays the odds will also differ.
That said, let’s create the Result class that inherits from BoxLayout in the results.py file and add the following properties:
– the StringProperty player_name initialized to an empty string, representing the player’s name,
– the NumericProperty money_before initialized to 0, representing the amount of money the player had before the race,
– the NumericProperty bet_amount initialized to 0, representing the bet amount,
– the StringProperty slug_name initialized to an empty string, representing the slug on which the bet was put,
– the StringProperty result_info initialized to an empty string, informing us whether the player won or lost money,
– the NumericProperty gain_or_loss initialized to 0, representing the amount of money gained or lost,
– the NumericProperty current_money initialized to 0, representing the amount of money the player has after the race,
– the NumericProperty odds initialized to 0, representing the odds before the race that just finished.
So, here’s the Python file:
# File name: results.py
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.properties import NumericProperty, StringProperty
# Configuration
Config.set('graphics', 'width', '1200')
Config.set('graphics', 'height', '675')
class Result(BoxLayout):
player_name = StringProperty('')
money_before = NumericProperty(0)
bet_amount = NumericProperty(0)
slug_name = StringProperty('')
result_info = StringProperty('')
gain_or_loss = NumericProperty(0)
current_money = NumericProperty(0)
odds = NumericProperty(0)
class ResultsScreen(BoxLayout):
pass
class ResultsApp(App):
def build(self):
return ResultsScreen()
if __name__ == '__main__':
ResultsApp().run()
Then, let’s add the corresponding rule in the kv file. For now let’s set all the properties to the same values that they were before. Don’t worry they don’t make much sense at this moment. As far as the last RegularLabel is concerned, its text property should be set by concatenating the ‘The odds were ‘ string with the value of the odds property. Finally let’s use the Result instances in the code.
Here’s the kv file:
# File name: results.kv
...
<Result>:
RegularLabel:
text: root.player_name
BoxLayout:
RegularLabel:
text: 'had'
size_hint: (.4, 1)
RegularLabel:
text: '$' + str(root.money_before)
BoxLayout:
RegularLabel:
text: 'bet'
size_hint: (.4, 1)
RegularLabel:
text: '$' + str(root.bet_amount)
RegularLabel:
text: 'on ' + root.slug_name
BoxLayout:
RegularLabel:
text: root.result_info
size_hint: (.5, 1)
RegularLabel:
text: '$' + str(root.gain_or_loss)
BoxLayout:
RegularLabel:
text: 'now has'
RegularLabel:
text: '$' + str(root.current_money)
RegularLabel:
text: 'The odds were ' + str(root.odds)
<ResultsScreen>:
canvas:
Color:
rgba: 1, 1, .8, 1
Rectangle:
pos: self.pos
size: self.size
orientation: 'vertical'
padding: 10
### TITLE LABEL ###
BoldLabel:
text: 'Race 1 Results'
size_hint: (1, None)
height: 30
### PLAYER RESULTS ###
BoxLayout:
orientation: 'vertical'
# player 1
Result:
player_name: 'Player 1'
money_before: 1000
bet_amount: 300
slug_name: 'Speedster'
result_info: '- won'
gain_or_loss: 400
current_money: 1400
odds: 2.54
# player 2
Result:
player_name: 'Player 2'
money_before: 1000
bet_amount: 300
slug_name: 'Speedster'
result_info: '- lost'
gain_or_loss: 400
current_money: 600
odds: 1.59
# player 3
Result:
player_name: 'Player 3'
money_before: 1000
bet_amount: 300
slug_name: 'Trusty'
result_info: '- won'
gain_or_loss: 400
current_money: 1400
odds: 2.24
# player 4
Result:
player_name: 'Player 4'
money_before: 1000
bet_amount: 300
slug_name: 'Speedster'
result_info: '- lost'
gain_or_loss: 400
current_money: 600
odds: 1.85
### NEXT RACE BUTTON ###
RedButton:
text: 'Next Race'
Let’s run the app again to make sure everything works as before.