Ruby/Tk, what am I doing wrong... 
Author Message
 Ruby/Tk, what am I doing wrong...

Now I'm diving into Ruby/Tk and I have some newbie type questions...

Question 1: I'm creating a root window and then a subordinate TkToplevel.
The TkTopLevel is always drawn behind the root... I want it to start out in
front (changing the order of the display calls doesn't seem to matter).  How
can I change the Z-order of the windows?

Question 2: I'm seeing inconsistencies between different kinds of widget
invocation.  For example:


works, however...

TkEntry.new(entryFrame) {
        textvariable variable

Quote:
}

returns the following error:

/cygdrive/c/ruby/lib/ruby/1.6/tk.rb:2239:in `tk_trace_variable': requires
TkVariable given NilClass (ArgumentError)
        from /cygdrive/c/ruby/lib/ruby/1.6/tk.rb:2636:in `textvariable'

Similarly...


      pack('side' => 'left')

Quote:
}


      pack('side' => 'left')

Quote:
}

correctly puts the two side by side left to right, however


      pack {side 'left'}

Quote:
}


      pack {side 'left}

Quote:
}

puts the two items top to bottom.

The pickaxe book implies that in both cases the two forms should be
equivalent.  What am I doing wrong?

Thanks,

Noel Rappin



Sat, 26 Jul 2003 06:16:17 GMT  
 Ruby/Tk, what am I doing wrong...

Quote:

>TkEntry.new(entryFrame) {
>    textvariable variable
>}

I THINK the problem with this one is that it should be


   TkEntry.new(entryFrame, { textvariable variable })

I don't mean the layout :-) ... just the fact that hash needs to be
part of the call to TkEntry#new, not a block passed to it.

However, I'm pretty new to the Tk stuff, too.

Quote:
>Similarly...


>      pack('side' => 'left')
>}

>      pack('side' => 'left')
>}

Hmmm ... maybe what I said before was wrong.  I would have thought
you'd get the same problem here, but you say this bit works.  Of
course, it might just happen to work because the default is to do what
you expect.

Anyway, I hope that was helpful, rather than being a red herring!

Good luck.



Sat, 26 Jul 2003 13:02:02 GMT  
 Ruby/Tk, what am I doing wrong...

Quote:

>Now I'm diving into Ruby/Tk and I have some newbie type questions...

Welcome!  I hope you're not offended that a fellow newbie answers your
questions since I have myself come across most of these very things myself
in the past three weeks! =)

Quote:
>Question 1: I'm creating a root window and then a subordinate TkToplevel.
>The TkTopLevel is always drawn behind the root... I want it to start out in
>front (changing the order of the display calls doesn't seem to matter).  How
>can I change the Z-order of the windows?

I found using the "lower" method to the TkRoot window works well:

        rw = TkRoot.new
        mw = TkToplevel.new(rw)
        mw.lower
        Tk.mainloop

Quote:
>Question 2: I'm seeing inconsistencies between different kinds of widget
>invocation.  For example:



>works, however...


>TkEntry.new(entryFrame) {
>    textvariable variable
>}

>returns the following error:

>/cygdrive/c/ruby/lib/ruby/1.6/tk.rb:2239:in `tk_trace_variable': requires
>TkVariable given NilClass (ArgumentError)
>    from /cygdrive/c/ruby/lib/ruby/1.6/tk.rb:2636:in `textvariable'

This is because you didn't use the same variable:
In the first instance:





        >TkEntry.new(entryFrame) {
        >    textvariable variable
        >}


been assigned a value, it's value is implicitly 'nil', thus causing the
error you saw.  Now I know what you're thinking -- you've already tried
doing this:


        >TkEntry.new(entryFrame) {

        >}

And you got this error:

/usr/local/lib/ruby/1.6/tk.rb:2239:in `tk_trace_variable': requires TkVariable given NilClass (ArgumentError)

But that is becuase inside of the {} body of your TkEntry initialization,
the scope of method variables is for the TkEntry and _not_ for your enclosing
class!  That is why in the fully-parenthesized version it works:


but in the block-version it doesn't:

        >TkEntry.new(entryFrame) {

        >}

Fortunately, in my case, the problem was that I was not familiar with how
in ruby, all variables are references to an object, and not actually the
object itself.  It took me several hours before I got it into my head that
all variables are just references and that it doesn't matter what the variable
name *is*, only that it *is* in scope! =)  This is a somewhat different way
of approaching OOP than in, say, C++.  All you have to do here is create a
local variable -- which will be in scope!



        TkEntry.new(entryFrame) {
                textvariable localvar
        }

and, Viola!

- Show quoted text -

Quote:
>Similarly...


>      pack('side' => 'left')
>}

>      pack('side' => 'left')
>}

>correctly puts the two side by side left to right, however


>      pack {side 'left'}
>}

>      pack {side 'left}
>}

>puts the two items top to bottom.

This is because you can't nest the "block" notation with the "pack" method.
I don't have an explanation for why this doesn't work, but as far as I can
tell, it does not work.  I've just resigned myself to using the parenthesized
form.  What is happening is that the 'pack' method is being passed no
arguments at all, and so the default behavior (top to bottom packing) is
occuring.

Quote:
>The pickaxe book implies that in both cases the two forms should be
>equivalent.  What am I doing wrong?

Maybe someone will shed light on this last question and englighten both
of us. =)

Jeremy



Wed, 30 Jul 2003 06:49:21 GMT  
 Ruby/Tk, what am I doing wrong...
Jeremy, thank you very much for this -- it's a very clear and lucid
explanation of what's going on.  I only hope that I can do as good a job
passing the knowledge down to the next batch of newbies!.

Noel

Quote:

> >Now I'm diving into Ruby/Tk and I have some newbie type questions...

> Welcome!  I hope you're not offended that a fellow newbie answers your
> questions since I have myself come across most of these very things myself
> in the past three weeks! =)

> >Question 1: I'm creating a root window and then a subordinate TkToplevel.
> >The TkTopLevel is always drawn behind the root... I want it to
> start out in
> >front (changing the order of the display calls doesn't seem to
> matter).  How
> >can I change the Z-order of the windows?

> I found using the "lower" method to the TkRoot window works well:

>    rw = TkRoot.new
>    mw = TkToplevel.new(rw)
>    mw.lower
>    Tk.mainloop

> >Question 2: I'm seeing inconsistencies between different kinds of widget
> >invocation.  For example:



> >works, however...


> >TkEntry.new(entryFrame) {
> >       textvariable variable
> >}

> >returns the following error:

> >/cygdrive/c/ruby/lib/ruby/1.6/tk.rb:2239:in `tk_trace_variable': requires
> >TkVariable given NilClass (ArgumentError)
> >       from /cygdrive/c/ruby/lib/ruby/1.6/tk.rb:2636:in `textvariable'

> This is because you didn't use the same variable:
> In the first instance:





>    >TkEntry.new(entryFrame) {
>    >    textvariable variable
>    >}

> You are (implicitly) referencing 'variable' which is not the same as

> been assigned a value, it's value is implicitly 'nil', thus causing the
> error you saw.  Now I know what you're thinking -- you've already tried
> doing this:


>    >TkEntry.new(entryFrame) {

>    >}

> And you got this error:

> /usr/local/lib/ruby/1.6/tk.rb:2239:in `tk_trace_variable':
> requires TkVariable given NilClass (ArgumentError)

> But that is becuase inside of the {} body of your TkEntry initialization,
> the scope of method variables is for the TkEntry and _not_ for
> your enclosing
> class!  That is why in the fully-parenthesized version it works:


> but in the block-version it doesn't:

>    >TkEntry.new(entryFrame) {

>    >}

> Fortunately, in my case, the problem was that I was not familiar with how
> in ruby, all variables are references to an object, and not actually the
> object itself.  It took me several hours before I got it into my head that
> all variables are just references and that it doesn't matter what
> the variable
> name *is*, only that it *is* in scope! =)  This is a somewhat
> different way
> of approaching OOP than in, say, C++.  All you have to do here is
> create a
> local variable -- which will be in scope!



