热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python设置显示行数_PythonIDLE编译器显示代码行号

1.配置LineNumbers.py文件创建名为:LineNumbers.py文件,粘贴下面的代码到文件中#IDLEXEXTENSION####C

1. 配置LineNumbers.py文件

创建名为:LineNumbers.py 文件,粘贴下面的代码到文件中

# IDLEX EXTENSION

## """

## Copyright(C) 2011 The Board of Trustees of the University of Illinois.

## All rights reserved.

##

## Developed by: Roger D. Serwy

## University of Illinois

##

## Permission is hereby granted, free of charge, to any person obtaining

## a copy of this software and associated documentation files (the

## "Software"), to deal with the Software without restriction, including

## without limitation the rights to use, copy, modify, merge, publish,

## distribute, sublicense, and/or sell copies of the Software, and to

## permit persons to whom the Software is furnished to do so, subject to

## the following conditions:

##

## + Redistributions of source code must retain the above copyright

## notice, this list of conditions and the following disclaimers.

## + Redistributions in binary form must reproduce the above copyright

## notice, this list of conditions and the following disclaimers in the

## documentation and/or other materials provided with the distribution.

## + Neither the names of Roger D. Serwy, the University of Illinois, nor

## the names of its contributors may be used to endorse or promote

## products derived from this Software without specific prior written

## permission.

##

## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

## IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR

## ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF

## CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH

## THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.

##

##

##

## LineNumbers Extension

##

## Provides line numbers to the left of the source code.

##

## The width of the line numbers adapts. Limit of 99,999 lines (for proper display).

##

## """

config_extension_def = """

[LineNumbers]

enable=1

enable_shell=0

visible=True

[LineNumbers_cfgBindings]

linenumbers-show=

"""

import sys

if sys.version <&#39;3&#39;:

from Tkinter import END, Text, LEFT, Y, NONE, RIGHT, NORMAL, DISABLED, Label, TOP, Frame, X

else:

from tkinter import END, Text, LEFT, Y, NONE, RIGHT, NORMAL, DISABLED, Label, TOP, Frame, X

from idlelib.configHandler import idleConf

from idlelib.Delegator import Delegator

from idlelib.Percolator import Percolator

FONTUPDATEINTERVAL &#61; 1000 # milliseconds

_AFTER_UNDO &#61; True # Flag to have the LineNumberDelegator inserted after the undo delegator

jn &#61; lambda x,y: &#39;%i.%i&#39; % (x,y) # join integers to text coordinates

sp &#61; lambda x: map(int, x.split(&#39;.&#39;)) # convert tkinter Text coordinate to a line and column tuple

def dbprint(func): # A decorator for debugging

def f(*args, **kwargs):

print(func, args, kwargs)

return func(*args, **kwargs)

return f

class LineNumbers(object):

menudefs &#61; [(&#39;options&#39;, [(&#39;!Show Line Numbers&#39;, &#39;<>&#39;)])]

def __init__(self, editwin):

self.editwin &#61; editwin

self.text &#61; self.editwin.text

self.textfont &#61; None

self.width &#61; 2

self.after_id &#61; None

self.create_linenumbers()

e &#61; idleConf.GetOption("extensions", "LineNumbers",

"visible", type&#61;"bool", default&#61;True)

self.set_visible(e)

self.code_context_fix()

def close(self):

if self.after_id:

self.text.after_cancel(self.after_id)

self.visible &#61; False

def adjust_font(self):

try:

# taken from CodeContext.py

newtextfont &#61; self.editwin.text["font"]

if self.textln and newtextfont !&#61; self.textfont:

self.textfont &#61; newtextfont

self.textln["font"] &#61; self.textfont

if self._cc_text:

self._cc_text["font"] &#61; self.textfont

self.update_numbers()

except Exception as err:

import traceback; traceback.print_exc()

def font_timer(self):

if not self.visible:

return

self.adjust_font()

if self.after_id:

self.text.after_cancel(self.after_id)

self.after_id &#61; self.text.after(FONTUPDATEINTERVAL, self.font_timer)

if not _AFTER_UNDO:

self.update_numbers() # fixes a bug due to this percolator being ahead of undo percolator.

