Python Forum
[Tkinter] Double GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Double GUI
#1
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):
        T.Tk.__init__(self)
        self.main_frame = ttk.Frame(self, relief='ridge', borderwidth=2)
        self.main_frame.grid()
        self.protocol('WM_DELETE_WINDOW', self.close_window)
        self.main_labels()
        self.main_buttons()

    def close_window(self):
        self.destroy()

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

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

    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():
                    yield entry.name

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


main_gui = MainGUI()
main_gui.mainloop()
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.
Reply
#2
UPDATE:
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)
        subprocess.run('Taskkill /IM Explorer.exe /F')
        subprocess.run('C:\\Windows\\Explorer.exe')
        for s in self.scan_directory(pth):
            print('Loop')
            if s.startswith('thumb'):
                try:
                    os.remove(pth + s)
                except OSError:
                    print('LOCKED')
        print('SUCCESS!')
        if val[0] == 0:
            winreg.SetValueEx(key, 'IconsOnly', 0, winreg.REG_DWORD, 0)
            subprocess.run('Taskkill /IM Explorer.exe /F')
            subprocess.run('C:\\Windows\\Explorer.exe')
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.
Reply


Forum Jump:

User Panel Messages

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