2011-06-29 10 views
0

さて、この質問に答えるために何を検索するのか分かりませんでした。私のクラスのいくつかは、私のコードではPythonのエラーを参照しているクラス - 時には問題

class MyClass: 
    """A simple example class""" 
    i = 12345 
    def f(self): 
     return 'hello world' 

x = MyClass() 

により指示に従って参照することができるいくつかの理由のために私のコードで

は、私は私が作る次の

import [custom module] 
from [custom module] import * 

を使用して、私のカスタムPythonモジュールをインポートしましたクラスへの参照

classRef = [classfunction]() 

以下は私の実際のコードです

 self.searchDogRef = searchFile() 
     self.appGuiRef = appGUI() 

from Tkinter import * 
import os, sys 
import Tkinter, tkFileDialog 
from tkFileDialog import * 
from openpyxl.reader.excel import load_workbook 
from openpyxl.workbook import Workbook 
#custom python classes 
import reader 
import searchDog 
from searchDog import * 
from reader import * 




class appGUI: 
    def __init__(self,frame): 
     #set windows size 600 by 300 

     #frame = Tk() 
     frame.geometry("600x300+30+30") 
     self.openExcel = Button(frame, text = "Open Defect Excel", command = lambda: self.openExcelFile()) 
     self.openExcel.place(x=10,y=10,width=100,height=25) 
     self.openDefectTextFile = Button(frame, text ="Open Text File", command = lambda: self.openDefectText()) 
     self.openDefectTextFile.place(x=10,y=40,width=100,height=25) 
     self.startButton = Button(frame, text="Start",command = lambda: self.startLoadProcess()) 
     self.startButton.place(x=10,y=70,width=100,height=25) 
    def openExcelFile(self): 
     self.openTemp = tkFileDialog.askopenfilename(parent = root, title = 'Select Defect Excel File') 
    def openDefectText(self): 
     self.openDefect = tkFileDialog.askopenfilename(parent = root, title= 'Select Defect Text File') 
    def start(self): 
     self.startLoadProcess() 


    def startLoadProcess(self): 
     #text file where defect comments are stored 
     filePath = self.openDefect 

     #keyword condition to search for 
     keyword = "18360" #probably will put this in a loop 

     #create an instance of ReadFile from searchDog 
     readInText = ReadFile() 
     keywordSearch = searchFile() 
     #----End searchDog Reference 
     #create an instance of readExcel from ExcelFileHandle 

     i = 2 
     #searches for and writes the comments into a text file 
     while self.readData(i, 2, self.openTemp) != None: 
      keywordSearch.searchForKeywordText(self.readData(i,2,self.openTemp), filePath,i) #searchDog Function 
      self.loadExcelFile.save(self.openTemp) 
      i += 1 

    def readData(self,inputRow,inputColumn,excelFilePath): 

     ''' 
     Constructor 
     ''' 
     self.loadExcelFile = load_workbook(filename = self.openTemp) 
     self.excelWorksheet = self.loadExcelFile.get_sheet_by_name('Defects') 
     self.rowInput = inputRow 
     self.columnInput = inputColumn 
     self.cellValue = self.excelWorksheet.cell(row=self.rowInput,column=self.columnInput).value 
     return self.cellValue, inputRow 


root = Tk() 
app = appGUI(root) 
root.mainloop() 

次のモジュールがexcelWiteIn.py

#built in modules from python 
from openpyxl.reader.excel import load_workbook 
from openpyxl.workbook import Workbook 

#my custom modules from the project 
import searchDog 
import main 


class excelWriteInClass(): 

    ''' 
    writes data coming from searchDog into Defects page 
    ''' 


    def writeToExcelFile(self,textLine,writeRow): 
     ''' 
     Constructor 
     ''' 
     self.searchDogRef = searchFile() 
     self.appGuiRef = appGUI() 

     self.excelDefectWorksheet = self.appGuiRef.loadExcelFile.get_sheet_by_name('Defects') 
     self.excelDefectWorksheet.cell(row= writeRow, column = 11).value = textLine 

私が手にエラーと呼ばれる "main.pyは、" 私の参照は、この行で具体的に定義されていないということです

私はクラスを参照することが時にはうまくいかないことを理解していません。これはPyDevの不具合ですか、それはEclipseの不具合ですか?

***コンピュータ情報:

Eclipse V. Version: 3.6.2 Build id: M20110210-1200 
Compiling under Python 2.7 
    - installed modules --> openPyxl 
using Latest PyDev plugin downloaded from Eclipse Market*** 
+0

これは、あるファイルで 'from module import *'を使用し、別のファイルで 'import module'を使用しているために発生すると考えてください。 '*'の代わりに 'from module import 'を使用する方が常に良いことに注意してください。これにより、インポートを制御することができます。 –

