Python模块问题
我有两个名为“notepad.py”和“pycolor.py”的.py文件。第一个在父文件夹中,第二个在“subdir”文件夹中。 第一个文件:Python模块问题,python,module,tkinter,Python,Module,Tkinter,我有两个名为“notepad.py”和“pycolor.py”的.py文件。第一个在父文件夹中,第二个在“subdir”文件夹中。 第一个文件: #notepad.py from Tkinter import * from subdir import pycolour class SimpleNotepad(object): def __init__(self): self.frame = Frame(root, bg='#707070', bd=1)
#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()
#pycolor.py
从Tkinter进口*
导入关键字
#为Python代码着色
类别颜色(文本):
定义初始化(自身,父级):
Text.\uuuuu init\uuuuuuu(自我,父)
self.focus_set()
self.tag_conf()
自我绑定(“”,按下自我键)
标记={'com':'009999',#注释为暗绿色
“str”:“F50000”,字符串为红色
“kw”:“F57A00”,关键字为橙色
'obj':'#7A00F5',#函数或类名为紫色
‘int’:‘#3333FF’#整数是深蓝色的
}
def tag_形态(自身):
对于标记,self.tags.items()中的值:
self.tag\u配置(标记,前景=值)
def tag_delete(自我、开始、结束):
对于self.tags.items()中的标记:
self.tag\u移除(标记、开始、结束)
按def键(自,键):
如果“:[(])”中的key.char,“\”:
self.edit_separator()#用于撤消/重做
cline=self.index(INSERT.split('.')[0]
lastcol=0
char=self.get(“%s.%d%”(cline,lastcol))
而char!='\n':
lastcol+=1
char=self.get(“%s.%d%”(cline,lastcol))
自着色(渐变色、渐变色)
def着色(自着色、渐变着色、lastcol着色):
buffer=self.get(“%s.%d%”(cline,0),“%s.%d%”(cline,lastcol))
标记化=缓冲区分割(“”)
self.tag_remove(“%s.%d%”(cline,0),“%s.%d%”(cline,lastcol))
引号=0
开始=0
对于范围内的i(len(缓冲区)):
如果缓冲区[i]位于['”,“'”]:
如果引用:
self.tag_add('str','%s.%d'(cline,start),'%s.%d'(cline,i+1))
引号=0
其他:
开始=i
引号=1
elif缓冲区[i]='#':
self.tag_add('com','%s.%d'(cline,i),'%s.%d'(cline,len(buffer)))
打破
开始,结束=0,0
obj_标志=0
对于标记化中的标记:
结束=开始+长度(令牌)+1
如果obj_标志:
self.tag_add('obj','%s.%d'(渐变,开始),'%s.%d'(渐变,结束))
obj_标志=0
如果关键字.kwlist中的token.strip()为:
self.tag_add('kw','%s.%d'(渐变,开始),'%s.%d'(渐变,结束))
如果['def','class']中的token.strip():obj_flag=1
其他:
对于范围内的索引(len(令牌)):
try:int(令牌[索引])
除了ValueError:通过
else:self.tag_add('int','%s.%d'(cline,start+index))
开始+=len(令牌)+1
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
st=PyColour(根)
st.pack(填充class='两个',扩展class='yes')
root.mainloop()
“pycolor.py”在自己的文本小部件中为python语法着色。我想给“notepad.py”中名为“text\u field”的文本小部件中的python语法着色,所以我编写了
try\u to\u color
函数。问题是我不理解为什么这个函数不能正常工作。pycolor
不是一个实用程序类,而是一个文本
小部件。它只能自己着色。所以你需要做的是更换
self.text_field = Text(self.frame, ....)
与
问题是
PyColor
调用了self.tag\u add()
和colorize()
中的类似方法。这些只在PyColor
实例self
上工作,而不在您的小部件上工作。PyColor是一个类。为了充分利用它的功能,您必须创建它的实例(例如,self.text\u field=pycolor(…)
),或者创建自己的类,将pycolor类分为子类(例如,class mypycolor(pycolor):…;self.text\u field=mypycolor(…)
)第一行将出现错误,来自subdir import pycolor
,因为这不是用于从子目录导入模块的命令,所以为什么不问问为什么会出现错误?您应该更具体一点。您正在从命令行运行脚本吗?你得到了什么输出?有例外吗?@kinall,这句话没有错,subdir里面有“\uu init.py”。@F.C.,这是一个基于gui的程序,所以只要在itAh上双击就足够了,这样就可以了。但是它不是一个子目录,而是一个模块。我想,我有点误解了,因为这个技巧不起作用。好吧,“pycolor.py”在它自己的文本小部件中为Python语法着色。这没问题,但我想在代码中使用它,所以它在“notepad.py”的文本小部件中为Python语法着色。没有给出错误,这就是为什么我如此困惑的原因。什么是“我的代码”?那是一个小部件还是什么?在你的问题中,你说你不想给文本上色。因此,我的回答解释了如何禁用pycolor.py
中的代码。我知道英语不是你的母语,但请确保你的问题确实表达了你想知道的…我编辑了我的问题。我想,我的问题很容易理解。提前谢谢。
self.text_field = PyColor(self.frame)
# find a different way to set font, etc.