0

I'm using the wmctrl -lp to try and find the process id associated with a tkinter GUI window. However, it looks like the tkinter window is not associated with a pid.

0x06400010  0 0                     N/A tk

Using xprop, it looks like the _NET_WM_PID property is not set.

However, I can't find any way to get tkinter to set this automatically, or to set it explicitly from my python code.

The context is that I have a script that looks for any windows "owned" by a particular process, or any child processes thereof. But a tkinter window cannot be found by this script, because the script does not see it as being associated with the pid of the python script that creates the window.

Any help much appreciated.

EDIT: Further weirdness

It seems that this issue only arises if I run the python script from within a bash script.

So if I have a bash script gui_from_bash_script.sh which says:

#!/bin/bash

python my_gui.py

... and then I run ./gui_from_bash_script.sh, then I get the problem.

If I just run python my_gui.py from the terminal, I don't get the issue.

samfrances
  • 3,405
  • 3
  • 25
  • 40
  • 1
    ***Tkinter doesn't associate a window with a pid***: Why do you think `Tkinter` have to do so? – stovfl Jun 17 '20 at 14:01
  • Well, with most windows, if you run `xprop` against it you get a result for `_NET_WM_PID`. I don't really care whether tkinter does it, I just want to find a way to do it. – samfrances Jun 17 '20 at 14:03
  • ***...I have a script that looks for any windows "owned" by a particular process***: Show the relevant part of this script. – stovfl Jun 17 '20 at 14:11
  • Do you want to get the `pid` of tkinter window? – jizhihaoSAMA Jun 17 '20 at 14:15
  • @samfrances Can' t reproduce your behavior. I get on *linux* *lxde*: `_NET_WM_PID(CARDINAL) = 2540 ... WM_NAME(STRING) = "tk"` – stovfl Jun 17 '20 at 14:15
  • @jizhihaoSAMA yes, exactly. – samfrances Jun 17 '20 at 14:18
  • @stovfi I'm on ubuntu 16.04, if that makes a difference. – samfrances Jun 17 '20 at 14:18
  • ***if that makes a difference***: Only the used Window Manager could behave different. – stovfl Jun 17 '20 at 14:19
  • @stovfi Interestingly, if I run the python script from within a shell script, I get the issue. If I run it directly, I don't. So I don't think tkinter is the issue after all. – samfrances Jun 17 '20 at 14:20
  • PID (Process ID) is the ID of a process, not ID of a window. So PID is the ID of the Python process running a script which produces the tkinter window. – acw1668 Jun 17 '20 at 16:55

1 Answers1

0

I managed to resolve this by adding the following line when creating the root window.

root = tk.Tk()
root.client(socket.gethostname())   # THIS LINE HERE

I don't yet understand why this made the difference, but it appears to have done so.

wmctrl -lp now shows:

0x06400010  0 1234   my-host-name tk
samfrances
  • 3,405
  • 3
  • 25
  • 40