简体   繁体   中英

Python tkinter button: AttributeError: 'str' object has no attribute 'tk'

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:))

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM