【问题标题】:Uncertain of what instance should be passed to the argument不确定应该将哪个实例传递给参数
【发布时间】:2023-03-26 03:20:01
【问题描述】:

错误信息

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Patrick\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "<string>", line 20, in set_training_data_directory
  File "<string>", line 252, in printer
AttributeError: 'Navigate' object has no attribute 'Listbox1'

在这段代码中,如果我调用函数 set_training_data_directory,它也应该从它所做的 Toplevel1 调用打印机。但是,由于方法打印机期望 self 作为参数,因此我作为对象传递的 Navigate 类中的任何实例都说“对象没有属性 'Listbox1”。我已经将 self 和 self.main_g 作为参数传递了,到目前为止,我得到的错误是“对象没有属性 'Listbox1”

模块名称:All_Classes

import import_ipynb
import Navigator
import Listings
import Main_GUI

模块名称:主模块

from tkinter import *
import import_ipynb
import Main_GUI
root = Tk()
top = Main_GUI.Toplevel1(root)
root.mainloop()

模块名称:列表

import import_ipynb
from tkinter import filedialog
import sys
import tkinter as tk
import os
import All_Classes

class Lists:
    #Making a Navigator Object
    #List of the training data
    def get_list_of_training_data(self):
        training_data_names = []
        directory = self.get_training_dir()        
        for classes in os.listdir(directory):
            for data in os.listdir(os.path.join(directory,classes)):
                print(data)
                training_data_names.append(data)
        return training_data_names

模块名称:导航器

import import_ipynb
from tkinter import filedialog
import tkinter as tk
import All_Classes

class Navigate:  
    def __init__(self):
        #Global Navigation variables
        #Training data directory
        self.TRAINING_DIR = ''
        #Model directory for loading
        self.MODEL_DIR = ''
        #Sound file path for loading
        self.SOUND_FILE = ''

        self.listings = All_Classes.Listings.Lists

        #self.g = GUI.Toplevel1()
        self.main_g = All_Classes.Main_GUI.Toplevel1

    #Function for Data directory
    def set_training_data_directory(self):
        self.TRAINING_DIR = filedialog.askdirectory()
        p = self.listings.get_list_of_training_data(self)
        print(p)
        self.main_g.printer(self)

    def get_training_dir(self):
        return self.TRAINING_DIR

模块名称:Main_GUI

import import_ipynb
import All_Classes
import sys
import tkinter as tk
import tkinter.ttk as ttk
py3 = True

class Toplevel1:  
    def __init__(self, top=None):
        self.nav = All_Classes.Navigator.Navigate()
        #'''This class configures and populates the toplevel window.
        #  top is the,  toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85' 
        _ana2color = '#ececec' # Closest X11 color: 'gray92' 
        self.style = ttk.Style()

        top.geometry("832x674+650+150")


        self.style.configure('TNotebook.Tab', background=_bgcolor)
        self.style.configure('TNotebook.Tab', foreground=_fgcolor)
        self.style.map('TNotebook.Tab', background=
        [('selected', _compcolor), ('active',_ana2color)])
        self.TNotebook1 = ttk.Notebook(top)
        self.TNotebook1.place(relx=0.012, rely=0.0, relheight=0.895
               , relwidth=0.978)
        self.TNotebook1.configure(width=814)
        self.TNotebook1.configure(takefocus="")
        self.TNotebook1_t0 = tk.Frame(self.TNotebook1)
        self.TNotebook1.add(self.TNotebook1_t0, padding=3)


        self.TNotebook2 = ttk.Notebook(self.TNotebook1_t0)
        self.TNotebook2.place(relx=0.037, rely=0.113, relheight=0.461
                , relwidth=0.412)
        self.TNotebook2.configure(width=334)
        self.TNotebook2.configure(takefocus="")
        self.TNotebook2_t0 = tk.Frame(self.TNotebook2)
        self.TNotebook2.add(self.TNotebook2_t0, padding=3)
        self.TNotebook2.tab(0, text="Audio Files", compound="left", underline="-1"
                ,)
        self.TNotebook2_t0.configure(background="#d9d9d9")
        self.TNotebook2_t0.configure(highlightbackground="#d9d9d9")
        self.TNotebook2_t0.configure(highlightcolor="black")


        self.Button2 = tk.Button(self.TNotebook1_t0)
        self.Button2.place(relx=0.123, rely=0.78, height=40, width=190)
        self.Button2.configure(activebackground="#ececec")
        self.Button2.configure(activeforeground="#000000")
        self.Button2.configure(background="#d9d9d9")
        self.Button2.configure(disabledforeground="#a3a3a3")
        self.Button2.configure(foreground="#000000")
        self.Button2.configure(highlightbackground="#d9d9d9")
        self.Button2.configure(highlightcolor="black")
        self.Button2.configure(pady="0")
        self.Button2.configure(text='''Load Training Data''')
        self.Button2.configure(command= self.nav.set_training_data_directory)

        self.Listbox1 = tk.Listbox(self.TNotebook2_t0)
        self.Listbox1.place(relx=0.0, rely=0.0, relheight=1.008, relwidth=1.012)
        self.Listbox1.configure(background="white")
        self.Listbox1.configure(disabledforeground="#a3a3a3")
        self.Listbox1.configure(font="TkFixedFont")
        self.Listbox1.configure(foreground="#000000")
        self.Listbox1.configure(width=334)
        #Init Scrollbar object
        self.scroll1 = tk.Scrollbar(self.Listbox1)
        self.scroll1.configure(command=self.Listbox1.yview)
        self.Listbox1.configure(yscrollcommand = self.scroll1.set)

    def printer(self): 
        print("printer Reached")

        self.Listbox1.pack()
        self.scroll1.pack(side = "right",fill = 'y')

        for i in range(20):
            self.Listbox1.insert(tk.END,i)

