Python Window Activation - python

How would I programmatically activate a window in Windows using Python? I'm sending keystrokes to it and at the moment I'm just making sure it's the last application used then sending the keystroke Alt+Tab to switch over to it from the DOS console. Is there a better way (since I've learned by experience that this way is by no means foolproof)?

You can use the win32gui module to do that. First you need to get a valid handle on your window. You can use the win32gui.FindWindow if you know the window class name or the exact title. If not, you can enumerate the windows with the win32gui.EnumWindows and try to find the right one.
Once you have the handle, you can call the win32gui.SetForegroundWindow with the handle. It will activate the window and will be ready for getting your keystrokes.
See an example below. I hope it helps
import win32gui
import re
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
"""Pass to win32gui.EnumWindows() to check all the opened windows"""
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
w = WindowMgr()
w.find_window_wildcard(".*Hello.*")
w.set_foreground()

Pywinauto and SWAPY will probably require the least effort to set the focus of a window.
Use SWAPY to auto-generate the python code necessary to retrieve the window object, e.g.:
import pywinauto
# SWAPY will record the title and class of the window you want activated
app = pywinauto.application.Application()
t, c = u'WINDOW SWAPY RECORDS', u'CLASS SWAPY RECORDS'
handle = pywinauto.findwindows.find_windows(title=t, class_name=c)[0]
# SWAPY will also get the window
window = app.window_(handle=handle)
# this here is the only line of code you actually write (SWAPY recorded the rest)
window.SetFocus()
If by chance other windows are in front of the window of interest, not a problem. This additional code or this will make sure it is shown before running the above code:
# minimize then maximize to bring this window in front of all others
window.Minimize()
window.Maximize()
# now you can set its focus
window.SetFocus()

import ctypes, platform
if platform.system() == 'Windows':
Active_W = ctypes.windll.user32.GetActiveWindow()
ctypes.windll.user32.SetWindowPos(Active_W,0,0,0,0,0,0x0002|0x0001)
Here we go. you just need to store the value of the active window.

Pip install keyboard.
Before you set foreground window, simulate a keyboard to esc that is keyboard.send('esc')
You may want to do it three times for either of the following:
Sidebar
Windows key overlay
Task manager which is always on top

Using SetWindowPos or SetForegroundWindow might NOT be enough if the window was minified aka IsIconic! We can use ShowWindow with SW_RESTORE (9):
import ctypes
def activate_window(hwnd):
user32 = ctypes.windll.user32
user32.SetForegroundWindow(hwnd)
if user32.IsIconic(hwnd):
user32.ShowWindow(hwnd, 9)
Depending on how you identify the desired window there are some ways to get the hwnd aka window handle.
You could loop through all the windows to find the right handle according to the pid via user32.GetWindowThreadProcessId or by window name with user32.GetWindowTextW
To get ProcessIds you could use Windows built-in wmic. There are loads of other nifty things you can do with it. (all attributes: wmic process get /?) (get handle is broken tho) For example:
def get_pids(proc_name):
out = subprocess.check_output('wmic process where Name="%s" get ProcessId' % proc_name)
pids = out.decode().strip().split()[1:]
if not pids:
raise WindowsError('Could not find pids for process')
return [int(pid) for pid in pids]

