From: Morton Goldberg Date: 2007-08-18T09:54:25+09:00 Subject: Re: basic RubyTk questions On Aug 17, 2007, at 11:45 AM, Pete Siemsen wrote: > I have a Mac OS X system. I wrote a Ruby program that uses RubyTk to > display a TkLabel. It works, but I'd like a bit more control over how > it looks. Here's the code minus the non-Tk stuff that isn't relevant > to my questions: > > require 'tk' > > answer = 'test' > TkLabel.new do > text answer > foreground 'yellow' > background 'black' > # place('x' => 400, 'y' = 400) > pack('padx' => 15, 'pady' => 15) > end > > def timer_loop3 > exit > end > TkAfter.new(3000, # milliseconds > -1, # ? The -1 requests an indefinite number of timeout events. Since you are going to exit on the first timeout, I suggest using 1 here, but it doesn't really matter. > proc{timer_loop3} # what to call when the timer expires > ).start > > Tk.mainloop > > The program displays a label for 3 seconds and exits, as intended, > but... > > 1. How do I control the placement of the label on the screen? It > now apppears > near the upper left-hand corner. I tried "place" (commented > out) with no > luck. The 'place' method affects the geometry of a widget within its container. It does not affect the placement of the window containing the widget on the screen. > 2. How can I make the label appear on top of other windows? It now > appears > "under" other windows, so I only see the label if I keep the > upper-left part > of my screen free of other windows. The following ought to fix it, but it doesn't. root.focusmodel(:active) root.focus(true) This appears to be a problem with Tk. I don't know a work-around. > 3. Is there a way to remove the borders? I don't need the title bar. The borders, yes. The title bar I don't know how to remove. > 4. I use this as a Mac OS X service, defined with ThisService, a > great little > program. When I run it as a service, two windows appear. One > is titled "tk" > and the other is the same as what I see when I run the program > from the > command line. Can I get rid of the "tk" window? If you watch carefully, sometimes you will see this window appear momentarily even when you run from the command line. I don't know why it persists when you run your script as a service. > 5. Of course, is there a better way to do this? There's always a better way to do something with Ruby :) The following is the best I can do with Ruby/Tk. I don't know if you will think it's good enough, but it solves some of your problems. #! /usr/bin/ruby -w require 'tk' SEC = 3 # display duration in seconds TkLabel.new do text 'Hello, World!' font "HelveticaBold 36" foreground 'yellow' background 'black' pack(:fill => :both, :expand => true) end root = Tk.root root.title('') # suppress title (title bar remains) # Set initial window geometry; i.e., size and placement win_y, win_w, win_h = 50, 300, 80 win_x = (root.winfo_screenwidth - win_w) / 2 # center on screen root.geometry("#{win_w}x#{win_h}+#{win_x}+#{win_y}") # Suppress resizing window root.resizable(false, false) # One-shot timer TkTimer.new(SEC * 1000, 1) { root.destroy }.start Tk.mainloop This may be a situation where Ruby/Tk just won't cut it. The better way you are looking for may require using a GUI library other than Ruby/Tk. Regards, Morton