Skip to content

GUI Programming with Python and Kivy E-BOOK

$48.00

All the basics that you need to know to start programming GUI applications with Python and Kivy. Widgets, layouts, Kivy ids and properties, graphics, screens, animation, sound, deployment.

Over 800 pages of text. Lots of full-color images.

GUI Programming with Python and Kivy

This book covers all the basics that you need to know to start programming GUI applications with Python and Kivy. Throughout the book we will be building a GUI application from scratch, a fully functional game using all kinds of tools that Kivy has to offer.

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.

 

The book contains over 800 pages and lots of full-color images. Here are the detailed contents:

CONTENTS

Preface. xiv

Part I – Let’s Get Started.. 1

Chapter 1 – Introduction. 2

Settings Screen. 2

Race Screen. 3

Bets and Results 3

Chapter 2 – Setting Up the Environment 5

The Kivy Library. 5

The Visual Studio Code Text Editor 6

Extensions 6

Chapter 3 – A Basic Kivy App in Python. 8

Hello World – A Basic Kivy App. 8

Create a Folder 8

Create a New File. 9

The Hello World App. 9

Run Your Kivy App. 10

Chapter 4 – Running Your Kivy App. 11

How To Run Your App. 11

Chapter 5 – A Basic Kivy App Rewritten with Python and the Kivy Language. 14

kv Files and the Kivy Language. 14

Naming Convention. 15

The Builder Class 15

Linking with a kv File. 16

 

Part II – Kivy Widgets. 17

Chapter 6 – Introduction to Kivy Widgets. 18

Kivy Built-in Widgets 19

Button. 19

Carousel. 20

Switch. 20

Chapter 7 – The Label Widget 22

The font_size Property. 23

The color Property. 23

Text Markup. 24

The Tags 24

The markup Property. 25

It’s Your Turn Now….. 26

Chapter 8 – The Button Widget 27

A Button is Basically a Label….. 27

The background_color and background_normal Properties 28

Basic Button Events 29

Multiple Commands 30

It’s Your Turn Now….. 32

Chapter 9 – The Check Box Widget 34

The on_active Event 34

It’s Your Turn Now….. 35

Chapter 10 – The Toggle Button Widget 36

It’s Your Turn Now….. 37

Chapter 11 – The Text Input Widget 38

Multiline Text 38

Hint Text 39

Single Line Text 39

Colors and Alignment 40

Other Properties 41

It’s Your Turn Now….. 42

Chapter 12 – The Slider Widget 44

Horizontal Orientation. 44

Vertical Orientation. 45

It’s Your Turn Now….. 46

Chapter 13 – Defining Widgets in Python Code. 48

The Label 48

The Button. 50

The Check Box. 51

The Toggle Button. 52

The Text Input 52

The Slider 53

It’s Your Turn Now….. 54

Chapter 14 – Loading Kivy Language Strings in Python Code. 57

It’s Your Turn Now….. 58

Chapter 15 – Custom Widgets. 61

Defining Custom Widgets in Code. 61

Widget Position and Size. 62

Other Properties in Custom Widgets 63

It’s Your Turn Now….. 64

Chapter 16 – Classes and Class Rules. 66

Defining Classes in the Kivy Language. 66

Defining Classes in Python. 68

The Problem with Fixed Positions 69

It’s Your Turn Now….. 69

Chapter 17 – The root, self and app Variables. 71

Positioning with the root and self Variables 72

Using the root, self and app Variables 74

self. 74

root. 75

app. 76

It’s Your Turn Now….. 77

 

Part III – Kivy Layouts. 81

Chapter 18 – Introduction to Kivy Layouts. 82

Introduction to Positioning and Scaling Widgets in Layouts 82

Kivy Layout Types 82

Chapter 19 – Widget Size and Position in Layouts. 84

The size_hint and pos_hint Properties 84

The x, right, y and top Properties 84

Widget Size. 85

Setting Widget Size with the size_hint Property. 85

Setting Widget Size with the size_hint_x and size_hint_y Properties 86

Widget Position. 87

Horizontal Positioning. 87

Vertical Positioning. 90

Positioning in Both Directions 91

It’s Your Turn Now….. 92

