Book Image

Python GUI Programming Cookbook

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook

By: Burkhard Meier

Overview of this book

Table of Contents (18 chapters)
Python GUI Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

How to use design patterns successfully


In this recipe, we will create widgets for our Python GUI by using the factory design pattern.

In previous recipes, we created our widgets either manually one at a time or dynamically in a loop.

Using the factory design pattern, we will use the factory to create our widgets.

Getting ready

We will create a Python GUI which has three buttons each having different styles.

How to do it...

Towards the top of our Python GUI module, just below the import statements, we create several classes:

import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu

class ButtonFactory():
    def createButton(self, type_):
        return buttonTypes[type_]()
            
class ButtonBase():     
    relief     ='flat'
    foreground ='white'
    def getButtonConfig(self):
        return self.relief, self.foreground
    
class ButtonRidge(ButtonBase):
    relief     ='ridge'
    foreground ='red'        
    
class ButtonSunken(ButtonBase...