Python Forum
[Tkinter] Double GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Double GUI
Code then question:
import tkinter as T
from tkinter import ttk
from tkinter import messagebox
import os
import subprocess

# Don't want to install a 3rd party module, so will collect system paths here:
AppData = 'C:\\Users\\MaLonn\\AppData\\'

class MainGUI(T.Tk):
    def __init__(self):
        self.main_frame = ttk.Frame(self, relief='ridge', borderwidth=2)
        self.protocol('WM_DELETE_WINDOW', self.close_window)

    def close_window(self):

    def main_labels(self):
        lbl1 = ttk.Label(self.main_frame, text='Delete Thumbnail Files')

    def main_buttons(self):
        btn1 = ttk.Button(self.main_frame, text='Scan', command=self.delete_thumbs)

    def main_messages(self, which):
        if which == 1:
            return messagebox.askyesno(title='Access Denied', message='Would '
            'you like to terminate the explorer process?')

    def scan_directory(self, path, recurs=0):
        with os.scandir(path) as it:
            for entry in it:
                if entry.is_dir():
                    if recurs:
                        yield from self.scan_directory(entry.path, 1)
                elif entry.is_file():

    def delete_thumbs(self):
        path = AppData + 'Local\\Microsoft\\Windows\\Explorer\\'
        tried = 0
        for s in self.scan_directory(path):
            if s.startswith('thumb'):
                    os.remove(path + s)
                except OSError:
                    if tried == 0:
                        if self.main_messages(1):
                            kill ='Taskkill /IM Explorer.exe /F')
                            tried = 1
        if kill.returncode == 0:

main_gui = MainGUI()
1. The script launches a simple GUI, press the button and it is supposed to delete the thumbnail cache files in W10. Trouble I'm having is that if it needs to close explorer, upon launching it again another GUI window opens. Why?
2. Windows question, not really Python, so no biggie if I can't get help. Anyone know the most efficient way to delete thumbnail cache? The .db files are often in use by Explorer.exe and closing it doesn't always do the trick. I'm experimenting with the folder setting to disable thumnails, but so far I can't reliably figure out how to disable the lock Explorer has on the files.
After some experimenting, I've found out more. It seems restarting the Explorer process (the script still runs if it is merely terminated but not started back up again) in Windows interferes with scripts. Here's an updated business end of the script:
def delete_thumbs(self):
        pth = AppData + 'Local\\Microsoft\\Windows\\Explorer\\'
        sb = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced'
        acc = winreg.KEY_ALL_ACCESS
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, sb, access=acc)
        val = winreg.QueryValueEx(key, 'IconsOnly')
        if val[0] == 0:
            winreg.SetValueEx(key, 'IconsOnly', 0, winreg.REG_DWORD, 1)'Taskkill /IM Explorer.exe /F')'C:\\Windows\\Explorer.exe')
        for s in self.scan_directory(pth):
            if s.startswith('thumb'):
                    os.remove(pth + s)
                except OSError:
        if val[0] == 0:
            winreg.SetValueEx(key, 'IconsOnly', 0, winreg.REG_DWORD, 0)
  'Taskkill /IM Explorer.exe /F')
When I restart the Explorer process, the script pauses. Note that this is in an SublimeText REPL session (SublimeREPL). I have not tried running the script via the file because it's harder to debug. I will though. After Explorer has restarted, the GUI hangs (have to force Windows to close it) and when I close the interactive REPL session, Explorer restarts again. Oddness. So, does anyone know anything about Python on Windows and having Explorer tied to the script being executed? Why does restarting Explorer terminate the script? Fix or workaround? Finally, it may be tied to SublimeREPL, I don't know. I will experiement and post back if running the script via double-clicking the file changes anything.

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020