+0

エラーが発生したのは、クラス 'excelWriteInClass'で' __init__'メソッドを定義していないからです。 –

答えて

1

私が正しくあなたを理解している場合、問題はあなたが間違って/インポート..からのインポートを使用していることです。まず、 'import module'と 'module import ...からの呼び出し'の両方を使用する必要はありません。

修正main.py:

import os, sys 

from tkFileDialog import <put here what you really need> 
from Tkinter import <put here what you really need> 

from openpyxl.reader.excel import load_workbook 
from openpyxl.workbook import Workbook 

#custom python classes 
from searchDog import <put here what you really need> 
from reader import <put here what you really need> 

修正excelWiteIn.py:

from main import appGUI 
from searchDog import searchFile 

#OR 

self.searchDogRef = main.searchFile()# in case searchFile specified within main.py otherwise you need to specify proper path to module that contains it 
self.appGuiRef = main.appGUI()# note that you have to add 'frame' here 

を編集 - あなたは小さな修正と質問/コメントをmain.pyここに置く:

import os, sys # not used 
from Tkinter import Tk, Button 
from tkFileDialog import askopenfilename 
from openpyxl.reader.excel import load_workbook 
from openpyxl.workbook import Workbook 

#custom python classes 
from searchDog import searchFile, ReadFile 
from reader import <put here what you really need> 

class appGUI(object): 

    def __init__(self, frame): 
     #set windows size 600 by 300 

     #frame = Tk() 
     frame.geometry("600x300+30+30") 
     self.openExcel = Button(frame, text = "Open Defect Excel", command = lambda: self.openExcelFile()) 
     self.openExcel.place(x=10, y=10, width=100, height=25) 
     self.openDefectTextFile = Button(frame, text ="Open Text File", command = lambda: self.openDefectText()) 
     self.openDefectTextFile.place(x=10, y=40, width=100, height=25) 
     self.startButton = Button(frame, text="Start",command = lambda: self.startLoadProcess()) 
     self.startButton.place(x=10, y=70, width=100, height=25) 

    def openExcelFile(self): 
     # root? it not specified here 
     self.openTemp = askopenfilename(parent=root, title='Select Defect Excel File') 

    def openDefectText(self): 
     # root? it not specified here 
     self.openDefect = askopenfilename(parent=root, title='Select Defect Text File') 

    def start(self): 
     self.startLoadProcess() 

    def startLoadProcess(self): 
     #text file where defect comments are stored 
     filePath = self.openDefect 

     #keyword condition to search for 
     keyword = "18360" #probably will put this in a loop 

     #create an instance of ReadFile from searchDog 
     readInText = ReadFile() 
     keywordSearch = searchFile() 
     #----End searchDog Reference 
     #create an instance of readExcel from ExcelFileHandle 

     i = 2 
     #searches for and writes the comments into a text file 
     while self.readData(i, 2, self.openTemp): 
      keywordSearch.searchForKeywordText(self.readData(i, 2, self.openTemp), filePath, i) #searchDog Function 
      self.loadExcelFile.save(self.openTemp) 
      i += 1 

    # excelFilePath not used at all 
    def readData(self, inputRow, inputColumn, excelFilePath): 
     ''' 
     Constructor 
     ''' 
     # not sure that this is a good idea to load workbook each time 
     self.loadExcelFile = load_workbook(filename = self.openTemp) 
     self.excelWorksheet = self.loadExcelFile.get_sheet_by_name('Defects') 
     self.rowInput = inputRow 
     self.columnInput = inputColumn 
     self.cellValue = self.excelWorksheet.cell(row=self.rowInput, column=self.columnInput).value 
     return self.cellValue, inputRow 


root = Tk() 
app = appGUI(root) 
root.mainloop() 
+0

さて、私は2つのファイルに変更を加えましたが、私はまだ同じ問題を抱えていて、いったん機能していれば、 'code'readInText = ReadFile()およびkeywordSearch = searchFile()'コード'個々のモジュールを作成するために異なるクラスを作成すると、ヘルパーよりも首に痛みがあるように思えるので、コードを再コーディングすることができます。いずれにしても、今のように無作為に停止するべきではありません。 @Artsiom Rudzenka –

+0

申し訳ありません@Artsiom Rudzenkaに言及して以前のコメント 'readInText = ReadFile()とkeywordSearch = searchFile()'をクリーンアップすることを忘れた –

+0

これは単純にこれらのリファレンスをmain.pyに追加する必要があることを意味します。私はあなたのソースをすべて持っているわけではないので、あなたがどこにいるのかは言えません。おそらく、 'searchDog import searchFile'のようなものを使用しなければなりませんが、あなたはモジュールを通ってこのクラスの本当の目的地を見つけなければなりません。 –

関連する問題