-2

I've looked around for a bit, but might of got off track trying to fix the issue.

Trying to scan selected folder for extensions, then populate checkboxes with found extensions for user to select what extensions to move forward with. the amount of checkboxes can change source to source, tried using the super() but don't believe to be using it right.

import os
from tkinter import *
import tkinter #as tk
from tkinter import filedialog


root = tkinter.Tk()
root.withdraw()

file_path = filedialog.askdirectory()

print(file_path)
#src_folder = "../../"
src_folder = file_path

ListFiles = os.walk(src_folder)
SplitTypes = []
for walk_output in ListFiles:
    for file_name in walk_output[-1]:
        SplitTypes.append(file_name.split(".")[-1])
#print(SplitTypes)

extensions = []
for i in SplitTypes:
    if i not in extensions:
        extensions.append(i)
extensions = str(extensions)
#print(str(extensions))
print(extensions)

class CheckBox(tkinter.Checkbutton):
    boxes = []  # Storage for all buttons

    def __init__(self, master, *args, **options):
        super(CheckBox, self).__init__()
        tkinter.Checkbutton.__init__(self, *args, options)  # Subclass checkbutton to keep other methods
        self.boxes.append(self)
        self.var = tkinter.BooleanVar()  # var used to store checkbox state (on/off)
        self.text = self.cget('text')  # store the text for later
        self.configure(variable=self.var)  # set the checkbox to use our var


a=0
while a<len(extensions):
   button=CheckBox(tkinter, text=extensions[a], command=print(extensions[a]))  # Replace Checkbutton
   a=a+1
   button.pack()

extensions_check = []
for box in CheckBox.boxes:
    if box.var.get():  # Checks if the button is ticked
        extensions_check.append(box.text)

print(extensions_check)

error that i am getting is

C:\Users\Python\Projects\filescanner\Scripts\python.exe C:/Users/Python/Projects/filescanner/extensionfinder.py
F:/TestFolder
['xlsx', 'txt', 'avi', 'nxt']
[
Traceback (most recent call last):
  File "C:/Users/Python/Projects/filescanner/extensionfinder.py", line 45, in <module>
    button=CheckBox(tkinter, text=extensions[a], command=print(extensions[a]))  # Replace Checkbutton
  File "C:/Users/Python/Projects/filescanner/extensionfinder.py", line 36, in __init__
    tkinter.Checkbutton.__init__(self, *args, options)  # Subclass checkbutton to keep other methods
  File "C:\Python\Current Install\lib\tkinter\__init__.py", line 2993, in __init__
    Widget.__init__(self, master, 'checkbutton', cnf, kw)
  File "C:\Python\Current Install\lib\tkinter\__init__.py", line 2561, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python\Current Install\lib\tkinter\__init__.py", line 2530, in _setup
    self.tk = master.tk
AttributeError: 'dict' object has no attribute 'tk'

Process finished with exit code 1

I am new to the python world and up for any advise, thank you all in advance

  • You probably want to start by using [getattr](https://docs.python.org/3/library/functions.html#getattr) instead of just accessing attributes without checking whether they even exist. You _could_ use [hasattr](https://docs.python.org/3/library/functions.html#hasattr) to check before you access it, but `getattr` combines those two steps into "getting you the attribute value _or_ some fallback value if it doesn't exist" – Mike 'Pomax' Kamermans Jun 11 '20 at 18:23
  • Hi Mike, with the getattr would i be doing that on self on its own line? so something like getattr(self) and should that be before the def __init__(self, master, *args, **options): – python_cowboy Jun 11 '20 at 18:33

1 Answers1

0

There are two errors (at least, two that cause the error you describe):

button=CheckBox(tkinter, text=extensions[a], command=print(extensions[a]))

Should be:

button=CheckBox(root, text=extensions[a], command=print(extensions[a]))

And:

tkinter.Checkbutton.__init__(self, *args, options)

Should be:

tkinter.Checkbutton.__init__(self, master, *args, options)

You should either use one of the two lines below:

super().__init__(master, *args, options)
tkinter.Checkbutton.__init__(self, master, *args, options)  # Subclass checkbutton to keep other methods

Both are equivalent though most people prefer to use super()

Ronald
  • 2,930
  • 2
  • 7
  • 18
  • Ronald - thank you for the response your edits did let the script go through all the way with no errors. now I have much more to figure out as it did not work the way I was hoping it to. I see that I am actually not making the checkboxes into a gui to be selected and the ``` button=CheckBox(root, text=extensions[a], command=print(extensions[a])) ``` is giving me only one letter instead of the whole text between ' ' i will continue playing with it now that the error is gone and possibly be posting a new question thanks again! – python_cowboy Jun 11 '20 at 19:00
  • You're welcome. tkinter can get really confusing when you're loosing track of what you are exactly doing. Working in very small incremental steps really helps. – Ronald Jun 11 '20 at 19:15
  • 1
    `super()` calls shouldn't need `self` passed, though, even when they are used? – Mike 'Pomax' Kamermans Jun 11 '20 at 19:40
  • @Mike: you are quite right. I edited my answer for that. – Ronald Jun 11 '20 at 19:48