Multiple files/windows with Tk

D

DBH

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.
 
A

Andrew Thompson

DBH said:
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
 
H

Hidetoshi NAGAI

From: DBH <[email protected]>
Subject: Multiple files/windows with Tk
Date: Sun, 1 Apr 2007 09:52:48 +0900
Message-ID: said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,177
Messages
2,570,953
Members
47,507
Latest member
codeguru31

Latest Threads

Top