O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Card class

The Card class will be the first class we define, as both of our other classes will need to use it. Open up a new file and type the following code:

import randomclass Card:    def __init__(self, suit, value):        self.suit = suit        self.value = value    def __repr__(self):        return " of ".join((self.value, self.suit))

The only import we will need for our game is the random module. This will allow us to shuffle our virtual deck of cards at the beginning of every game.

Our first class will be one representing the playing cards. Each card will have a suit (hearts, diamonds, spades, and clubs) and a value (ace to king). We define the __repr__ function in order to change how the card is displayed when we call print on it. Our function will return ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required