Forum: Ruby More tk stuff

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-12-02 22:25
(Received via mailing list)
OK, I dug up an answer to my previous question; here's code to
implement a window with scrollbars:

require 'tk'

root = TkRoot.new() { title "(Sc)rolling, (sc)rolling),
(sc)rolling..." }
xbar = TkScrollbar.new(root, 'orient'=>'h').pack('side'=>'bottom',
'fill'=>'x')
ybar = TkScrollbar.new(root, 'orient'=>'v').pack('side'=>'right',
'fill'=>'y')
text = TkText.new(root, 'wrap'=>'none',
'width'=>20).pack('fill'=>'both', 'expand'=>true)
text.insert('end', "A string that is longer than fits on one line...")
xbar.command(proc {|*args| text.xview(*args)})
text.xscrollcommand(proc {|first, last| xbar.set(first, last)})
ybar.command(proc {|*args| text.yview(*args)})
text.yscrollcommand(proc {|first, last| ybar.set(first, last)})

Tk.mainloop


Next question; how does one access the new 'themed' widgets in the
modern releases of Tk? Something like ttk::entry, I mean; I assume the
double colon wouldn't be used in Ruby (and I certainly couldn't get it
to work.)

Thanks,
Ken
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2008-12-03 00:04
(Received via mailing list)
From: Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net>
Subject: More tk stuff
Date: Wed, 3 Dec 2008 06:19:04 +0900
Message-ID: <09DB2CED-6766-47E4-AF0C-55253E4C425C@sbcglobal.net>
> OK, I dug up an answer to my previous question; here's code to
> implement a window with scrollbars:

Ruby/Tk supports a more simple way to combine a scrollbar and
its target widget.
The following part of your script
---------------------------------------------------------
> text.yscrollcommand(proc {|first, last| ybar.set(first, last)})
---------------------------------------------------------
can rewrite to the following.
---------------------------------------------------------
text = TkText.new(root, :wrap=>'none', :width=>20).pack(:fill=>'both',
:expand=>true)
text.insert(:end, "A string that is longer than fits on one line...")
xbar = text.xscrollbar(TkScrollbar.new(root).pack(:side=>'bottom',
:fill=>'x'))
ybar = text.yscrollbar(TkScrollbar.new(root).pack(:side=>'right',
:fill=>'y'))
---------------------------------------------------------

> Next question; how does one access the new 'themed' widgets in the
> modern releases of Tk? Something like ttk::entry, I mean; I assume the
> double colon wouldn't be used in Ruby (and I certainly couldn't get it
> to work.)

Please require 'tkextlib/tile', and you can use Tk::Ttk::Entry and so
on.
The latest version of Ruby/Tk (e.g. Ruby 1.8.7) includes
a sample script 'ttk_wrapper.rb' at 'ext/tk/sample' directory
to use Ttk widgets as default.
For example, if you call "ruby ttk_wrapper.rb your_rubytk_script.rb",
widget classes on 'your_rubytk_script.rb' (e.g. TkEntry) means
the ones on Tk::Ttk widgets (e.g. Tk::Ttk::Entry).
It uses 'Tk.default_widget_set=' method.
When call "Tk.default_widget_set = :Ttk", standard widget class names
denote the ones of ttk widgets.
Then, if you want to use original entry widgets of Tk,
Tk::Entry denotes the original one.
You can switch the default to original by "Tk.default_widget_set = :Tk",
whenever you want.
This topic is locked and can not be replied to.