Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

MVC and Tk

by LanX (Saint)
on Jul 08, 2022 at 13:19 UTC ( [id://11145356]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm searching for good examples for using the MVC pattern with Tk.

Motivation apart from the usual benefits is to be able to switch between different GUI frameworks or even serving HTML...

Any ideas?

Thanks! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

PS: FWIW: I found examples in Python° but would be interested seeing it in Perl...

°) like https://stackoverflow.com/questions/7638139/how-to-implement-the-mvc-pattern-in-tkinter#17236698

Replies are listed 'Best First'.
Re: MVC and Tk
by kcott (Archbishop) on Jul 08, 2022 at 20:43 UTC

    G'day Rolf,

    Here's a short example.

    #!/usr/bin/env perl use strict; use warnings; package Model; my $num; sub num { my ($new_num) = @_; $num = $new_num if defined $new_num; $num = 0 unless defined $num; return $num; } package View; use Tk; sub init { my ($num) = @_; my $mw = MainWindow::->new(-title => 'Tk MVC Example'); $mw->geometry('280x80+50+80'); $mw->Label(-textvariable => \$num)->pack(); $mw->Button( -text => 'Increment', -command => sub { ++$num } )->pack(); $mw->Button( -text => 'Exit', -command => sub { exit; } )->pack(); MainLoop; } package Controller; sub run { my ($start) = @_; View::init(Model::num($start)); } package main; Controller::run(@ARGV ? $ARGV[0] : undef);

    In production, those packages would be in separate files, so the separation of M, V & C would be more obvious:

    # Model.pm package Model; ... 1; # View.pm package View; ... 1; # Controller.pm package Controller; use Model; use View; ... 1; # pm_11145356_tk_mvc.pl ... use Controller; ...

    As an interesting artefact of a "short example" with no validation, you can call 'pm_11145356_tk_mvc.pl fred' and see increments: fred, free, fref, etc.

    — Ken

Re: MVC and Tk
by stefbv (Curate) on Jul 09, 2022 at 11:21 UTC

    Hi Rolf,

    I have a project (Moo + Tk) called Fenix on GitHub, and a project (Moo + Wx) named Sqitch-GUI.

    WiP on the devel branch.

    I don't know how good or bad they are, I had no feedback on them.

    Best regards, Stefan

Re: MVC and Tk
by karlgoethebier (Abbot) on Jul 11, 2022 at 02:29 UTC

    Take a look here for further inspiration. As far as I remember you where involved a decade or so.

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11145356]
Approved by salva
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found