GUI Application to keep windows active
Python3
install library
pip install pywin32
save below code as alive.pyw file
from ctypes import windll, wintypes, byref, c_uint, sizeof, Structure
import tkinter as tk
import ctypes
import sys
import threading
import time
import win32api
import win32con
stop_threads = True
SET_IDLE_TIME = 40 #in seconds
tm1 = time.time()
value = 0
class LASTINPUTINFO(Structure):
_fields_ = [
('cbSize', c_uint),
('dwTime', c_uint),
]
def get_idle_duration():
global value, tm1
lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = sizeof(lastInputInfo)
windll.user32.GetLastInputInfo(byref(lastInputInfo))
# millis = 4294967 - lastInputInfo.dwTime - windll.kernel32.GetTickCount()
# print(windll.kernel32.GetTickCount(), lastInputInfo.dwTime, sizeof(lastInputInfo), millis)
tm2 = time.time() - tm1
last_idel_time = lastInputInfo.dwTime
# print()
if value != last_idel_time:
value = last_idel_time
tm2 = time.time() - time.time()
tm1 = time.time()
# print("time:", tm1)
return tm2
def press_key_2():
global stop_threads, tm1
while True:
if not stop_threads:
break
idle_time = get_idle_duration() #seconds
# print(idle_time)
g = float("{:.2f}".format(idle_time))
st = str(g) + " / " + str(SET_IDLE_TIME)
var.set(st)
time.sleep(0.1)
if idle_time < SET_IDLE_TIME:
continue
print("in ideal state pressing cltr")
win32api.keybd_event(ord('x'), 0, win32con.KEYEVENTF_EXTENDEDKEY, 0)
tm1 = time.time()
#---------------- Monitor threads ------------------------------
t1 = threading.Thread(target=press_key_2, name='t1')
t1.daemon = True
#----------------- TK functions ----------------------
def display_on():
global tk, t1, stop_threads
stop_threads = True
print("Always On")
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
root.iconify()
t1.start()
# t2.start()
def display_reset():
print("quit pressed")
global stop_threads
stop_threads = False
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
sys.exit(0)
root = tk.Tk()
root.geometry("200x110")
root.title("Devil")
frame = tk.Frame(root)
frame.pack()
var = tk.StringVar()
var_idle = tk.StringVar()
label = tk.Label(frame, textvariable = var)#, bd = 5, justify = tk.RIGHT, padx = 10, pady = 10)
label_idle = tk.Label(frame,textvariable = var_idle)
var_idle.set("Idle Time")
var.set("-")
button = tk.Button(frame,
text="Quit",
fg="red",
command=display_reset)
slogan = tk.Button(frame,
text="Always ON",
command=display_on)
label_idle.pack(side=tk.BOTTOM,padx=15, pady=13)
label.pack(side=tk.BOTTOM,padx=15, pady=5)
slogan.pack(side=tk.LEFT,padx=15, pady=5)
button.pack(side=tk.LEFT,padx=15, pady=5)
root.mainloop()
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)

To add on #luc's answer, following is how the code would be more verbose about the handle it selected when multiple windows exist:
After pip install pywin32, run
import win32gui
import re
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
self._handles = []
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
"""Pass to win32gui.EnumWindows() to check all the opened windows"""
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handles.append(hwnd)
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
self._handles = []
win32gui.EnumWindows(self._window_enum_callback, wildcard)
self.set_handle()
def set_foreground(self):
"""put the window in the foreground"""
if self._handle != None:
win32gui.SetForegroundWindow(self._handle)
else:
print("No handle is selected, couldn't set focus")
def set_handle(self):
"""get one handle to operate on from all the matched handles"""
if len(self._handles) < 1:
print("Matched no window")
return False
if len(self._handles) > 1:
print("Selecting the first handle of multiple windows:")
else: # len(self._handles) == 1:
print("Matched a single window:")
self.print_matches()
self._handle = self._handles[0]
return True
def print_matches(self):
"""print the title of each matched handle"""
for hwnd in self._handles:
print("- " + str(win32gui.GetWindowText(hwnd)))
w = WindowMgr()
w.find_window_wildcard(".*Hello.*")
w.set_foreground()
Note: I couldn't make the addition by editing #luc's answer as its suggested edit queue is full.

Related

Tkinter GUI freezes while running looping