Chapter 20 – Advanced Scaling and Positioning. 96

Using the x, center_x, right, y, center_y and top Properties in Layouts 96

Using the x, center_x, right, y, center_y and top Properties Outside Layouts 97

Using the pos Property Inside Layouts 99

Using the size, width and height Properties Inside Layouts for Scaling. 100

It’s Your Turn Now….. 101

Chapter 21 – The RelativeLayout 106

RelativeLayout. 106

It’s Your Turn Now….. 110

Chapter 22 – The GridLayout 112

GridLayout Rows 112

GridLayout Columns 113

Column Width and Row Height 114

It’s Your Turn Now….. 116

Chapter 23 – The BoxLayout 118

It’s Your Turn Now….. 120

Chapter 24 – The StackLayout 121

It’s Your Turn Now….. 123

Chapter 25 – The AnchorLayout 125

It’s Your Turn Now….. 127

Chapter 26 – The PageLayout 128

It’s Your Turn Now….. 129

Chapter 27 – Embedding Layouts. 131

FloatLayouts and RelativeLayouts inside GridLayout 131

Top Menu in AnchorLayout 133

Toolbar with StackLayout and AnchorLayout 133

Imitating Different Cell Sizes in GridLayout 134

It’s Your Turn Now….. 136

Chapter 28 – Defining Layouts in Python Code. 138

The FloatLayout 138

The RelativeLayout 139

The GridLayout 141

The BoxLayout 142

The StackLayout 143

The AnchorLayout 145

The PageLayout 146

Embedded Layouts 146

It’s Your Turn Now….. 148

 

Part IV – The Slugrace Project Structure and Assets. 151

Chapter 29 – Game Assets. 152

Visual Game Assets – the Images 152

The Race Track. 152

The Slugs 152

The Accidents 153

Splash Screen. 154

Button Images 154

Audio Game Assets – the Sounds 155

Game Background Music and Sound Effects 155

Slugs’ Sound Effects 155

Accident Sound Effects 155

Chapter 30 – The Project Folder 157

The Project Folder 157

Chapter 31 – The File Hierarchy. 158

Adding Files 158

Chapter 32 – Some Basic Code to Get Started. 161

The main.py File. 161

The settings.py and settings.kv Files 161

The race.py and race.kv Files 163

 

Part V – The Slugrace Project GUIs. 164

Chapter 33 – Introduction to the Settings Screen GUI 165

Settings Screen Areas 166

The root Widget 166

Chapter 34 – The Settings Screen – the Players Area. 168

The Players Area. 168

The Radio Buttons 169

Player Name and Initial Money Setup. 171

It’s Your Turn Now….. 172

Chapter 35 – The Settings Screen – the Ending Conditions Area. 175

The Ending Conditions Area. 175

It’s Your Turn Now….. 176

Chapter 36 – The Settings Screen – Sizing and Positioning the Widgets. 179

Sizing and Positioning the Widgets 179

It’s Your Turn Now….. 181

Chapter 37 – The Settings Screen – Aligning Label Text 185

Aligning Label Text 185

It’s Your Turn Now….. 186

Chapter 38 – The Settings Screen – Padding and Spacing. 187

Padding and Spacing. 187

It’s Your Turn Now….. 189

Chapter 39 – The Settings Screen – Class Rules. 191

Class Rules 191

Custom Labels 191

It’s Your Turn Now….. 197

Chapter 40 – The Race Screen GUI 200

The root Widget 202

The Areas Overview.. 202

The Info, Stats and Buttons Area Overview.. 202

The Track Area Overview.. 203

The Bets Area Overview.. 204

The Areas One by One. 204

It’s Your Turn Now….. 205

Chapter 41 – The App Window Size Configuration. 210

Configuration Object 210

Fullscreen Mode. 212

Resizing the Window.. 212

It’s Your Turn Now….. 213

Chapter 42 – The Bets Screen GUI 215

The root Widget 215

The Title Label and Go Button Areas 216

The Player Bets Area. 217

It’s Your Turn Now….. 217

Chapter 43 – The Results Screen GUI 221

The root Widget 221

The kv File. 222

It’s Your Turn Now….. 222

Chapter 44 – The Game Over Screen GUI 226

