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 :))
root(the Tk root window) with one of the loop variables inos.walk().