I'm new to python coding and I have been working on a project which could click on an image based on a chosen color. I have been using a program which loops the search 50 times when I click the start button. However, I have been trying to implement a stop button, but the problem is that my code freezes when the loop is running. Any ideas?
I have heard to try threading but it seems very complicated and I have been unable to follow any tutorials properly in relation to my code. By the way, the image searched has been testing images I've been using stored inside the program files.
from imagesearch import *
import pyautogui
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import time
import threading
# ---Defined Programs---
def run():
global enterColor
enterColor = str(enterColorField.get())
program(enterColor)
def program(color):
whitePos = imagesearch_numLoop(str(color) + ".PNG", 0, 50)
pyautogui.moveTo(whitePos[0] + 20, whitePos[1] + 10)
pyautogui.click()
def stop():
print("Placeholder")
# ---Main Runner---
window = tk.Tk()
window.geometry("250x250")
window.configure(background="#181b54")
app = tk.Frame(window)
app.grid()
enterColorLabel = tk.Label(window, text="Enter Color:", bg="#181b54", fg="white")
enterColorLabel.place(x=10, y=50)
enterColorField = Combobox(window)
enterColorField['values'] = ("Black", "White")
enterColorField.current("0") # set the selected item
enterColorField.place(x=10, y=70)
submitButton = tk.Button(window, text="Start", bg="#66ff00", command=run)
submitButton.place(x=10, y=130)
stopButton = tk.Button(window, text="Stop", bg="red", command=stop)
stopButton.place(x=50, y=130)
window.mainloop()
#---New Python Script---
import cv2
import numpy as np
import pyautogui
import random
import time
def imagesearch_numLoop(image, timesample, maxSamples, precision=0.8):
pos = imagesearch(image, precision)
count = 0
while pos[0] == -1:
print(image+" not found, waiting")
count = count + 1
if count>maxSamples:
break
pos = imagesearch(image, precision)
return pos
Whenever clicking start, the whole code freezes. I can't even (x) out.
Here's a hopefully simple multiprocessing recipe that will work for you. We'll have three main functions. The first will be an example loop that you would put your processing inside. I included arguments in the function to show you that it's possible to pass args and kwargs while using multiprocessing.
def loop(a, b, c, d):
# Will just sleep for 3 seconds.. simulates whatever processing you do.
time.sleep(3)
return
Next is a function we will use to queue the multiprocessing process.
def queue_loop():
p = multiprocessing.Process(target = loop,
args = (1, 2),
kwargs = {"c": 3, "d": 4})
# You can pass args and kwargs to the target function like that
# Note that the process isn't started yet. You call p.start() to activate it.
p.start()
check_status(p) # This is the next function we'll define.
return
Then, you may be interested in knowing the status of your process throughout its execution. For example it is sometimes desirable to disable certain buttons while a command is being run.
def check_status(p):
""" p is the multiprocessing.Process object """
if p.is_alive(): # Then the process is still running
label.config(text = "MP Running")
mp_button.config(state = "disabled")
not_mp_button.config(state = "disabled")
root.after(200, lambda p=p: check_status(p)) # After 200 ms, it will check the status again.
else:
label.config(text = "MP Not Running")
mp_button.config(state = "normal")
not_mp_button.config(state = "normal")
return
Throwing this all together into one snippet:
import tkinter as tk
import multiprocessing
import time
def loop(a, b, c, d):
# Will just sleep for 3 seconds.. simulates whatever processing you do.
time.sleep(3)
return
def queue_loop():
p = multiprocessing.Process(target = loop,
args = (1, 2),
kwargs = {"c": 3, "d": 4})
# You can pass args and kwargs to the target function like that
# Note that the process isn't started yet. You call p.start() to activate it.
p.start()
check_status(p) # This is the next function we'll define.
return
def check_status(p):
""" p is the multiprocessing.Process object """
if p.is_alive(): # Then the process is still running
label.config(text = "MP Running")
mp_button.config(state = "disabled")
not_mp_button.config(state = "disabled")
root.after(200, lambda p=p: check_status(p)) # After 200 ms, it will check the status again.
else:
label.config(text = "MP Not Running")
mp_button.config(state = "normal")
not_mp_button.config(state = "normal")
return
if __name__ == "__main__":
root = tk.Tk()
mp_button = tk.Button(master = root, text = "Using MP", command = queue_loop)
mp_button.pack()
label = tk.Label(master = root, text = "MP Not Running")
label.pack()
not_mp_button = tk.Button(master = root, text = "Not MP", command = lambda: loop(1,2,3,4))
not_mp_button.pack()
root.mainloop()
The result is that when you click the "Using MP" button, the command buttons will be disabled and the process will be started without freezing your UI. Clicking the "Not MP" button will start the function like 'normal' and will freeze your UI as you noticed in your own code.
A simple answer is you cannot use while loop in GUI design.
But you can use the method .after(delay, callback=None) instead.
Here is an example:
from tkinter import *
root = Tk()
def loop():
print("Hi!")
root.after(1000, loop) # 1000 is equal to 1 second.
root.after(1000, loop) # This line is to call loop() in 1 second.
root.mainloop()