The root Widget 226

The kv File. 227

It’s Your Turn Now….. 227

 

Part VI – Graphics in Kivy. 230

Chapter 45 – Introduction to the Canvas Object 231

The Coordinate Space. 232

Drawing Instructions 232

What Is Actually a Widget? 232

Chapter 46 – Canvas Vertex Instructions – Drawing a Rectangle. 233

Drawing Shapes 233

Drawing a Rectangle. 234

It’s Your Turn Now….. 236

Chapter 47 – Drawing Filled Shapes on Widgets. 237

Drawing an Ellipse. 237

Drawing a Triangle. 238

Drawing a Hexagon. 238

Drawing a Segment of a Circle. 239

It’s Your Turn Now….. 240

Chapter 48 – Drawing Lines, Curves and Other Point-Defined Shapes. 242

Drawing Lines 242

Drawing Irregular Triangles 243

Drawing Quadrilaterals 243

Drawing Points 244

Drawing a Bezier Curve. 244

Drawing a Mesh. 245

It’s Your Turn Now….. 246

Chapter 49 – Drawing Just the Borders of Shapes. 248

Drawing the Border of a Rounded Rectangle. 248

Drawing the Border of a Decagon. 249

It’s Your Turn Now….. 249

Chapter 50 – Vertex Instructions in Python Code. 251

Using a Context Manager 254

It’s Your Turn Now….. 254

Chapter 51 – Adding Shapes to the Project Screens. 256

Rounded Rectangles in Settings Screen. 256

It’s Your Turn Now….. 258

Chapter 52 – Canvas Context Instructions – the Color Instruction. 262

It’s Your Turn Now….. 267

Chapter 53 – Canvas Instructions – Order of Execution. 269

The Three Sets of Canvas Instructions 270

It’s Your Turn Now….. 271

Chapter 54 – The Translate, Rotate and Scale Instructions. 273

Translation. 274

Rotation. 275

Scale. 278

It’s Your Turn Now….. 281

Chapter 55 – The PushMatrix and PopMatrix Instructions. 283

It’s Your Turn Now….. 286

Chapter 56 – Context Instructions in Python Code. 290

It’s Your Turn Now….. 293

Chapter 57 – Adding Colors to the Slugrace Project 295

It’s Your Turn Now….. 297

Chapter 58 – Styling the Widgets in the Slugrace Project 303

The Buttons 303

It’s Your Turn Now….. 304

Chapter 59 – Adding Background Graphics Using Canvas Instructions. 312

Image and Color 313

It’s Your Turn Now….. 314

Chapter 60 – Adding the Graphical Assets to the Slugrace Project 317

The Track Image. 317

The White Labels 319

The Top View Slug Images 323

It’s Your Turn Now….. 326

Chapter 61 – Making and Using an Atlas. 328

Creating an Atlas 328

The Atlas Image and the json File. 333

How to Use the Atlas 334

It’s Your Turn Now….. 336

 

Part VII – Kivy Properties and Referencing Objects. 340

Chapter 62 – Parents and Children. 341

The self Variable Revisited. 342

The root Variable Revisited. 343

The app Variable Revisited. 344

Parent-Child Relationships 345

Parents and Children in Python Code. 347

It’s Your Turn Now….. 349

Chapter 63 – Kivy ids. 352

Kivy ids in the Kivy Language. 352

Kivy ids in Python Code. 354

The Scope of Kivy ids 356

It’s Your Turn Now….. 357

Chapter 64 – Introduction to Kivy Properties. 360

What Are Kivy Properties? 360

Features of Kivy Property. 363

Types of Kivy Properties 364

It’s Your Turn Now….. 365

Chapter 65 – Referencing Objects Using Kivy Properties. 367

The ObjectProperty. 368

It’s Your Turn Now….. 370

Chapter 66 – Defining and Using Kivy Properties. 373

Class Attributes 373

Instance Attributes 377

Kivy Properties 379

It’s Your Turn Now….. 380

Chapter 67 – Observing Property Changes. 382

The on_<property_name> Methods 382

The bind Method. 384

It’s Your Turn Now….. 387

Chapter 68 – Adding Some Properties to the Slugrace Project 389

The Player Radio Buttons 389

