0

I am a beginner programmer trying out using tkinter to present and navigate a file system. while creating a bunch of buttons starting from 1 row and column in, I received the following error:

Traceback (most recent call last):
  File "/Users/harelorin/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python/externalFileManager/learning/test.py", line 24, in <module>
    folderButton = tk.Button(root, textvariable=folderName, font=('Raleway', 15))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2645, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2561, in __init__
    BaseWidget._setup(self, master, cnf)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2530, in _setup
    self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'

The code is as follows:

import os
import tkinter as tk

os.chdir('/Users/harelorin/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python')
global cwd
cwd = os.getcwd()
global folderList
folderList = [ os.path.join(cwd, folder) for folder in os.listdir(cwd) if os.path.isdir(os.path.join(cwd, folder)) ]

root = tk.Tk()

canvas = tk.Canvas(root, width=900, height=600)
canvas.grid(columnspan=5, rowspan=5)

for root, dirnames, filenames in os.walk(cwd):
    x, y = 1, 1
    for folder in dirnames:
        i = 0
        if x > 5:
            x = 1
            y = y + 1
        folderPath = folderList[i]
        folderName = tk.StringVar()
        folderButton = tk.Button(root, textvariable=folderName, font=('Raleway', 15))
        folderName.set(os.path.basename(folderPath))
        folderButton.grid(column=x, row=y)

        x = x + 1

root.mainloop()

If anyone can help me understand what my issue is and offer a result I would be forever grateful :))

2
  • 3
    You overwrote root (the Tk root window) with one of the loop variables in os.walk(). Commented Apr 29, 2022 at 18:10
  • Thank you @jasonharper! So how would you solve this? Commented Apr 30, 2022 at 9:01

1 Answer 1

1

With the help of @jasonharper, I realized that adding root as one of the os.walk() attributes would overrun the root = tk.Tk() that i had placed at the beginning. To solve this I changed the os.walk() code as follows:

for dirname, dirnames, filenames in os.walk(cwd):

This is the answer to my error. If anyone was looking to do a button organizer like me my code was not accurate, and for you to use I am posing the updated code:

import os
import tkinter as tk

os.chdir('/Users/harelorin/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python')
global cwd
cwd = os.getcwd()

root = tk.Tk()

canvas = tk.Canvas(root, width=900, height=600)
canvas.grid(columnspan=5, rowspan=5)

for dirname, dirnames, filenames in os.walk(cwd):
    folderList = [ os.path.join(cwd, folder) for folder in os.listdir(cwd) if os.path.isdir(os.path.join(cwd, folder)) ]
    
    fileList = [ os.path.join(cwd, file) for file in os.listdir(cwd) if os.path.isfile(os.path.join(cwd, file)) ]
    
    x, y = 1, 1
    i = 0
    for folder in dirnames:
        if x > 5:
            x = 1
            y = y + 1
        folderPath = folderList[i]
        folderName = tk.StringVar()
        folderButton = tk.Button(root, textvariable=folderName, font=('Raleway', 15))
        folderName.set(os.path.basename(folderPath))
        folderButton.grid(column=x, row=y)

        x = x + 1
        i = i + 1
    i = 0
    break
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.