Using keyword or wake word to activate Tkinter window in python

I found this amazing wake-word program called Porcupine. I got it to work and want to implement it into a Tkinter window. The idea is to have a state of on/off with the wake word turning it on. I have working code but the problem is that no matter where I put the listener in the code it will not make the window until I use the wake word.
I want the following behavior: I would like the window to be created and to appear and then to run the wake-word module so I can change the self.state variable. This wake word will then put me in a loop that will create other frames and features. I am assuming there is a threading solution but I could not figure it out.
from Tkinter import *
import threading
import ttk
import os
import sys
sys.path.append('Porcupine/demo/python')
import porcupine__demo
class FullscreenWindow:
def __init__(self):
self.state = False
self.tk = Tk()
self.tk.configure(background='black')
self.listen()
print(self.state)
def listen(self):
self.state = porcupine_demo.listen_for_keyword()
if __name__ == '__main__':
w = FullscreenWindow()
w.tk.mainloop()
The method *.listen_for_keyword() is a method I wrote that returns True when the wake word is captured.
I found an amazing blog post that answers this question. I have changed their code to reflect my needs. The blog is here:
from Tkinter import *
import threading
import sys
sys.path.append('Porcupine/demo/python')
import porcupine_demo
class App(threading.Thread):
def __init__(self, tk_root):
self.root = tk_root
threading.Thread.__init__(self)
self.start()
def run(self):
listening = True
while listening:
self.state = porcupine_demo.listen_for_keyword()
if self.state:
print("heard you:",self.state)
LABEL = Label(self.root, text="Hello, world!")
LABEL.pack()
ROOT = Tk()
ROOT.configure(background='black')
APP = App(ROOT)
ROOT.mainloop()
The porcupine package comes with a demo of how to use porcupine in a non-blocking mode, which is necessary to allow mainloop to process tkinter events. The key is to attach a callback to the audio stream.
I've never used porcupine before so I don't know how well this works in the real world, but it works for me on my mac, and doesn't require explicit threading.
import sys
sys.path.append("/tmp/Porcupine/binding/python")
import Tkinter as tk
import struct
from datetime import datetime
from porcupine import Porcupine
import pyaudio
library_path = "/tmp/Porcupine/lib/mac/x86_64/libpv_porcupine.dylib"
model_file_path = "/tmp/Porcupine/lib/common/porcupine_params.pv"
keyword_file_paths = ["/tmp/Porcupine/words_mac.ppn",]
num_keywords = len(keyword_file_paths)
sensitivities = [0.5] * num_keywords
class TkPorcupine(object):
def __init__(self):
self.initialize_ui()
self.initialize_porcupine()
def initialize_ui(self):
self.state = False
self.root = tk.Tk()
self.text = tk.Text(self.root, width=60, height=8)
self.vsb = tk.Scrollbar(self.root, orient="vertical", command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.text.pack(fill="both", expand=True)
def initialize_porcupine(self):
self.porcupine = Porcupine(library_path, model_file_path,
keyword_file_paths=keyword_file_paths,
sensitivities=sensitivities)
self.audio_stream = pyaudio.PyAudio().open(
rate=self.porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=self.porcupine.frame_length,
input_device_index=None,
stream_callback=self.audio_callback)
self.audio_stream.start_stream()
def audio_callback(self, in_data, frame_count, time_info, status):
if frame_count >= self.porcupine.frame_length:
pcm = struct.unpack_from("h" * self.porcupine.frame_length, in_data)
result = self.porcupine.process(pcm)
if num_keywords == 1 and result:
message = '[%s] detected keyword' % str(datetime.now())
self.text.insert("end", message + "\n")
self.text.see("end")
elif num_keywords > 1 and result >= 0:
message = '[%s] detected keyword #%d' % (str(datetime.now()), result)
self.text.insert("end", message + "\n")
self.text.see("end")
return None, pyaudio.paContinue
if __name__ == '__main__':
demo = TkPorcupine()
tk.mainloop()

Tix/TkInter, FolderDialog won't stay open when clicked

I realize that the first suggestion will be to "stop using Tix", but I like some of the widgets even though they haven't been maintained since '08. One thing I've noticed is that some of the dialog boxes won't stay open. For instance I'm using a FileEntry Widget inside of a LabelFrame Widget inside of the notebook widget. The file dialog looks as follows.
And when you click on the file dialog button you get:
The red arrow shows the drop down to files in that filter, but nothing happens when I click it. You can see a brief flash (like in the milliseconds) like an event loop was checked or something but then nothing. The same with the other buttons on FileEntry.
For the full code to this, you can see here
I think the relevant parts are:
import os, os.path, sys, Tix
from Tkconstants import *
import tkFileDialog
import traceback, tkMessageBox
from Tkinter import *
class pyigblast_gui():
def __init__(self,root):
#Initialization
self.root = root
self.exit = -1
self.dir = None
self.argument_dict = {'query':''}
#local
_program_name = sys.argv[0]
_directory_name = os.path.dirname(_program_name)
def MainMenu(self):
main_menu = Tix.Frame(self.root,bd=2,relief=RAISED)
return main_menu
def TabNotebook(self):
notebook_frame = self.root
notebook = Tix.NoteBook(notebook_frame, ipadx=5, ipady=5, bg='black')
notebook.add('f_and_d', label="Files and Databases", underline=0,
createcmd=lambda self=self,nb=notebook,name='f_and_d': self.files_and_directories(nb,name))
notebook.add('readme', label="Usage", underline=0,
createcmd=lambda self=self,nb=notebook,name='readme': self.readme(nb,name) )
return notebook
def files_and_directories(self,nb,name):
f_and_d_page = nb.page(name)
options = "label.padX4"
self.input_frame = Tix.LabelFrame(f_and_d_page,options=options)
self.input_frame.pack(side=TOP,expand=0,fill=BOTH)
self.make_fasta_entry()
#self.input_frame.grid(in_=f_and_d_page,row=0,column=0,columnspan=2)
def make_fasta_entry(self):
message = Tix.Message(self.input_frame,relief=Tix.FLAT, width=500, anchor=W,
text='Enter the entry FASTA file here',font=('Arial',16))
self.fasta_entry = Tix.FileEntry(self.input_frame, label="Select a FASTA file:",selectmode="normal")
message.pack(side=TOP,expand=1,fill=BOTH,padx=3,pady=3)
self.fasta_entry.pack(side=TOP,fill=X,padx=3,pady=3)
def build(self):
window_info = self.root.winfo_toplevel()
window_info.wm_title('PyIgBLAST - GUI')
#if window_info <= 800:
window_info.geometry('1500x900+10+10')
frame1 = self.MainMenu()
frame1.pack(side=BOTTOM, fill=X)
frame2 = self.TabNotebook()
frame2.pack(side=TOP,expand=1,fill=BOTH,padx=5,pady=5)
window_info.wm_protocol("WM_DELETE_WINDOW", lambda self=self:self.quitcmd())
def loop(self):
while self.exit < 0:
# There are 2 whiles here. The outer one lets you continue
# after a ^C interrupt.
try:
# This is the replacement for _tkinter mainloop()
# It blocks waiting for the next Tcl event using select.
while self.exit < 0:
self.root.tk.dooneevent(0)
except SystemExit:
# Tkinter uses SystemExit to exit
self.exit = 1
return
except KeyboardInterrupt:
if tkMessageBox.askquestion ('Interrupt', 'Really Quit?') == 'yes':
# self.tk.eval('exit')
self.exit = 1
return
continue
except:
# Otherwise it's some other error - be nice and say why
t, v, tb = sys.exc_info()
text = ""
for line in traceback.format_exception(t,v,tb):
text += line + '\n'
try: tkMessageBox.showerror ('Error', text)
except: pass
self.exit = 1
raise SystemExit, 1
def destroy(self):
self.root.destroy()
if __name__ == '__main__':
root = Tix.Tk()
pyigblast_class = pyigblast_gui(root)
pyigblast_class.build()
pyigblast_class.loop()
pyigblast_class.destroy()
Also in a seperate but unrelated warning given by Tix, I get this output to the terminal.
(TixForm) Error:Trying to use more than one geometry
manager for the same master window.
Giving up after 50 iterations.
If anyone can tell me about what I need to change with Tix to keep the dialog open and/or why it says I'm using two geometry managers I would appreciate it!
Thanks,
J
Ok, shine that noise.
The ttk solution is so much cleaner and more customizable. Here is a very elegant solution recapitulating this exact file dialog, but is much cleaner using ttk.
http://www.pyinmyeye.com/2012/08/tkinter-filedialog-demo.html

How to refresh the tree view using python?

I am new to python and gui programming.I am writing a program that will list all the directories in root(/) and all the sub-directories in these directories.I am using treeview to show these directories and sub-directories.To speed up my program i am using multi-threading,but i am facing a problem that the tree is displayed only after all the threads are executed.I want tree to be displayed as any thread is executed and other nodes append to tree dynamically as other threads are executed.Thanks in advance.
Here is my code
import os
import threading
import gtk
class FileBrowser:
def __init__(self):
self.window = gtk.Window()
self.window.show_all()
self.window.connect("destroy", gtk.main_quit)
box = gtk.VBox()
box.show()
self.scrolled_w = gtk.ScrolledWindow()
self.scrolled_w.show()
self.window.add(box)
button = gtk.Button("start")
button.show()
button.connect("clicked", self.start_scanning)
button.set_size_request(30, 50)
box.pack_start(button, False, False, 4)
self.model=gtk.TreeStore(str)
self.treeview = gtk.TreeView(self.model)
self.treeview.show()
col = gtk.TreeViewColumn("FileName")
cell = gtk.CellRendererText()
self.treeview.append_column(col)
col.pack_start(cell, 0)
col.set_attributes(cell, text=0)
box.pack_start(self.scrolled_w, 10)
self.scrolled_w.add(self.treeview)
self.window.set_size_request(600, 300)
def start_scanning(self, w):
self.model.clear()
main_dir = "/"
list_dir = os.listdir(main_dir)
no_of_threads = len(list_dir)
threads = []
for i in range(no_of_threads):
t = threading.Thread(target=self.thread_scanning,
args=(main_dir,list_dir[i],))
threads.append(t)
t.start()
for t in threads:
t.join()
def thread_scanning(self, main_d, list_d):
path = main_d+""+list_d
if os.path.isdir(path):
list_subd = os.listdir(path)
par = self.model.append(None, [list_d])
for sub in list_subd:
self.model.append(par, [sub])
def main(self):
gtk.main()
if __name__=="__main__":
fb = FileBrowser()
fb.main()
So there were two basic issues with your code.
As #windfinder pointed out, you need to run gobject.threads_init() for threading to work properly with your gtk-application if you want the threads to update your gui. Actually it can cause many weird problems if you don't run gobject.threads_init()
The callback of the button doesn't finnish until all threads have finnished since you ask the threads to join before you return control to the main-loop. You can fix this by adding a timeout via gobject.
I did a couple of changes to your code (I also asked the threads to sleep so I could see that the gui was updated while the threads are still running).
#!/usr/bin/env python
import gobject
import time # This is just used for slowing down the threads
import os
import threading
import gtk
class FileBrowser:
def __init__(self):
self.window = gtk.Window()
self.window.show_all()
self.window.connect("destroy", self._destroy)
box = gtk.VBox()
box.show()
self.scrolled_w = gtk.ScrolledWindow()
self.scrolled_w.show()
self.window.add(box)
self.status = gtk.Label("...") # I added a status label to see when scan is done
self.status.show()
box.pack_start(self.status, False, False, 2)
self.button = gtk.Button("start")
self.button.show()
self.button.connect("clicked",self.start_scanning)
self.button.set_size_request(30,50)
box.pack_start(self.button,False,False,4)
self.model=gtk.TreeStore(str)
self.treeview = gtk.TreeView(self.model)
self.treeview.show()
col = gtk.TreeViewColumn("FileName")
cell = gtk.CellRendererText()
self.treeview.append_column(col)
col.pack_start(cell,0)
col.set_attributes(cell,text=0)
box.pack_start(self.scrolled_w,10)
self.scrolled_w.add(self.treeview)
self.window.set_size_request(600,300)
self.threads = list() # I made this be part of the full application
""" That allows us to wait for all threads on close-down, don't know how
necessary it is."""
def start_scanning(self,w):
self.button.set_sensitive(False) # To disallow stacking scan-stats
self.status.set_text("Scanning...") # Let user know it is started
self.model.clear()
main_dir= os.path.expanduser("~") # I just wanted my home-dir instead
list_dir = os.listdir(main_dir)
no_of_threads = len(list_dir)
for i in range(no_of_threads):
t = threading.Thread(target=self._thread_scanning,args=(main_dir,list_dir[i],))
self.threads.append(t)
t.start()
gobject.timeout_add(200, self._callback) # This will cause the main app to
#check every 200 ms if the threads are done.
def _callback(self):
if threading.active_count() == 1: # If only one left, scanning is done
self.status.set_text("Done!")
self.button.set_sensitive(True) # Allow button being pressed again
return False # False make callback stop
print threading.active_count()
return True
def _thread_scanning(self,main_d,list_d):
path = os.sep.join((main_d, list_d)) # Made use of os's sep instead...
if os.path.isdir(path):
list_subd = os.listdir(path)
par = self.model.append(None,[list_d])
for sub in list_subd:
self.model.append(par,[sub])
time.sleep(3) # Useless other than to delay finish of thread.
def main(self):
gtk.main()
def _destroy(self, *args, **kwargs):
#Own destroy that waits for all threads...
for t in self.threads:
t.join()
gtk.main_quit(*args, **kwargs)
if __name__=="__main__":
gobject.threads_init()
fb=FileBrowser()
fb.main()

GTK::Socket and Gtk::Plug unexpected behaviour under Gnome and FVWM2

The following code works fine if run within FVWM2. But if you change desktop to Gnome, then the embedded window is destroyed instead of being embedded.
Why is that? What am I missing?...
The code follows but basically all it does is fork. In the child, we create a VPython window an let it idle forever. In the parent, we create a GTK window, find out what the window ID of the child window is, and try to embed it vis a GTK::Socket.
Note that the VPython part maybe irrelevant to this.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import sys
import os
import re
import time
from visual import *
def find_window_id (title):
"""Gets the OpenGL window ID."""
pattern = re.compile('0x[0-9abcdef]{7}')
proc = subprocess.Popen(['xwininfo', '-name', title],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
errors = proc.stderr.readlines()
if errors:
return None
for line in proc.stdout.readlines():
match = pattern.findall(line)
if len(match):
return long(match[0], 16)
return None
class Setting ():
"""VPython/OpenGL class."""
def __init__ (self, w=256, h=256, title='OpenGL via VPython'):
"""Initiator."""
self.width = w
self.height = h
self.title = title
self.scene = display.get_selected()
self.scene.title = self.title
self.scene.width = self.width
self.scene.height = self.height
self.sphere = sphere()
class GTKDisplay ():
def __init__ (self, winID):
"""Initiator: Draws the GTK GUI."""
import gtk
import pygtk
self.OpenGLWindowID = winID
window = gtk.Window()
window.show()
socket = gtk.Socket()
socket.show()
window.add(socket)
window.connect("destroy", lambda w: gtk.main_quit())
socket.add_id(long(self.OpenGLWindowID))
gtk.main()
def main ():
"""Main entry point."""
name = 'sphere OpenGL window'
child_pid = os.fork()
if 0 == child_pid:
sut = Setting(title=name)
else:
winID = None
while not winID:
time.sleep(.1)
winID = find_window_id(name)
try:
gui = GTKDisplay(winID)
except KeyboardInterrupt, err:
print '\nAdieu monde cruel!'
if __name__ == "__main__":
main()
PS: Yes, this is a follow up from this question.

Categories

Resources