Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Object Oriented reference in -textvariable

by Corion (Patriarch)
on Aug 21, 2017 at 12:43 UTC ( [id://1197733]=note: print w/replies, xml ) Need Help??


in reply to Object Oriented reference in -textvariable

Are you certain that $mockOb->search returns a variable and not a value? Usually, Tk objects require a real variable and not a value to watch. A value never changes.

Depending on whether you have control over $mockOb, either have ->search return the reference to the variable to watch directly, or alternatively, pass the reference to the variable within $mockOb yourself. Having two things that access internals of $mockOb (Tk and $mockOb itself) certainly is bad design.

Replies are listed 'Best First'.
Re^2: Object Oriented reference in -textvariable
by JOption (Novice) on Aug 21, 2017 at 12:57 UTC
    I have complete control over $mockOb. It is a custom class created by me.

    I was trying to create a Form and a custom object to hold the values.

    I am more used to Java an the Object-Oriented design. Maybe my design is flawed.

      It depends on who actually is supposed to change the text. If only the user should change the text, instead of referencing the variable directly I would use the events on the<FocusOut> event to update the value in $mockOb manually:

      my $mockOb = SomeClass->new; my $search = $contentFrame->new_ttk__entry(-takefocus => 1, -font => ' +Arial 11', -width => 40, -validate => 'focusout', -textvariable =>\$ +mockOb->search, -validatecommand => [\&searchValidation, Tkx::Ev('%P +')] ); # Write information back to our $mockOb $search->bind('<FocusOut>', sub { $mockOb->search( $search->value )}); sub searchValidation{ ....do crazy stuff } SomeClass sub new { my $class = shift; my $self = { search = "" }; bless $self, $class; return $self; } sub search{ my $self = shift(); if(@_) { $self->{search} = shift(); } return $self->{search}; }

      But I haven't done Tk for a long time, so I don't know how applicable my idea of handling the focus event is, and if it is actually done the way that I wrote above untested code.

        I found a solution!

        Before

        -textvariable =>$mockOb->search sub search{ my $self = shift(); if(@_) { $self->{search} = shift(); $self->{search} =~ s/^\s+|\s+$//g; } return $self->{search}; }

        After

        -textvariable =>$mockOb->searchRef sub searchRef{ my $self = shift(); if(@_) { $self->{search} = shift(); $self->{search} =~ s/^\s+|\s+$//g; } return \$self->{handelspartnerSuche}; }

        I do not understand the solution completely, but it seems to work just fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1197733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found