Python Forum
[Tkinter] Updating Label After Button Press
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Updating Label After Button Press
#1
First off, I admit that I haven't really searched for an answer on these forums. I'm kind of impatient. Here's my question:

I have a simple practice GUI that has a couple labels that describe a task and buttons that do specific tasks. These buttons are tied to functions where the magic happens. I want to know how to update the labels associated with them to show that the task is complete. So, label is "you have 10 files that can be deleted", button deletes them, how can I update the label to say "you now have 0 files to delete"? Preferably, I want to run the function that checks for the files to delete first--that way I'm sure all is good--then update the label. Working code:
import os
import winreg
import tkinter
from tkinter import ttk
import ctypes
import sys

def scan_dumps():
    '''Walks the OS drive and returns
    a dict with the path to and size of
    each .DMP file found on the drive.'''
    dumps = {}
    k = 0
    for dpath, dname, fname in os.walk('C:\\'):
        for f_n in fname:
            if f_n.endswith('.dmp'):
                dumps[k] = [os.path.join(dpath, f_n), os.stat(os.path.join(dpath, f_n)).st_size]
                k += 1
    return dumps

def get_size(dmp_dict_in):
    '''Takes the dict created by
    "scan_dumps()" and gets total
    size of all .DMP files to
    display in the GUI.'''
    i = 0
    tsize = 0
    while i < len(dmp_dict_in):
        tsize += dmp_dict_in[i][1]
        i += 1
    tsize /= 1024
    tsize = round(tsize)
    return format(tsize, ',')

def get_profile_keys():
    '''Opens a specific registry key
    and creates a list of key + sub-key.'''
    reg_key = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles'
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_key, 0, winreg.KEY_ALL_ACCESS) as k:
        ns = winreg.QueryInfoKey(k)[0]
        subs = []
        for i in range(ns):
            subs.append(reg_key + '\\' + winreg.EnumKey(k, i))
    return subs

def get_newest_keys(keys_in):
    '''Takes "get_profile_keys" and decodes the
    "DateCreated" registry value.  Next finds the
    newest values per "DateCreated"and returns a
    list of the full registry key for the newest
    key(s).'''
    stamp = []
    stamps = {}
    for i in range(len(keys_in)):
        reg_key = keys_in[i]
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_key, 0, winreg.KEY_ALL_ACCESS) as k:
            val = winreg.QueryValueEx(k, 'DateCreated')
            stamp.append(int.from_bytes(val[0][:2], 'little'))
            stamp.append(int.from_bytes(val[0][2:4], 'little'))
            stamp.append(int.from_bytes(val[0][6:8], 'little'))
            stamp.append(int.from_bytes(val[0][8:10], 'little'))
            stamp.append(int.from_bytes(val[0][10:12], 'little'))
            stamp.append(int.from_bytes(val[0][12:14], 'little'))
            stamps[i] = stamp
            stamp = []
    if len(stamps) == 1:
        return None
    elif len(stamps) == 2:
        z = 0
        for x in stamps[0]:
            if x > stamps[1][z]:
                return [keys_in[0]]
            elif x < stamps[1][z]:
                return [keys_in[1]]
            z += 1
    #add condition for more than two keys

def delete_dmps(mem_dict_in):
    '''Walks the dict and removes each
    file stored.  "mem_dict_in" must be
    the dict created by "scan_dumps()"'''
    i = 0
    while i < len(mem_dict_in):
        p, s = mem_dict_in[i]
        os.remove(p)
        i += 1

def delete_keys(delete_in):
    '''Takes the list returned by
    "get_newest_keys" and deletes
    each key.'''
    if delete_in is None:
        return None
    else:
        for x in delete_in:
            winreg.DeleteKey(winreg.HKEY_LOCAL_MACHINE, x)