>    TkEntry.new(entryFrame) {
>            textvariable localvar
>    }

> and, Viola!

> >Similarly...


> >      pack('side' => 'left')
> >}

> >      pack('side' => 'left')
> >}

> >correctly puts the two side by side left to right, however


> >      pack {side 'left'}
> >}

> >      pack {side 'left}
> >}

> >puts the two items top to bottom.

> This is because you can't nest the "block" notation with the
> "pack" method.
> I don't have an explanation for why this doesn't work, but as far as I can
> tell, it does not work.  I've just resigned myself to using the
> parenthesized
> form.  What is happening is that the 'pack' method is being passed no
> arguments at all, and so the default behavior (top to bottom packing) is
> occuring.

> >The pickaxe book implies that in both cases the two forms should be
> >equivalent.  What am I doing wrong?

> Maybe someone will shed light on this last question and englighten both
> of us. =)

> Jeremy



Fri, 01 Aug 2003 23:19:13 GMT  
 Ruby/Tk, what am I doing wrong...
This, nesting blocks, was the cause of my confusion with grid also (see
02/14/2001 thread). Nested blocks keep putting the widgets top-to-bottom
rather than the grid pattern I was specifying.

I now notice that the pickaxe book uses parenthesis  for all of its examples
(Ruby Tk Chapter) except for the first very basic example.

Has anyone supplied a definitive answer to this question?

Quote:
> >Similarly...


> >      pack('side' => 'left')
> >}

> >      pack('side' => 'left')
> >}

> >correctly puts the two side by side left to right, however


> >      pack {side 'left'}
> >}

> >      pack {side 'left}
> >}

> >puts the two items top to bottom.

> This is because you can't nest the "block" notation with the "pack"
method.
> I don't have an explanation for why this doesn't work, but as far as I can
> tell, it does not work.  I've just resigned myself to using the
parenthesized
> form.  What is happening is that the 'pack' method is being passed no
> arguments at all, and so the default behavior (top to bottom packing) is
> occuring.

> >The pickaxe book implies that in both cases the two forms should be
> >equivalent.  What am I doing wrong?

> Maybe someone will shed light on this last question and englighten both
> of us. =)

> Jeremy



Wed, 06 Aug 2003 12:40:41 GMT  
 Ruby/Tk, what am I doing wrong...

j> Has anyone supplied a definitive answer to this question?

 Use #pack only with (), when you use it with a block {} it will *never*
call it

Guy Decoux



Wed, 06 Aug 2003 20:45:03 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. What am I doing wrong!

2. What am I doing wrong?

3. Stupid newb question: What am I doing wrong?

4. What am I doing Wrong

5. what am i doing wrong

6. Help - What am I doing wrong

7. Saving a Variable to a field in a record- what am i doing wrong

8. What am I doing wrong?

9. What am I doing wrong?

10. #CONTROL What am I doing wrong

11. What am i doing wrong.

12. What am I doing wrong?

 

 
Powered by phpBB® Forum Software