# Passing the data from a tkEntry widgit to a variable

**URL:** <https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006>\
**Category:** Ruby\
**Created:** [October 13, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006 "2006-10-13T15:46:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [October 13, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006/1 "2006-10-13T15:46:35Z")

</div>

I’m a bit confused about this,  
when you create a TkEntry Widgit on a frame  
How exactly do you put the data entered into a variable?

I want it passed when the user clicks a button.

So I was looking at examples and it seemed to say that along  
with passing the frame name, i should pass a textvariable linked  
too a variable. But thats just throwing errors.

I was looking at examples that seem to bind the data but i was  
having difficulty figuring out what they were passing.

Could I bother someone to explain it briefly. I want to be able to  
understand it to make sure I can do this.

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [October 13, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006/2 "2006-10-13T17:01:32Z")

</div>

Ok.  
Sample code for other newbies.

@mytext = TkVariable.new(‘hi this is a test’)  
testtwo = TkEntry.new(Frameone, ‘textvariable’=\> @mytext)  
{  
puts value  
}

Value outputs whats been passed to the TkEntry widgit.  
I think.

Binding I think will allow data into the widgit  
so possibility into the variable. But I havent got that far yet.

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [October 14, 2006, 3:16am UTC](https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006/3 "2006-10-14T03:16:51Z")

</div>

From: Mer G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Passing the data from a tkEntry widgit to a variable  
Date: Sat, 14 Oct 2006 00:01:34 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Sample code for other newbies.
> 
> @mytext = TkVariable.new(‘hi this is a test’)  
> testtwo = TkEntry.new(Frameone, ‘textvariable’=\> @mytext)  
> {  
> puts value  
> }
> 
> Value outputs whats been passed to the TkEntry widgit.  
> I think.

----\<sample 1\>--------------------------------------  
mytext = @mytext = TkVariable.new(‘hi this is a test’)  
testtwo = TkEntry.new(Frameone, ‘textvariable’=\> @mytext)  
{

# self is the TkEntry widget

## self.bind(‘Button-1’){puts mytext.value} }

## ----\<sample 2\>-------------------------------------- testtwo = TkEntry.new(Frameone) { self.value = ‘hi this is a test’ self.bind(‘Button-1’){puts self.value} }

## ----\<sample 3\>-------------------------------------- @mytext = ‘hi this is a test’ testtwo = TkEntry.new(Frameone) testtwo.value = @mytext testtwo.bind(‘Button-1’){@mytext = testtwo.value; puts @mytext}

## ----\<sample 4\>-------------------------------------- @mytext = ‘hi this is a test’ testtwo = TkEntry.new(Frameone) testtwo.value = @mytext callback = proc{@mytext = testtwo.value; puts @mytext} testtwo.bind(‘Button-1’, callback) testtwo.bind(‘Return’, callback)

## ----\<sample 5\>-------------------------------------- @mytext = ‘hi this is a test’ testtwo = TkEntry.new(Frameone) testtwo.value = @mytext ev = TkVirtualEvent.new(‘Button-1’, ‘Return’) testtwo.bind(ev){@mytext = testtwo.value; puts @mytext}

and so on.

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [October 17, 2006, 12:21am UTC](https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006/4 "2006-10-17T00:21:50Z")

</div>

On Oct 16, 2006, at 10:53 AM, Mer G. wrote:

> Trying your examples and think I understand them and what  
> it. But of course it doesnt.  
> Bind ‘ButtonRelease-1’ and its action to your button widget.  
> Supposing your button is referenced by the variable ‘my\_btn’, then  
> the following should work:

```
 my_btn.bind('ButtonRelease-1'){@mytext = testtwo.value; puts

```

@mytext}

I prefer handling a ‘ButtonRelease-1’ event to a ‘Button-1’ (an alias  
for ‘ButtonPress-1’) because it gives the user a chance withdraw from  
committing to a widget action to by clicking off the widget in question.

If you need to bind more than one event to the button, then use a  
virtual event as Nagai san demonstrated.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [October 16, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/passing-the-data-from-a-tkentry-widgit-to-a-variable/72006/5 "2006-10-16T16:53:37Z")

</div>

Hidetoshi NAGAI wrote:

> ## ----\<sample 5\>-------------------------------------- @mytext = ‘hi this is a test’ testtwo = TkEntry.new(Frameone) testtwo.value = @mytext ev = TkVirtualEvent.new(‘Button-1’, ‘Return’) testtwo.bind(ev){@mytext = testtwo.value; puts @mytext}
> 
> and so on.

Trying your examples and think I understand them and what  
they are doing. I have a much better idea of how to use  
tkvirtual event anyway.

But The data in the entry box goes into the  
variable as soon as I click on the entry box. Not when i click  
on the button. Button-1 means left click so I see why this is  
so.

How do I change the action to clicking on a button?  
I was wondering if the button name instead of button-1 might do  
it. But of course it doesnt.
