Getting text from TkText widget

I am at the rudimentary level in Ruby and am trying to learn Tk.

The attached program (tkalpha.rb) worked fine from the command line,
without the Tk code. All it should do is apply a mask/code to text, to
form a cryptogram, then save the cryptogram, and the code number, to a
file. Without the Tk code, each line of the resulting data file is
either a cryptogram or a code number. When I run the program with the Tk
code inserted, it adds either a blank line and a line with the code
number, but no cryptogram, or it adds the cryptogram, a blank line, the
code number, a blank line, and then a code number, which may or may not
be the same as the first code number.

I also attached the data file. After running tkalpha 12 times, it saved
the cryptogram only once, followed by a blank line, the code number, a
blank line, and then the code number. The other 11 times it saved only a
blank line and then the code number.

I tried .chomp, and .strip to eliminate blank lines, but no luck. It
seems the extra lines are introduced when the code number (fixnum) is
added to the file. I tried .to_s with .chomp and .strip to no avail.

I am using a Windows Vista platform, Ruby 2.0, and Tk 8.5.

There is no error message. I didn’t have far to go, but I am at my wit’s
end. Any insight or suggestions would be greatly appreciated.

7stud:

I was looking at the output by reading the text file in my text editor.
When I added p cgrams the screen display was much more difficult to
read, but I did discover one interesting thing. The print to screen
occurred before I typed the new cryptogram into the text widget and
before I pressed enter. So much for text.bind(“Enter”). I thought the
code block in {} would be executed only after the Enter key had been
pressed.

Roger Atkins wrote in post #1157349:

I am at the rudimentary level in Ruby and am trying to learn Tk.

Not a good idea. GUI programming is an intermediate topic.

The attached program (tkalpha.rb)

When you post to a forum, you should post the simplest possible code
that can demonstrate your problem. That means you start deleting lines.
For instance, all this:

alpha = [(‘a’…‘z’)]
upper = [
(‘A’…‘Z’)]
xchrs = "1234567890-=!@#$%^&*()_+<>,.;:?|][{}~`’ "
axchrs = xchrs.chars
allchr = alpha + upper + axchrs
masks = Hash.new

1.upto(25) {|k| masks[k] = alpha.rotate(k)}
masknum = rand(1…25)
mask = masks[masknum]

Can be replaced by:

mask = {
1 => ‘a’
2 => ‘b’
}

And when you have keys that are sequential numbers, that means you
should be using an Array.

worked fine from the command line,

Take your current code and add this line

File.readlines(‘cryptograms.txt’).each {|line| cgrams.push line}

p cgrams #HERE********

File.open(‘cryptograms.txt’, ‘w’) do |file|
cgrams.each {|cg| file.puts(cg)}
end

Then look in your terminal window for the output. That is known as
debugging. As a beginner you get better and better at debugging; and as
an intermediate you get pretty good at debugging. When you enter the
complex setting of a GUI program, you have to have some idea of how
things work, and be good at debugging, so that you know where to probe
to find your mistakes.

The output from that additional line should allow you to work backward
to find out why there is garbage in the cgrams array. It may just be
that you are using the same file for output and input, so if you write
garbage to the file, the next time you try to run the program,
it reads garbage in. So use two different files.

By the way, what do you think will happen if your system crashes a
moment after you open the file for writing? The file is
erased as soon as you open it for writing, you have the contents
of the file stored in a variable, and that variable goes poof. No
intermediate programmer would ever read the contents of a file, then
open the same file for writing because they know all the data could be
lost.

I can only infer that the code in {} associated with the text widget
executes every time the mouse pointer enters the application window
instead of just executing when the Enter key is pressed. I’m guessing
that the event handler must have defaults that operate unless turned
off, and that I need to learn more about event handling. However, if I
acknowledge that trying to write this program as a beginner is a bad
idea, as you said, then perhaps I should just call it quits. I spent
part of the day looking for better ways to proceed toward becoming a
programmer. I found a number of web sites and other resources, although
not much specifically related to Ruby. I’ll give them a try. Thanks for
your insight.

Roger Atkins wrote in post #1157385:

7stud:

I was looking at the output by reading the text file in my text editor.
When I added p cgrams the screen display was much more difficult to
read,

How’s that? This is what I’m using for an input file:

$ cat cryptograms_in.txt
hello
world

When you are debugging, you don’t use an input file that
is 100 lines long. For the output file, I’m using cryptograms_out.txt.
When I run your program, this is what I see in my terminal window:

~/ruby_programs$ ruby your_prog.rb
["\n", 17, “hello\n”, “world\n”]

That seems very uncluttered and very easy to read.

The print to screen
occurred before I typed the new cryptogram into the text widget and
before I pressed enter. So much for text.bind(“Enter”).

I want you to try something:

  1. Run your pogram with the ‘p cgrams’ line added to it.
  2. Click on your Tk window.
  3. Move your mouse outside the window.
  4. Move your mouse over the window.
  5. Repeat #3 and #4, entering and leaving the window while watching your
    terminal window.

