Skip to Content
Tkinter GUI Programming by Example
book

Tkinter GUI Programming by Example

by David Love
April 2018
Beginner content levelBeginner
340 pages
7h 54m
English
Packt Publishing
Content preview from Tkinter GUI Programming by Example

The Deck class

The Deck will need to contain 52 unique cards and must be able to shuffle itself. It will also need to be able to deal cards and decrease in size as cards are removed:

class Deck:    def __init__(self):        self.cards = [Card(s, v) for s in ["Spades", "Clubs", "Hearts",                      "Diamonds"] for v in ["A", "2", "3", "4", "5", "6",                       "7", "8", "9", "10", "J", "Q", "K"]]    def shuffle(self):        if len(self.cards) > 1:            random.shuffle(self.cards)    def deal(self):        if len(self.cards) > 1:            return self.cards.pop(0)

When creating an instance of the Deck, we simply need to have a collection of every possible card. We achieve this using a list comprehension which contains lists of every suit and value. We pass each combination over to the initialization for ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Tkinter GUI Application Development Blueprints, Second Edition - Second Edition

Tkinter GUI Application Development Blueprints, Second Edition - Second Edition

Bhaskar Chaudhary

Publisher Resources

ISBN: 9781788627481Supplemental Content