Multiple files/windows with Tk

I’m currently trying to write a Ruby program using Tk that requires the
use of multiple files and windows. For instance, I have a main screen,
and when I click on one of the buttons, I want the main window to close
and a new window to open. The code for these windows is located in
different files (but if possible I could put them all in one). So far
all I’ve been able to do is get the second window to attach itself onto
the first, resulting in a very ugly looking GUI. How can I get the Tk
interface to let me close one window and open another with a button
click or similar event?

Thanks in advance to any help anyone can offer.

DBH wrote:

I’m currently trying to write a Ruby program using Tk that requires the
use of multiple files and windows. For instance, I have a main screen,
and when I click on one of the buttons, I want the main window to close
and a new window to open. The code for these windows is located in
different files (but if possible I could put them all in one). So far
all I’ve been able to do is get the second window to attach itself onto
the first, resulting in a very ugly looking GUI. How can I get the Tk
interface to let me close one window and open another with a button
click or similar event?

Thanks in advance to any help anyone can offer.

There’s a couple approaches you can take. You can #withdraw the current
window and create a new TkToplevel and display it, or you can again
#withdraw the current window, unpack it’s contents, pack in some new
contents and redisplay the window (I don’t recall the method to
redisplay the window off-hand). I prefer the second approach in general,
but either should work fine. You should be able to do whatever you want
by passing a block to TkButton#command.

Hope that helps,

Andrew

From: DBH [email protected]
Subject: Multiple files/windows with Tk
Date: Sun, 1 Apr 2007 09:52:48 +0900
Message-ID: [email protected]

I’m currently trying to write a Ruby program using Tk that requires the
use of multiple files and windows. For instance, I have a main screen,
and when I click on one of the buttons, I want the main window to close
and a new window to open. The code for these windows is located in
different files (but if possible I could put them all in one). So far
(snip)

For example,

require ‘tk’

root = TkRoot.new
win = TkToplevel.new

win.withdraw

TkLabel.new(root, :text=>‘This is a root window.’).pack
TkButton.new(root, :text=>‘hide root’,
:command=>proc{root.withdraw; win.deiconify}).pack

TkLabel.new(win, :text=>‘This is a toplevel window.’).pack
TkButton.new(win, :text=>‘hide win’,
:command=>proc{win.withdraw; root.deiconify}).pack

Tk.mainloop

Hi DBH, It’s been since 03/2007 so you are probably no longer interested.
I found that I could use this code below to generate a button that opens an Openfile Dialog Box when clicked.
At this time I am only able to select one file and you can see the filename path that I print to the ruby command console.
I am still trying to discover how to use this tk gem to select and capture multiple files.
Anyone able to contribute to this stream and provide insight to ‘tk’, please do.

code:

        require 'tk'

	files = ""
	root = TkRoot.new
	root.title = "Select Files"

	button_click = Proc.new {
	   files = Tk.getOpenFile
	   # print filenames to ruby console.
	   puts files
	}

	button = TkButton.new(root) do
	   text "button"
	   pack("side" => "left",  "padx"=> "50", "pady"=> "50")
	end

	button.comman = button_click	
	Tk.mainloop