How do you explain what you see?

There is a pretty good explanation of the Tk event names–in a python
tkinter tutorial–here:

Just note that in ruby, the event names do not have the angle brackets
around them. Event names in Tcl/Tk have angle brackets around them and
Python adopted that syntax. Ruby decided to drop the angle brackets.

Another option: learn Tk with Python instead of Ruby. The Python docs
are much better than the Ruby docs, and Tk (called tkinter) comes
installed with python, and there are numerous tkinter tutorials:

  1. The python docs: 25. Graphical User Interfaces with Tk — Python 3.4.10 documentation

  2. The effbot tutorial:
    http://effbot.org/tkinterbook/tkinter-index.htm#class-reference

  3. Tutorialspoint:
    Python - GUI Programming (Tkinter)

  4. The zetcode tutorial: http://zetcode.com/gui/tkinter/

  5. 1. Labels in Tkinter | Tkinter | python-course.eu

  6. http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

There is a actually a simplified Gui package for Ruby called Shoes

But Shoes depends on Java.

Do you really want him to go the Java route?

Roger Atkins wrote in post #1157450:

I can only infer that the code in {} associated with the text widget
executes every time the mouse pointer enters the application window

Hence, the event is named ‘Enter’ in Tk. There is a corresponding event
named ‘Leave’.

instead of just executing when the Enter key is pressed. I’m guessing
that the event handler must have defaults that operate unless turned
off,

There’s nothing to turn off. In Tk, you have to use the correct event
name if you want to listen for that event. Try this:

require ‘tk’

root = TkRoot.new
root.title = “MyWindow”
root[‘geometry’] = ‘500x300+100+50’

entry1 = TkText.new(root) do
width 40
height 10
end.pack()

entry1.bind(“Return”) do
puts ‘Return key was clicked’
end

Tk.mainloop

When you can’t get a program to work, you need to simplify things in
order to get something basic to work, then add to it.

and that I need to learn more about event handling. However, if I
acknowledge that trying to write this program as a beginner is a bad
idea, as you said, then perhaps I should just call it quits.

You might as well try to finish just to see if you can get something to
work. However, I would not recommend trying to learn Ruby by doing Gui
programming. And though Tk comes with ruby, it is a pain to get set up,
and there are really no docs for it. You have to go to the Tcl/Tk docs,
which will be very difficult for a beginner to understand.

There is a actually a simplified Gui package for Ruby called Shoes, so
if GUI programming is something that you are really interested in, you
might want to start with Shoes. There is a manual/documentation for it,
so at least you’ll have something to refer to. But then again, you got
pretty far with Tk, you just have to figure out the event names.

I spent
part of the day looking for better ways to proceed toward becoming a
programmer. I found a number of web sites and other resources,
although not much specifically related to Ruby.

If you search for “Ruby tutorial”, you will find a lot of stuff.
Personally, I prefer a good book. An old version of Programming Ruby is
available free online:

http://ruby-doc.com/docs/ProgrammingRuby/

But it would be better to get the latest edition of the book. And it’s
nice to have a physical book, so that you can scribble notes in the
margins.

Robert H. wrote in post #1157502:

There is a actually a simplified Gui package for Ruby called Shoes

But Shoes depends on Java.

Incorrect.

Programming Ruby, as I understand, is known as the Pickaxe. I have two
editions. I’ve read the narrative portions and some of it more than
once. The rest of the book is difficult for a beginner who does not have
command of the tech jargon and code used as reference material. The
Pragmatic Studio course on Ruby enabled me to make more progress, and it
was more extensive than the Code Academy course, but some of it was
difficult to follow.

There is an on-line Tk tutorial at TkDocs Tutorial that
I read in cursory fashion, and I have been going back through it, widget
by widget. My first idea was to create a Tk Reference. It would display
labeled widgets that, when activated, would display the configuration
options for the selected widget. I discovered that it is easy enough to
do it for buttons and other widgets that have ‘command’ as a
configuration option, but I needed to find a way to do it for other
widgets such as TkText, which is why I created tkalpha. I see now, that
I should have used an even simpler program.

Roger Atkins wrote in post #1157526:

Programming Ruby, as I understand, is known as the Pickaxe. I have two
editions. I’ve read the narrative portions and some of it more than
once. The rest of the book is difficult for a beginner who does not have
command of the tech jargon and code used as reference material.

You need to try another book. A good book is “Beginning Ruby: From
Novice to Professional” but it is was published in 2009. Another
excellent book is, “The Well Grounded Rubyist”. It just came out in a
new edition.

I would definitely try Shoes instead of Tk–just because there is a
manual for it. Tk is a fine Gui framework, but you have to be prepared
to use docs in another language if you are having issues, which is a
huge failure on the part of the Ruby community. Personally, I prefer the
wx Gui frameworks, but the wxRuby wiki is long gone, and wxRuby seems to
have fallen into a black hole.

I will definitely purchase both of those books. Thanks for the insight.