def set_visible(self, b&#61;True):

self.visible &#61; b

if self.visible:

self.text.after(1, self.font_timer) # avoid a start-up bug

self.show()

# use .after to avoid a start-up error caused by update_idletasks in update_numbers

self.text.after(1, self.update_numbers)

else:

self.hide()

idleConf.SetOption("extensions", "LineNumbers",

"visible", &#39;%s&#39; % self.visible)

self.editwin.setvar("<>", self.visible)

def linenumbers_show_event(self, ev&#61;None):

self.set_visible(not self.visible)

self._code_context_toggle()

def create_linenumbers(self):

""" Create the widget for displaying line numbers. """

editwin &#61; self.editwin

text &#61; self.text

text_frame &#61; editwin.text_frame

self.textln &#61; textln &#61; Text(text_frame, width&#61;self.width,

height&#61;1, wrap&#61;NONE)

# adjust font

textln.config(font&#61;(idleConf.GetOption(&#39;main&#39;, &#39;EditorWindow&#39;, &#39;font&#39;),

idleConf.GetOption(&#39;main&#39;, &#39;EditorWindow&#39;, &#39;font-size&#39;)))

textln.bind("", self.focus_in_event)

textln.bind(&#39;&#39;, self.button_ignore)

textln.bind(&#39;&#39;, self.button_ignore)

textln.bind(&#39;&#39;, self.button_ignore)

textln.bind(&#39;&#39;, self.button_ignore)

textln.bind(&#39;&#39;, self.button_ignore)

textln.bind(&#39;&#39;, self.button_ignore)

textln.bind("", self.button4)

textln.bind("", self.button5)

textln.tag_config(&#39;LINE&#39;, justify&#61;RIGHT)

textln.insert(END, &#39;1&#39;)

textln.tag_add(&#39;LINE&#39;, &#39;1.0&#39;, END)

# start the line numbers

self.per &#61; per &#61; Percolator(textln)

self.line_delegator &#61; LineDelegator()

per.insertfilter(self.line_delegator)

textln._insert &#61; self.line_delegator.delegate.insert

textln._delete &#61; self.line_delegator.delegate.delete

lines &#61; LineNumberDelegator(self)

if _AFTER_UNDO:

# Percolator.py&#39;s .insertfilter should have an "after&#61;" argument

lines.setdelegate(editwin.undo.delegate)

editwin.undo.setdelegate(lines)

else:

editwin.per.insertfilter(lines)

editwin.vbar[&#39;command&#39;] &#61; self.vbar_split

editwin.text[&#39;yscrollcommand&#39;] &#61; self.yscroll_split

def button4(self, ev&#61;None):

self.text.event_generate("")

return "break"

def button5(self, ev&#61;None):

self.text.event_generate("")

return "break"

def button_ignore(self, ev&#61;None):

return "break"

def show(self):

self.textln.pack(side&#61;LEFT, fill&#61;Y, before&#61;self.editwin.text)

def hide(self):

self.textln.pack_forget()

def focus_in_event(self, event&#61;None):

self.editwin.text.focus_set()

self.textln.tag_remove(&#39;sel&#39;, &#39;1.0&#39;, &#39;end&#39;)

#self.editwin.text.event_generate("<>")

def generate_goto_event(self, event&#61;None):

self.editwin.text.event_generate("<>")

return "break"

def vbar_split(self, *args, **kwargs):

""" split scrollbar commands to the editor text widget and the line number widget """

self.textln.yview(*args, **kwargs)

self.text.yview(*args, **kwargs)

def yscroll_split(self, *args, **kwargs):

""" send yview commands to both the scroll bar and line number listing """

#import traceback; traceback.print_stack()

self.editwin.vbar.set(*args)

self.textln.yview_moveto(args[0])

def update_numbers(self, add&#61;None, remove&#61;None):

if not self.visible: return

textln &#61; self.textln

text &#61; self.editwin.text

endline1, col1 &#61; sp(text.index(END))

endline2, col2 &#61; sp(textln.index(END))

if endline1

# delete numbers

textln._delete(&#39;%i.0&#39; % endline1, END)

elif endline1 > endline2:

# add numbers

q &#61; range(endline2, endline1)

r &#61; map(lambda x: &#39;%i&#39; % x, q)

s &#61; &#39;\n&#39; &#43; &#39;\n&#39;.join(r)

textln._insert(END, s)

textln.tag_add(&#39;LINE&#39;, &#39;1.0&#39;, END)

# adjust width of textln, if needed. (counts from 1, not zero)

if endline1 <&#61; 100:

width &#61; 2

elif endline1 <&#61; 1000:

width &#61; 3

elif endline1 <&#61; 10000:

width &#61; 4

else:

width &#61; 5 # more than 9999 lines in IDLE? Really?

# XXX: If your code requires width>5, i.e > 100,000 lines of code,

# you probably should not be using IDLE.

if width > self.width: # 2011-12-18 - only grow, not shrink

self.width &#61; width

textln.configure(width&#61;width)

if self._cc_text: # adjust CC width

self._cc_text.configure(width&#61;width)

self.textln.update_idletasks()

a &#61; self.text.yview()

self.textln.yview_moveto(a[0])

def code_context_fix(self):

self._cc_text &#61; None

self._cc_frame &#61; None

def f():

self.text.bind(&#39;<>&#39;, self._code_context_toggle, &#39;&#43;&#39;)

self._code_context_toggle()

self.text.after(10, f)

def _code_context_toggle(self, event&#61;None):

cc &#61; self.editwin.extensions.get(&#39;CodeContext&#39;, None)

if cc is None:

return

if not self.visible:

if self._cc_frame:

L &#61; cc.label

L.pack_forget()

self._cc_frame.destroy()

L.pack(side&#61;TOP, fill&#61;X, expand&#61;False,

before&#61;self.editwin.text_frame)

return

editwin &#61; self.editwin

text &#61; self.text

text_frame &#61; editwin.text_frame

# repack the Label in a frame

if cc.label:

cc.label.pack_forget()

F &#61; Frame(self.editwin.top)

F.lower() # fix Z-order

t &#61; Text(F, width&#61;self.width, height&#61;1,

takefocus&#61;0)

t.bind("", lambda x: self.text.focus())

t["font"] &#61; self.textln.cget(&#39;font&#39;)

t.pack(side&#61;LEFT, fill&#61;Y)

cc.label.pack(in_&#61;F, fill&#61;X, expand&#61;False)

F.pack(side&#61;TOP, before&#61;text_frame, fill&#61;X, expand&#61;False)

self._cc_frame &#61; F

self._cc_text &#61; t

else:

if self._cc_frame:

self._cc_frame.destroy()

self._cc_frame &#61; None

self._cc_text &#61; None

class LineNumberDelegator(Delegator):

# for editwin.text

def __init__(self, line_number_instance):

Delegator.__init__(self)

self.ext &#61; line_number_instance

def insert(self, index, chars, tags&#61;None):

self.delegate.insert(index, chars, tags)

if &#39;\n&#39; in chars:

self.ext.update_numbers()#add&#61;chars.count(&#39;\n&#39;))

def delete(self, index1, index2&#61;None):

t &#61; self.get(index1, index2)

self.delegate.delete(index1, index2)

if &#39;\n&#39; in t:

self.ext.update_numbers()#remove&#61;t.count(&#39;\n&#39;))

class LineDelegator(Delegator):

# for textln

def insert(self, *args, **kargs):

pass

def delete(self, *args, **kargs):

pass

复制 LineNumbers.py文件到Python安装目录下的Lib\idlelib文件夹。

2. 启动扩展

配置idlelib目录下的config-extensions.def文件

往config-extensions.def文件配置如下数据&#xff1a;

[LineNumbers]

enable&#61;True #开启扩展

enable_editor&#61;True #开启idle编辑器支持

enable_shell&#61;True #开启idle shell支持

visible&#61;True #扩展可见![](https://img2020.cnblogs.com/blog/1995639/202004/1995639-20200406141238406-1079269100.png)

保存之后重启IDLE&#xff0c;可以看到我们的Shell和editor都有了行号。



推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文介绍了在Python张量流中使用make_merged_spec()方法合并设备规格对象的方法和语法,以及参数和返回值的说明,并提供了一个示例代码。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • NotSupportedException无法将类型“System.DateTime”强制转换为类型“System.Object”
    本文介绍了在使用LINQ to Entities时出现的NotSupportedException异常,该异常是由于无法将类型“System.DateTime”强制转换为类型“System.Object”所导致的。同时还介绍了相关的错误信息和解决方法。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有