Opening a window that duplicates the program we're on

Running two copies of the program simultaneously or setting up two windows with one copy of the program running

Didier(Saturday February 5th 2011) I'd like to program my editor so that two copies of the program run at the same time: the first version of the Editor will occupy one half of the screen and the second one will occupy the other half. I am doing this so that I can copy and paste parts of documents more easily than if I open two copies of the program on two different windows. How should I proceed? Please note once again that I'd like the two programs to open at the same time on split screen. Tx!

RLE (2011-05-05) Your question appears to mix some metaphors. Do you really want two independent copies of the program, or are you just asking for a split-screen, side-by-side view of two documents? I.e., you want one big window, which contains two document views side by side? If so you should take a look at the ttk::panedwindow widget if you are running 8.5.x. You can put two text widgets inside of it side by side.

Or, you could just open two top level windows, put one text widget in each, and then using the wm command, adjust the sizes and positions of the two windows so that the each take up a half screen, and sit side by side.

Didier (2011-02-05) Tx RLE. I imperatively must open two copies of the same program for technical reasons and not simply two windows.

RLE (2011-02-05) fails to understand why such technical reasons exist, but suggests you take a look at How to embed a non-Tk GUI into a Tk frame which part way down mentions BLT containers among other ideas. You could also take a look at Embedding Windows applications in Tk frames. However I fear both will be suboptimal. You may simply find it easiest to either manually position and size both independent windows, or to include some form of command line argument or push button that sizes the windows appropriately. I.e., a -left/-right command line argument that causes the relevant window to be 1/2 screen on the left or right side, or a pair of buttons "Left" "Right" that reposition/size their parent toplevel to be 1/2 screen, left or right side.

Didier(2011-02-06) Tx again, RLE. Is there a simpler solution? What I want to do in fact is to load a precise page from Directory X on the left window and load another precise page from Directory Y on the right window.

My editor works on a subdirectory level with automatic saving and if I open two files that belong to two different directories each on its own window, I get a gigantic screw-up and this is why I need to open two versions of the program. Version A will handle file in directory X and Version B will handle file in directory Y.

I agree however that in most cases your solution works. But your solution would work with my editor only if both files belonged to the same directory. I have programmed what you suggest already.

What I really want to do is to start two copies of the program simultaneously each working in its own window. As I said, each will open a different file in a different directory.

I was thinking it would simply be a case of:

- a) Open Editor Version 1.exe and run it on left window

- b) Open Editor Version 2.exe and run it on right window

What's really needed is the capacity to work with two files located on two different directories thus avoiding the gigantic screw-up that will result if I don't plan things differently.

GJS You may simply try minimizing all windows but the two you want shown side by side, then right clicking the task bar. On vista there is an option labeled "Show Windows Side by Side". I don't know what OS you're using but it may have a similar option. While not being a TK solution it may be adequate.

Didier (2011-02-07) Thanks GJS. I am using Vista, believe it ot not. Actually what you are proposing is really the answer to my problem. If this works, I won't have to code anything! So I'm running to my other machine where Vista is located, I'll try your suggestion and I'll get back shortly to post an answer. Tx again.

A few minutes later: Gary, I know you're expecting a yes but answer but it just won't happen for your solution really, truly works A1. This is what I did:

1) I opened a first copy of the Editor. I minimized it.

2) I opened a second copy of the same program. I minimized it also.

3) I right clicked on "Show Windows Side by Side" like you told me to and both windows appeared side by side!

Wow! perfect solution! I have used this function in the past inadvertently but I never really fathomed its huge benefits.

Say what you will about Microsoft but they come up with great answers most of the time.

I remember a few years back I realized the huge benefits of the Start in function which is included in Shorcut properties for it lets you open any program in any directory except Notepad strangely! Maybe this has changed. It was the solution to my problem just like today. This Microsoft solution has saved me a lot of headaches.

So there we have an OS solution to a difficult problem.

RLE (2011-02-06): Are you writing this from scratch or are you attempting to morph someone else's code to fit your needs? I ask because there is no technical reason why, programmed correctly, something like what I outline below would not work (this is not all Tcl code, just an outline mostly):

create text widget .ew1
position .ew1 on left half of screen
create text widget .ew2
position .ew2 on right half of screen

load file X into .ew1;
remember file X's name somewhere, say ::state(.ew1-filename)
note: as an absolute path, [file normalize] and/or [tk_getOpenFile] are your friends;
load file Y (from a different directory, again as an absolute path) into .ew2;
remember file Y's name, say as ::state(.ew2-filename) (again, as an absolute path);

set up a pair of "after" timers
after $delay [ list savefile .ew1 $delay ]
after $delay [ list savefile .ew2 $delay ]

proc savefile { widget save_delay } {
  global state

  set fp [ open $state(${widget}-filename) {WRONLY TRUNC} ]
  puts -nonewline $fp [ $widget get 0.0 end ]
  close $fp

  after $save_delay [ list savefile $widget $save_delay ]
   
}

The result being that after every $delay milliseconds both text widgets overwrite the contents of the original file they contain, in the original directory that the file was loaded from. If what you want are versioned backups, that is a small change to the savefile proc that I will leave as an exercise.

Note, the "$widget get 0.0 end" command might make the file contents grow, in which case, replace "end" with "end-1c" to not save the final newline that the text widget contains.

And, voila, works with two different files, in two different directories, with no screw-ups.

I suspect you might be working with relative file names (i.e. "my_file.txt") instead of absolute file names (/home/dider/edit/files1/my_file.txt or c:\users\dider\Documents\files1\my_file.txt). If I understand what you are attempting to do and what has gone wrong correctly (telling us what the gigantic screw-up actually is would be helpful), you've been bitten by the fact that a relative filename is, well, "relative" and exactly what it points to on disk varies dependent upon the current working directory value. This is where file normalize can help, it will take a relative name and return the current absolute name that the relative name will reference.

Didier (2011-02-07) Thanks RLE. You're 100% right. The architecture of the program will have to be redone at one point so that it could work in two different directories; one directory in each window. I am not yet capable to program like a pro and I am not sure I ever will. For now I'll go with the easy Show windows side by side suggested by GJS. Tx for your help. I'll know what to do when I'll decide to rebuild the whole architecture of the Editor so that it can work in two different directories. I'll simply follow your outline for it is well thought of. Tx again for your help.

Problem solved. Tx!


Running a second copy of the program in a window

Steven, November 3rd 2009

Hi. I am in a TCL app and I'd like a second copy of the program to open and to run in a window. Is this possible, is this advisable? If so, how can this be done? The app is Notebook written by Bill Duquette. Thanks for the info.

AMG: One method: use exec. To run the second program in the background (i.e. not have the first wait for the second to terminate), give & as the last argument to exec.

Another method: check if the program supports multiple "logical instances" within a single process. If it keeps its state in global variables, it probably does not. But if it was designed to be used as a megawidget, it probably does. If multiple instances are possible, create a new toplevel and put it in there. Thanks to his Snit experience, WHD is more likely than most to design his programs as megawidgets. :^)