不确定应将哪种实例传递给参数

时间:2019-03-11 00:15:05

标签: python user-interface tkinter ipython

错误消息

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

模块名称:Main

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中的对象很陌生。如果您碰巧有一个答案,我将不胜感激。难道这是一种不好的设计方式吗?

1 个答案:

答案 0 :(得分:0)

好吧。我看了一下,这似乎与如何编写和使用类有关。在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)的话:“ 这不是一个类的签名是它有两种方法,其中一种是init”。 YouTube剪辑:Stop Writing Classes

最后,我无法使它正常运行,因为我的os.path.join()提供了功能异常的路径,不知道为什么。

但是:看一下是否可以使代码不那么混乱,我认为发现问题会更容易。