0

I'm creating an application where buttons are created and deleted according to demand, I would like to find a way to change its theme, in case a new file is added to the folder that labels the button

import threading
from tkinter import* 
from tkinter.ttk import *

root = tk.Tk()


def Hide_elemen(widget): # function to hide unused buttons

    widget.grid_forget()

def show_element(widget, colum, row):
    widget.grid(column=colum, row=row, sticky='nsew')

def update_buttons():

    buttons= sorted(glob.glob('data/*'))

    if len(buttons) >= 1:
        Name = buttons[0].replace('buttons\\','')
        show_element(button1, 0, 0)
        subfolder = sorted(glob.glob(f'{buttons[0]}\*'))
        if len(subfolder ) > 0:
            style = Style()
            style.configure('TButton', font = ('calibri', 10, 'bold', ), foreground = 'green')
            button1.configure(text=Name)
        else:
            style = Style()
            style.configure('TButton', font = ('calibri', 10), foreground = 'Black')
            button1.configure(text=Name)
    else:
        Hide_elemen(button1)

    if len(botoes) >= 2:
        Nome = botoes[1].replace('botoes\\','')
        Mostrar_elemento(Botao2, 1, 0)
        subpasta = sorted(glob.glob(f'{botoes[1]}\*'))
        if len(subpasta) > 0:
            style = Style()
            style.configure('TButton', font = ('calibri', 10, 'bold', ), foreground = 'green')
            Botao2.configure(text=Nome )
        else:
            style = Style()
            style.configure('TButton', font = ('calibri', 10), foreground = 'Black')
            Pista_2.configure(text=Nome)
    else:
        Hide_elemen(Botao2).configure(text=completo)

def monitor_events():
    while True:
        folders= sorted(glob.glob('folders\*'))
        update_buttons()
        while True:
            folders2 = sorted(glob.glob('folders\*'))
            if folders2 != folders:
                break
            for folder in folders2:
                subfolders = sorted(glob.glob(f'{folder}\*'))
                if len(subfolders) > 0:
                    return monitor_events()()

button1 = ttk.Button(frame, textvariable='', command=lambda: Esconder())
button1.grid(row=0, column=0)

button2 = ttk.Button(frame, textvariable='', command=lambda: Mostrar_elemento())
button2.grid(row=0, column=1)


threading.Thread(target=monitor_events).start()

root.mainloop()

with this I want that, when a folder is created a button appears, if the folder is deleted the button is deleted, if a sub folder is created the style of the associated button is changed this interface must work together with another application that automatically updates all folders and subfolders

JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • First of all, `monitor_events()()` doesn't make sense because `monitor_events` doesn't return anything. Second, avoid calling `tkinter` from threads other than the one where you created the `tk.Tk`. `tkinter` doesn't always play nice with threads. If you want a `tkinter` loop friendly loop, look at [this](https://stackoverflow.com/a/459131/11106801) – TheLizzard Jan 12 '23 at 15:20
  • Add this from tkinter import ttk import tkinter as tk and frame to root ttk.Button(root, – toyota Supra Jan 12 '23 at 15:32
  • - Don't use `lambda` for both button. Even not using parameter - Comment out line 70 `#threading.Thread(target=monitor_events).start()` – toyota Supra Jan 12 '23 at 15:39

0 Answers0