if ctypes.windll.shell32.IsUserAnAdmin():
    MEM_DUMPS = scan_dumps()
    SIZE = get_size(MEM_DUMPS)
    KEYS = get_profile_keys()
    DEL = get_newest_keys(get_profile_keys())

    BASE = tkinter.Tk()
    BASE.title('GUI Practice GUI')

    MEM_EXISTS = tkinter.StringVar()
    REG_SCAN = tkinter.StringVar()
    MEM_EXISTS.set('{} KB of memory dumps found!'.format(SIZE))
    if len(KEYS) > 1:
        NUM = len(KEYS) - 1
        REG_SCAN.set('{} unwanted key(s) found!'.format(NUM))
    else:
        REG_SCAN.set('No unwanted keys found!')

    ROOT_FRAME = ttk.Frame(BASE)
    ROOT_FRAME.grid(column=0, row=0, sticky=('N', 'E', 'S', 'W'))
    ROOT_FRAME.columnconfigure(0, weight=1)
    ROOT_FRAME.rowconfigure(0, weight=1)
    MEM_FRAME = ttk.Frame(ROOT_FRAME, relief='raised', borderwidth=2)
    MEM_LABL1 = ttk.Label(MEM_FRAME, text='Memory Dumps:')
    MEM_LABL2 = ttk.Label(MEM_FRAME, textvariable=MEM_EXISTS)
    MEM_BUTON = ttk.Button(MEM_FRAME, text='Delete', command=delete_dmps(MEM_DUMPS))
    REG_FRAME = ttk.Frame(ROOT_FRAME, relief='raised', borderwidth=2)
    REG_LABL1 = ttk.Label(REG_FRAME, text='Registry:')
    REG_LABL2 = ttk.Label(REG_FRAME, textvariable=REG_SCAN)
    REG_BUTON = ttk.Button(REG_FRAME, text='Delete', command=delete_keys(DEL))

    MEM_FRAME.grid(column=0, row=0)
    MEM_LABL1.grid(column=0, row=0)
    MEM_LABL2.grid(column=0, row=1)
    MEM_BUTON.grid(column=0, row=2)
    REG_FRAME.grid(column=1, row=0)
    REG_LABL1.grid(column=1, row=0, sticky='N')
    REG_LABL2.grid(column=1, row=1)
    REG_BUTON.grid(column=1, row=2)

    BASE.mainloop()
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'practice_gui.py', None, 1)
I suppose I could tie in label stuff into the functions that the buttons call? I don't know. I'm just hoping for a little insight.

-malonn
Reply


Messages In This Thread
Updating Label After Button Press - by malonn - Aug-16-2018, 03:13 PM
RE: Updating Label After Button Press - by Larz60+ - Aug-16-2018, 06:53 PM
RE: Updating Label After Button Press - by malonn - Aug-16-2018, 10:58 PM
RE: Updating Label After Button Press - by woooee - Aug-17-2018, 01:35 AM
RE: Updating Label After Button Press - by Larz60+ - Aug-17-2018, 02:41 AM
RE: Updating Label After Button Press - by malonn - Aug-17-2018, 01:02 PM
RE: Updating Label After Button Press - by Larz60+ - Aug-17-2018, 01:36 PM
RE: Updating Label After Button Press - by malonn - Aug-23-2018, 10:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 2,887 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,223 Aug-15-2022, 07:10 PM
Last Post: menator01
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,123 May-31-2022, 06:37 PM
Last Post: Winfried
  Updating button text based upon different variable values knoxvilles_joker 0 2,250 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  .get() invoke after a button nested press iddon5 5 3,313 Mar-29-2021, 03:55 AM
Last Post: deanhystad
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 4,047 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Anytime I press the button, the result is depicted Jionni 2 2,251 Feb-24-2020, 10:08 AM
Last Post: Jionni
  [Tkinter] Updating a Label with new information LucasRibeiro 2 1,969 Oct-23-2019, 06:45 PM
Last Post: LucasRibeiro
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,055 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 6,439 Jun-17-2019, 05:28 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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