The Player Settings 390

It’s Your Turn Now….. 393

 

Part VIII – Screens, Files and Classes. 408

Chapter 69 – Game Structure Organization. 409

Chapter 70 – A Centralized Style Sheet File. 411

Access to the Rules Defined in the widgets.kv File. 413

It’s Your Turn Now….. 413

Chapter 71 – Let Screens Be Screens. 416

It’s Your Turn Now….. 418

Chapter 72 – Screen Managers. 421

The ScreenManager Class 421

Importing in kv Files 423

Switching Screens 425

It’s Your Turn Now….. 426

Chapter 73 – Managing Screens – Some Code Cleanup. 431

kv Files loaded Multiple Times 432

Configuration Code. 433

Error Loading Texture. 433

Chapter 74 – Transitions. 437

Transition Direction. 437

Fixing the Background Color 438

No Transition at All 439

Other Transitions 440

CardTransition. 440

SwapTransition. 440

FadeTransition. 441

WipeTransition. 441

FallOutTransition. 442

RiseInTransition. 442

It’s Your Turn Now….. 443

 

Part IX – Game Logic. 445

Chapter 75 – The Main Classes for the Game Logic. 446

The Slug Class 446

The Player Class 450

It’s Your Turn Now….. 450

Chapter 76 – The Game Class. 452

It’s Your Turn Now….. 455

Chapter 77 – Events in Kivy. 458

Overriding Inherited Event Methods 458

Triggering Methods 461

Property Events 463

It’s Your Turn Now….. 465

Chapter 78 – Binding and Unbinding Events. 467

Binding Events 467

Unbinding Events 469

It’s Your Turn Now….. 470

Chapter 79 – Events in the Settings Screen. 473

Player Radio Button Events 474

It’s Your Turn Now….. 477

Chapter 80 – Player Settings. 481

Setting the Players 481

Accessing App Data in Python Code. 483

The set_players Method. 483

It’s Your Turn Now….. 485

Chapter 81 – Slug Settings. 490

It’s Your Turn Now….. 491

Chapter 82 – Game Settings. 495

It’s Your Turn Now….. 498

Chapter 83 – Widget Visibility and Functionality. 502

Disabling Widgets 502

Widget Opacity. 503

Disabling and Hiding Widgets in the Settings Screen. 505

It’s Your Turn Now….. 506

Chapter 84 – User Input Validation. 519

Maximum String Length for Players’ Names 519

Only Digits in the Initial Money Text Inputs 522

Limited Value Range for the Initial Money Text Inputs 524

It’s Your Turn Now….. 526

Chapter 85 – Using the Sliders. 535

It’s Your Turn Now….. 536

Chapter 86 – Betting Logic. 538

Choosing a Slug. 538

It’s Your Turn Now….. 541

Chapter 87 – Start of the Game. 546

Starting the Game. 547

It’s Your Turn Now….. 547

Chapter 88 – Race Simulation. 549

Simulation of a Race. 549

The Results Screen. 552

Updating the Odds 555

It’s Your Turn Now….. 558

Chapter 89 – Resetting the Race. 567

Updating the Race Number 567

Resetting the Players’ Chosen Slugs and Bets 569

It’s Your Turn Now….. 579

Chapter 90 – Handling the Players. 581

Bankrupt Players 582

Bankrupt Players in the Bets Screen. 583

It’s Your Turn Now….. 586

Chapter 91 – Game Over Check. 590

One Player Left – the Winner 590

All Players Go Bankrupt – No Winner 593

It’s Your Turn Now….. 593

Chapter 92 – Game Over After a Set Number of Races. 595

Updating Races Info. 595

Game Over Check. 597

Handling Game Over 598

It’s Your Turn Now….. 606

Chapter 93 – Using the Clock. 610

The Clock Class 611

Scheduling Delay. 611

Scheduling Interval 613

Unscheduling a Method. 614

Scheduling a Method with Arguments 615

It’s Your Turn Now….. 617

Chapter 94 – Using the Clock Class in the Slugrace Project 618

Starting the Game Clock. 618

Game Over Check. 620

It’s Your Turn Now….. 622

Chapter 95 – Restarting the Game. 627

Restarting the Game. 628

