2011-05-17 130 views
0

我有兩個名爲「notepad.py」的.py文件,&「pycolour.py」。首先是在父文件夾中,其次是在「subdir」文件夾中。 第一個文件:Python模塊問題

#notepad.py 
from Tkinter import * 
from subdir import pycolour 

class SimpleNotepad(object): 

    def __init__(self): 
     self.frame = Frame(root, bg='#707070', bd=1) 
     self.text_field = Text(self.frame, font='Verdana 10', bd=3, wrap='word') 
     self.text_field.focus_set() 
     self.btn = Button(root, text='try to colour', command=self.try_to_colour) 

     self.coloured = pycolour.PyColour(root) 

     self.frame.pack(expand=True, fill='both') 
     self.text_field.pack(expand=True, fill='both') 
     self.btn.pack() 

    def try_to_colour(self): 
     txt = self.text_field.get(1.0, 'end') 
     text = str(txt).split('\n') 
     for i in range(len(text)-1): 
      self.coloured.colourize(i+1, len(text[i])) 

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

第二個文件:

#pycolour.py 
from Tkinter import * 
import keyword 

#colorizing the Python code 
class PyColour(Text): 

    def __init__(self, parent): 
     Text.__init__(self, parent) 
     self.focus_set() 
     self.tag_conf() 

     self.bind('<Key>', self.key_pressed) 

    tags = {'com' : '#009999', #comment is darkgreen 
      'str' : '#F50000', #string is red 
      'kw' : '#F57A00', #keyword is orange 
      'obj' : '#7A00F5', #function or class name is purple 
      'int' : '#3333FF' #integers is darkblue 
      } 

    def tag_conf(self): 
     for tag, value in self.tags.items(): 
      self.tag_configure(tag, foreground=value) 

    def tag_delete(self, start, end): 
     for tag in self.tags.items(): 
      self.tag_remove(tag, start, end) 

    def key_pressed(self, key): 
     if key.char in ' :[(]),"\'': 
      self.edit_separator() #for undo/redo 

     cline = self.index(INSERT).split('.')[0] 
     lastcol = 0 
     char = self.get('%s.%d'%(cline, lastcol)) 
     while char != '\n': 
      lastcol += 1 
      char = self.get('%s.%d'%(cline, lastcol)) 

     self.colourize(cline,lastcol) 

    def colourize(self, cline, lastcol): 
     buffer = self.get('%s.%d'%(cline,0),'%s.%d'%(cline,lastcol)) 
     tokenized = buffer.split(' ') 

     self.tag_remove('%s.%d'%(cline, 0), '%s.%d'%(cline, lastcol)) 

     quotes = 0 
     start = 0 
     for i in range(len(buffer)): 
      if buffer[i] in ['"',"'"]: 
       if quotes: 
        self.tag_add('str', '%s.%d'%(cline, start), '%s.%d'%(cline, i+1)) 
        quotes = 0 
       else: 
        start = i 
        quotes = 1 
      elif buffer[i] == '#': 
       self.tag_add('com', '%s.%d'%(cline, i), '%s.%d'%(cline, len(buffer))) 
       break 

     start, end = 0, 0 
     obj_flag = 0 
     for token in tokenized: 
      end = start + len(token)+1 
      if obj_flag: 
       self.tag_add('obj', '%s.%d'%(cline, start), '%s.%d'%(cline, end)) 
       obj_flag = 0 
      if token.strip() in keyword.kwlist: 
       self.tag_add('kw', '%s.%d'%(cline, start), '%s.%d'%(cline, end)) 
       if token.strip() in ['def','class']: obj_flag = 1 
      else: 
       for index in range(len(token)): 
        try: int(token[index]) 
        except ValueError: pass 
        else: self.tag_add('int', '%s.%d'%(cline, start+index)) 
      start += len(token)+1 

if __name__ == '__main__': 
    root = Tk() 
    st = PyColour(root) 
    st.pack(fill='both', expand='yes') 
    root.mainloop() 

「pycolour.py」 的顏色在它自己的文本插件的Python語法。我想從「notepad.py」中將文本小部件中的python語法着色爲「text_field」,所以我寫了try_to_colour函數。問題是我不明白爲什麼這個功能不能正常工作。

+0

你將在第一個因爲這不是從子目錄導入模塊的命令,所以爲什麼不問爲什麼*這是*給你一個錯誤? – kindall 2011-05-17 13:55:40

+0

你應該更具體些。你是否從命令行運行腳本?你會得到什麼輸出?有什麼例外? – 2011-05-17 13:57:12

+0

@kinall,在這個語句中沒有錯誤,subdir裏面有「__init.py」。 – 2011-05-17 14:19:47

回答

0

PyColour不是實用程序類,而是Text小部件。它只能自己着色。所以,你需要做的是更換

self.text_field = Text(self.frame, ....) 

self.text_field = PyColor(self.frame) 
# find a different way to set font, etc. 

的問題是,PyColor調用colourize()self.tag_add()和類似的方法。這些僅在PyColor實例self上工作,不在您的小部件上。

+0

我想,我誤解了一些東西,因爲這個技巧不起作用。 – 2011-05-18 06:39:21

+0

如果你告訴我什麼不起作用,我可能會提供幫助。 – 2011-05-18 10:39:18

+0

好吧,「pycolour.py」在它自己的文本小部件中着色Python語法。沒有問題,但我想在我的代碼中使用它,所以它從「notepad.py」爲文本小部件中的Python語法着色。沒有錯誤,這就是爲什麼我很困惑。 – 2011-05-18 12:37:25

1

PyColour是一個類。爲了利用它的作用,你必須創建它的一個實例(例如self.text_field = PyColour(...))或者創建你自己的類,該類繼承PyColour類(例如class MyPyColour(PyColour): ...; self.text_field = MyPyColour(...)