vendredi 13 septembre 2019

Where should functions that control the window attributes go in a MCV design pattern for Tkinter?

I'm designing a Tkinter app and I really want to apply proper structure to my application. One thing I still struggle with is where certain methods should go. In this case, I have a function that centers the main window once the GUI runs so that the user sees the window in the middle of their screen. I'm thinking that this function should be a method in the Controller class but I also thought it could go in the Model class because it's technically logic? Or maybe it shouldn't be in a class at all?

I don't want to get to broad, but is there generally a good way to think about where too place methods? Like a checklist of sorts to go through when making design assumptions regarding the methods put into a class?

CODE

import tkinter as tk

class Model:
    def __init__(self):
        pass


class View:
    def __init__(self, master):
        self.master_frame = tk.Frame(master)


class Controller:
    def __init__(self):
        self.root = tk.Tk()
        self.model = Model()
        self.view = View(self.root)

    def run(self):
        self.root.title("My App")
        self.middle_of_screen(1350, 750)
        self.root.resizable(False, False)
        self.root.iconbitmap('images/an_image.ico')
        self.root.mainloop()

    # Not exactly sure where it's most conceptually correct to put this method
    def middle_of_screen(self, window_width, window_height):
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()

        x = (screen_width // 2) - (window_width // 2)
        y = (screen_height // 2) - (window_height // 2)

        self.root.geometry(f'{window_width}x{window_height}+{x}+{y}')


if __name__ == '__main__':
    c = Controller()
    c.run()


Aucun commentaire:

Enregistrer un commentaire