Some Issues After Restarting the Game. 632

It’s Your Turn Now….. 633

Chapter 96 – Ending the Game Manually. 636

Ending the Game Manually. 636

It’s Your Turn Now….. 639

Chapter 97 – Quitting the Game. 640

It’s Your Turn Now….. 643

 

Part X – Animation and Sound.. 645

Chapter 98 – Introduction to Animation in Kivy. 646

A Simple Animation. 647

Animating Multiple Properties 648

Transitions 649

Step. 650

Stopping and Canceling Animations 651

It’s Your Turn Now….. 652

Chapter 99 – Sequential, Parallel and Repeated Animations. 658

Sequential Animations 658

Parallel Animations 660

It’s Your Turn Now….. 662

Chapter 100 – Animation Events. 664

Stopping vs Canceling the Animation. 666

It’s Your Turn Now….. 667

Chapter 101 – Animating the Slugs on the Track. 671

Adding the Running Animation to the Slug Class 671

Differentiating the Speeds of the Slugs 673

It’s Your Turn Now….. 676

Chapter 102 – Animating the Eyes. 678

Creating the Eye Rotation Animation. 679

It’s Your Turn Now….. 681

Chapter 103 – Game Logic Revisited. 682

The Race and the Winning Slug. 682

It’s Your Turn Now….. 688

Chapter 104 – Sound in Kivy. 696

Loading, Playing and Stopping Sound. 696

Playing vs Looping. 697

Sound Events 698

Unloading Audio File from Memory. 699

It’s Your Turn Now….. 700

Chapter 105 – Adding Sounds to the Slugrace Project 703

Sound Assets Overview.. 703

Loading the Sounds 705

Background Music 705

The Slugs Running Sound. 706

The Go Sound. 707

The Game Over Sound. 708

It’s Your Turn Now….. 708

Chapter 106 – Accidents in the Slugrace App. 716

Chapter 107 – Accident Classes. 719

The Accident Abstract Class 719

The __init__ Method. 720

The Subclasses 721

Chapter 108 – Accident Logic. 728

Instantiating Accident Classes 728

When Should an Accident Happen? 729

Let the Accident Happen. 730

Testing the Accidents 731

It’s Your Turn Now….. 733

Chapter 109 – The Broken Leg Accident 737

The BrokenLegAccident Class’s happen Method. 738

It’s Your Turn Now….. 740

Chapter 110 – The Overheat Accident 743

It’s Your Turn Now….. 746

Chapter 111 – The Heart Attack Accident 747

It’s Your Turn Now….. 749

Chapter 112 – The Grass Accident 751

Race Continuation. 752

It’s Your Turn Now….. 753

Chapter 113 – The Asleep Accident 758

It’s Your Turn Now….. 759

Chapter 114 – The Blind Accident 761

It’s Your Turn Now….. 763

Chapter 115 – The Puddle Accident 764

Accessing the Racetrack. 764

It’s Your Turn Now….. 767

Chapter 116 – The Electroshock Accident 769

It’s Your Turn Now….. 770

Chapter 117 – The Turning Back Accident 771

It’s Your Turn Now….. 773

Chapter 118 – The Shooting Eyes Accident 775

It’s Your Turn Now….. 779

Chapter 119 – The Rubberized Accident 780

It’s Your Turn Now….. 783

Chapter 120 – The Devoured Accident 784

It’s Your Turn Now….. 787

Chapter 121 – Accident Setup. 788

 

Part XI – Final Touches and Deployment 791

Chapter 122 – Game Instructions. 792

Screenshots 792

The Instructions Screen. 795

Navigation Between Instructions Screen and Race Screen. 797

Chapter 123 – Splash Screen. 798

Splash Image. 798

Splash Screen. 798

Chapter 124 – Final Configuration. 801

Window Frame. 801

Chapter 125 – Deployment 802

PyInstaller 802

Getting Ready to Package the App. 802

Creating an App Icon. 803

Packaging the App. 803

Chapter 126 – Conclusion. 808

Index. 809

 

 

 

Reviews

There are no reviews yet.

Be the first to review “GUI Programming with Python and Kivy E-BOOK”

Your email address will not be published. Required fields are marked *