我对 python 中的对象很陌生。如果您碰巧有答案,我将不胜感激。这也可能是一种糟糕的设计方式吗?

【问题讨论】:

  • 此代码不运行。请提供Minimal, Complete, and Verifiable example
  • @figbeam 我很抱歉,因为它看起来很长。主要关注的是 set_training_data_directory 方法中的 Navigate 类。在那里我调用属于模块 Main_GUI 的功能打印机。这些代码也应该在不同的模块中。有什么事请告诉我,谢谢
  • 代码应该完整(可运行)的原因是因为我们运行它来看看发生了什么。代码也应该是最小的,所以它不会充满不相关的位。通过使用几个自定义模块,我们必须做很多额外的工作才能让它运行。模块PopupGUI 丢失,import_ipynb 是什么,真的有必要产生错误吗?
  • @figbeam 我再次道歉。我不想让你们经历这个麻烦,但我花了很多时间试图修复它,因为我知道发布所有这些代码会很烦人。不幸的是,有必要产生错误,因为除了我之前给出的解释之外,我不知道任何其他方式来解释它。至于 PopupGUI,您可以将其从 All_Classes 中删除,它应该会运行。我测试了它。必须安装 import_ipynb。例如,我使用的是 jupyter notebook,我是从 anaconda 提示符安装的。
  • 所以,请告诉我一些关于您的环境的信息。我安装了import_ipynbIPython,现在它在抱怨No module named 'nbformat'。你能显示一个依赖列表吗?

标签: python user-interface tkinter ipython


【解决方案1】:

嗯。我看了看,这绝对是你如何编写和使用类的问题。在 Navigator 中,你调用了 Lists 类,但你没有实例化它:

self.listings = All_Classes.Listings.Lists

要创建一个实例,你必须用括号结束类名。因此,当您运行 get_list_of_training_data(self) 方法时,没有 self 可以传递,因为 self 是实例的名称。

self.main_g = All_Classes.Main_GUI.Toplevel1 相同。

在我看来,当你真的不需要它们时,你会构建一个复杂的类结构。引用 Jack Diederich 的话:“this is not a class 的签名是它有两个方法,其中一个是 init”。 YouTube 剪辑:Stop Writing Classes

就我而言,我无法让它正常运行,因为我的os.path.join() 提供了功能失调的路径,不知道为什么。

但是:看看你是否可以让代码不那么破碎,我认为它会更容易发现问题。

【讨论】:

  • 是的,你是对的。这是在我现在正在学习的python中制作实例的一种非常糟糕的方法。如果你同意的话,我会改变我的方法,让你保持在循环中。
  • 好的。将其作为新问题发布,并通过链接添加对此问题的评论。
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2020-04-29
  • 2016-09-24
  • 2021-10-26
相关资源
最近更新 更多