Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AttributeError:“dict”对象没有属性“tk”_Python_Tkinter - Fatal编程技术网

Python AttributeError:“dict”对象没有属性“tk”

Python AttributeError:“dict”对象没有属性“tk”,python,tkinter,Python,Tkinter,我环顾了一下四周,但可能是因为试图解决问题而偏离了轨道 尝试扫描选定文件夹中的扩展名,然后用找到的扩展名填充复选框,以便用户选择要使用的扩展名。复选框的数量可以改变源到源,尝试使用super,但不相信使用它是正确的 import os from tkinter import * import tkinter #as tk from tkinter import filedialog root = tkinter.Tk() root.withdraw() file_path = filedi

我环顾了一下四周,但可能是因为试图解决问题而偏离了轨道

尝试扫描选定文件夹中的扩展名,然后用找到的扩展名填充复选框,以便用户选择要使用的扩展名。复选框的数量可以改变源到源,尝试使用super,但不相信使用它是正确的

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)
我得到的错误是

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

我是python世界的新手,希望得到任何建议,提前谢谢大家

至少有两个错误,两个错误导致了您描述的错误:

button=CheckBox(tkinter, text=extensions[a], command=print(extensions[a]))
应该是:

button=CheckBox(root, text=extensions[a], command=print(extensions[a]))
tkinter.Checkbutton.__init__(self, master, *args, options)
以及:

应该是:

button=CheckBox(root, text=extensions[a], command=print(extensions[a]))
tkinter.Checkbutton.__init__(self, master, *args, options)
您应该使用以下两行中的一行:

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

虽然大多数人喜欢使用super,但这两种方法都是等效的。您可能希望从使用属性开始,而不仅仅是在不检查属性是否存在的情况下访问属性。您可以在访问它之前使用检查,但是getattr将这两个步骤结合起来,以获取属性值,或者如果属性值不存在,则返回某个值。Mike,使用getattr,我会在自己的线路上这样做吗?比如getattrself,应该在def uu init uu self,master,*args,**选项之前:Ronald-感谢您的回复,您的编辑让脚本一路通过,没有错误。现在我有更多的事情要弄清楚,因为它没有按照我希望的方式工作。我发现我实际上并没有将复选框变成要选择的gui,``按钮=CheckBoxroot,文本=extensions[a],命令=printextensions[a]``只给了我一个字母,而不是“”之间的整个文本。现在错误已经消失,我将继续玩它,并且可能会再次发布一个新问题。谢谢!不客气。当你忘记了自己到底在做什么时,tkinter会变得非常困惑。在非常小的增量步骤中工作真的很有帮助。但是,超级调用不需要自传递,即使在使用它们时也是如此?@Mike:你说得很对。我编辑了我的答案。