mercredi 4 novembre 2020

Entry widget factory class with a factory method

I have a modified version of Burkhard Meier's Button factory that needs to be extended with a text field factory. I need to follow the below structure:

Expected output:

enter image description here

import tkinter as tk
from tkinter import ttk
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):
    relief     ='sunken'
    foreground ='blue'        

   
class ButtonGroove(ButtonBase):
    relief     ='groove'
    foreground ='green'        

buttonTypes = [ButtonRidge, ButtonSunken, ButtonGroove]

Is the below a correct extension of the above code to create a concrete Entry widget Factory class with a fFactory method called generateText(..)? My instructions say tkinter textfields are Entry widgets.

class TextFactory():
    def generateText(self):
        sv=tk.StringVar()
        tx = factory.generateText(0).getTextConfig()[0]
        sv.set(tx)
        bg  = factory.generateText(0).getTextConfig()[1]
        action = tk.Entry(self.widgetFactory, textvariable=sv, background=bg, foreground="white")   
        action.grid(column=1, row=1)'''           

Is there a Python ninja out there that could help me understand how to do the above and also how to:

  1. Create an abstract product named TextBase() that will have default attributes textvariable and background?

  2. Declare a getTextConfig(...) method.

  3. Create 3 concrete text products named text_1/2/3/ that will be assigned textvariable values ‘type red / type blue / type green” as shown in the image below.

  4. Extend the OOP class with createTextFields() method that creates a factory object.

  5. I’ve been given the below code as well but I don’t know how to implement it to solve the above.

     #Entry field 1
     sv=tk.StringVar()
     tx = factory.createText(0).getTextConfig()[0]
     sv.set(tx)
     bg  = factory.createText(0).getTextConfig()[1]
     action = tk.Entry(self.widgetFactory, textvariable=sv, background=bg, foreground="white")   
     action.grid(column=1, row=1) 
    

Aucun commentaire:

Enregistrer un commentaire