3

I want to open a Tcl/Tk dialog on linux (kde,vnc) and put some information in:

package require Tk
toplevel .my
pack [label .my.l -text "hallo"]

Doing this, the dialog opens and the new dialog takes the focus.

How can I prevent this?

package require Tk
toplevel .my
<something with> .my
pack [label .my.l -text "hallo"]

I saw several question regarding the similar topic, but none for Tcl/Tk.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
tbasien
  • 87
  • 7
  • Where should the focus be instead? You can change the focus to another window with `focus .` (here I'm using `.` for example). Otherwise, I'm not sure why you would want to prevent focus. I found I actually *want* my sub-windows take focus when writing code for applications and it bugs me otherwise. – Jerry Jul 07 '15 at 13:24
  • It has more to do with the window manager than with Tk. Unfortunately, not many window managers prevent focus stealing (I read recently that Cinnamon supports this). I'd really love to have that feature. – Brad Lanam Jul 07 '15 at 16:10
  • @Jerry: lets say I have an XTERM and run a TK Gui, the focus is at the TK GUI and I have to focus back. – tbasien Jul 08 '15 at 06:30
  • If it is a Window Manager issue, can I get the current WindowsID infocus in the TK GUI, save it, open the GUI and refocus on the original ID? – tbasien Jul 08 '15 at 06:33
  • @tbasien Hmm, if you were on Windows, I would say you'd have to get the twapi package and then get the window handle of XTERM (e.g. if the window title is XTERM, then you use `set handle [::twapi::find_windows -text "XTERM"]` to get the window handle then `::twapi::show_window [lindex $handle 0]` to set the focus to that window). If there are multiple handles obtained, you'll need to refine the find. I'm not sure if there's a similar package for mac though :( – Jerry Jul 08 '15 at 07:07
  • @Jerry, my fault: Talking here on LINUX. – tbasien Jul 08 '15 at 09:26

2 Answers2

0

This is highly dependent on the window manager. For metacity and marco, there is a setting called 'focus-new-windows' that can be changed to 'strict'.

Mate with marco window manager:

gsettings set org.mate.Marco.general focus-new-windows strict

I don't use KDE, but you can try: System Settings -> Window Behaviour -> Focus and see if there's a setting there that will help.

Compiz has a focus prevention setting in General Options -> Focus.

Brad Lanam
  • 5,192
  • 2
  • 19
  • 29
0

Here is my solution:

 1  package require Tk

 2  proc leave {w e} {
 3      puts "gui left $w $e ..."
 4      if {$e != $w}  return
 5      focusHandler::restore

 6  }

 7  proc press {w} {
 8      puts "press $w"
 9      focusHandler::save
10      bind $w <Leave> focusHandler::restore
11      wm overrideredirect  [winfo toplevel $w] 0
12      raise  $w
13      focus -force $w
14      wm overrideredirect  [winfo toplevel $w] 1

15  }

16  namespace eval focusHandler {
17      variable helper
18  }
19  proc focusHandler::save {} {
20      variable helper
21      if ![info exists helper] {
22          puts "create helper"
23          set helper .xxx[clock clicks]
24          # create a new toplevel window
25          # this is controlled by the window manager
26          toplevel $helper
27          wm geometry $helper 0x0-1-1
28      }
29      puts "set focus of helper $helper"
30      raise $helper
31      update 
32      after 100
33  }
34  proc focusHandler::restore {} {
35      variable helper
36      if ![info exists helper] return
37      # destroy this toplevel and windows manager focus on the last know window
38      # which is not this gui, because of overrideredirect
39      destroy $helper
40      unset helper
41  }

42  wm withdraw .
43  toplevel .my
44  wm overrideredirect  .my 1
45  text .my.text  -height 10 -width 40
46  button .my.exit -command exit -text exit
47  bind .my.text <ButtonPress-1> [list press %W]
48  pack .my.text
49  pack .my.exit
tbasien
  • 87
  • 7