简体   繁体   English

Python模块问题

[英]Python module problem

I have two .py files called "notepad.py" & "pycolour.py". 我有两个.py文件,分别称为“ notepad.py”和“ pycolour.py”。 First is in the parent folder, second is in "subdir" folder. 第一个位于父文件夹中,第二个位于“ subdir”文件夹中。 First file: 第一个文件:

#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()

Second file: 第二档:

#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" colours the python syntax in it's own text widget. “ pycolour.py”为自己的文本小部件中的python语法着色。 I want to colour python syntax in text widget called "text_field" from "notepad.py", so i wrote try_to_colour function. 我想在“ notepad.py”中名为“ text_field”的文本小部件中为python语法着色,所以我编写了try_to_colour函数。 the problem is that i do not understand why this function doesn't work properly. 问题是我不明白为什么这个功能不能正常工作。

PyColour is a class. PyColour是一门课。 In order to take advantage of what it does you must either create an instance of it (eg self.text_field = PyColour(...) ) or create your own class that subclasses the PyColour class (eg class MyPyColour(PyColour): ...; self.text_field = MyPyColour(...) ) 为了利用它的功能,您必须创建它的一个实例(例如self.text_field = PyColour(...) )或创建自己的类作为PyColour类的子类(例如, class MyPyColour(PyColour): ...; self.text_field = MyPyColour(...)

PyColour is not a utility class but a Text widget. PyColour不是实用程序类,而是Text小部件。 It can only color itself. 它只能自己着色。 So what you need to do is replace 所以您需要做的是更换

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

with

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

The problem is that PyColor calls self.tag_add() and similar methods in colourize() . 问题是PyColorcolourize()调用self.tag_add()和类似方法。 These work only on the PyColor instance self , not on your widget. 这些仅适用于PyColor实例self ,不适用于您的小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM