Skip to main content

Base classes for easy using MVC pattern with CTk library

Project description

CTkMVC

Install

pip install ctkmvc

Description

The library that provides a few base classes for easy using MVC architecture pattern together with CTk library to make simple and lightweight desktop applications.

Using example

In this example we will create a simple system that let us to make a button changing its text by clicking on it. To make your own View, Controller and Model classes you should inherit them from base abstract classes View, Controller and ObservableModel like that:

  • Imports
from customtkinter import CTkButton
from random import Random

from view import View
from model import ObservableModel
from controller import Controller
  • Our View
class MyWindow(View):

    def __init__(self, controller, model):

        super().__init__(controller, model)

        self.wm_title('Window')

        self.resizable(False, False)

        self.geometry('600x400')

        self.button = CTkButton(self, command=self._controller.button_click)

        self.button.grid(row=4, column=0, padx=(20, 20), pady=(10, 10), sticky="ew")


    def model_is_changed(self):
        
        self.button.configure(text=self._model.button_text)

Inherit our Viewfrom View abstract class and override model_is_changed method to react to Model changes. Then in __init__ method we create CTk attributes to design our window and do not forget to call super __init__ method to construct base View class by passing our Controller and Model objects.

  • Our Model
class MyWindowModel(ObservableModel):

    def __init__(self):

        super().__init__()
        
        self.__button_text = None

    @property
    def button_text(self):
        return self.__button_text
    
    @button_text.setter
    def button_text(self, value):

        self.__button_text = value

        self.notify_observers()

Inherit our Model from ObservableModel base abstract class and implement necessary properties our View observes. In setter method we call notify_observers method to notify subscibed views.

  • Our Controller
class MyWindowController(Controller):

    def __init__(self, view_cls, model):

        super().__init__(view_cls, model)

    
    def button_click(self):

        button_names = ['ctk', 'MVC', 'architecture', 'pattern']

        self._model.button_text = Random().choice(button_names)

Inherit our Controller from Controller base abstract class. Our Controller controles creating View object therefore in its __init__ method we pass our Model object and CLASS itself but not certain object of our View beacuse of this:

class Controller(ABC):

    def __init__(self, view_cls: 'type[View]', model: ObservableModel):

        self._view = view_cls(self, model)
        
        # Some code...

Result

from window import MyWindow, MyWindowModel, MyWindowController

def main():
    
    MyWindowController(

        MyWindow,
        MyWindowModel()
    )


if __name__=='__main__':
    main()

We get a window with a button that changes its text by clicking on it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ctkmvc-0.0.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

ctkmvc-0.0.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file ctkmvc-0.0.3.tar.gz.

File metadata

  • Download URL: ctkmvc-0.0.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for ctkmvc-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e82373d4b8bd9ac48373719430e2e29116bd6e85da6d9fbcf6e5309a5ef475ea
MD5 599d0683a6e3a3e1b8ecc1671f346948
BLAKE2b-256 fd4be544b8566fd5cfb486ab3cb8b0e98bc62d8ce6ae8cc979084a387f68e8b8

See more details on using hashes here.

File details

Details for the file ctkmvc-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: ctkmvc-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for ctkmvc-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b160aaacffb589200def130d35b122b3db7f6f5dacad374347a56d9f5a5263a7
MD5 085950c91c0bc12867048fe56b6af2cf
BLAKE2b-256 54ad2b7cffb1ac04a8fa67e805d585aa9416c7e8264bbe28ee29769a8cf70a62

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page