2011-05-17 132 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(...)