# Ruby/Tk code critique

**URL:** <https://www.ruby-forum.com/t/ruby-tk-code-critique/100418>\
**Category:** Ruby\
**Created:** [July 28, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/ruby-tk-code-critique/100418 "2006-07-28T21:07:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rube](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@rube](https://www.ruby-forum.com/u/rube)\
**Post date:** [July 28, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/ruby-tk-code-critique/100418/1 "2006-07-28T21:07:31Z")

</div>

I have a bunch of Tk buttons tied to code. They’re wrapped in an array  
to process later.

What I’d like to do is set each one to have a visual indicator that it  
was used. So after it’s clicked, it gets configured to have relief  
“flat” or background “yellow” etc.

And then later, when a new row is being processed, I can iterate thru  
button\_array and reset their color/relief etc. – this is to aid the  
user in knowing which buttons they’ve already used.

The simple/ugly way I suppose would be to make each button be like:  
TkButton.new(frame) {  
text “bleh”  
command proc {  
self.configure(“relief”,“flat”)  
run\_code(:buttoncode)  
}  
}

What I’m looking for is basically an elegant way to do this. Rather than  
modify each proc to add code to change the relief, I’d like an ‘around’  
method to put around run\_code. Or in run\_code if I could identify the  
button that was clicked.

#Random status label changes  
pru=proc{@status.configure(‘text’=\>“Processing…”)}  
prr=proc{@status.configure(‘text’=\>“Ready…”)}

```
	## Proc for ETL tasks
	retl = proc{run_code(:etl)}
	etlre = proc{run_code(:etlreuse)}
	idsre = proc{run_code(:idsreuse)}
	edwre = proc{run_code(:edwreuse)}
	sorre = proc{run_code(:sorreuse)}
	#Buttons - ETL reuse
	lxxs_4 = TkButton.new(reusefr) { text "Reuse-ETL"; command etlre}
	lxxs_5 = TkButton.new(reusefr) { text "Reuse-IDS"; command idsre}
	lxxs_6 = TkButton.new(reusefr) { text "Reuse-EDW"; command edwre}
	lxxs_7 = TkButton.new(reusefr) { text "Reuse-SOR"; command sorre}

```

button\_array = [lxxs\_4, lxxs\_5 etc.]

Incidentally, run\_code looks like this:  
def run\_code(par)  
@controller.dispatcher(par)  
end

---

<div class="post-metadata">

**Author:** ![rube](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@rube](https://www.ruby-forum.com/u/rube)\
**Post date:** [July 29, 2006, 12:46am UTC](https://www.ruby-forum.com/t/ruby-tk-code-critique/100418/2 "2006-07-29T00:46:57Z")

</div>

Probably, using `bindtag’ is not so bad idea.

From: rubist \_\_ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Ruby/Tk code critique  
Date: Sat, 29 Jul 2006 04:07:46 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> button\_array = [lxxs\_4, lxxs\_5 etc.]

btag = TkBindTag.new  
btag.bind(‘ButtonRelease-1’, proc{|w| w[‘relief’] = ‘flat’}, ‘%W’)  
button\_array.each{|b| b.bindtags\_unshift(btag) }
