跳转到主要内容

一套用于显示只读文本和代码的Tkinter小部件。

项目描述

一组用于显示只读文本和代码的Tkinter小部件。

入门指南

tkReadOnly可以从PyPI安装

pip install tkreadonly

ReadOnlyText

ttk.Text小部件的扩展,禁用所有用户编辑功能。

内置的ttk.Text小部件没有“只读”模式。您可以禁用小部件,但这也会禁用选择和其他鼠标事件,并更改文本的颜色方案。

此小部件捕获并丢弃Text小部件上的所有插入和删除事件。这允许小部件在所有其他方面都表现得像正常的ttk.Text小部件。

参数

ReadOnlyText与基本ttk.Text小部件具有相同的参数。

用法

ReadOnlyText的用法与基本ttk.Text小部件的用法相同。

示例

from Tkinter import *

from tkreadonly import ReadOnlyText

# Create the main Tk window
root = Tk()

# Create a main frame
main_frame = Frame(root)
main_frame.grid(column=0, row=0, sticky=(N, S, E, W))

# Put a ReadOnlyText widget in the main frame
read_only = ReadOnlyText(main_frame)
read_only.grid(column=0, row=0, sticky=(N, S, E, W))

# Add text to the end of the widget.
read_only.insert(END, 'Hello world')

# Run the main loop
root.mainloop()

ReadOnlyCode

一个组合小部件,允许您显示带有垂直滚动条的行号注释代码。语法高亮将自动从文件名和/或文件内容中猜测。

参数

样式

要使用的Pygments样式表。默认为monokai

属性

filename

当前显示的文件名。如果您设置此属性,您提供的路径将加载到代码窗口中。

line

文件的当前行。当前行将被突出显示。如果您设置此属性,任何现有的当前行将被清除,新的行将被突出显示。

方法

refresh()

强制重新加载当前文件。

line_bind(sequence, func)

将事件处理器 func 绑定到指定的行号事件序列。如果已存在对给定序列的绑定,它将被覆盖。

支持 <Button-1>-<Button-5><Double-1>-<Double-5> 序列,以及 Shift、Alt 和 Control 修饰符。

当事件发生时,处理器将使用单个参数(发生的事件)被调用。此事件对象将具有一个 line 属性,描述生成事件的行。

name_bind(sequence, func)

func 事件处理器绑定到代码中标记的给定事件序列。如果已存在对给定序列的绑定,它将被覆盖。

支持 <Button-1>-<Button-5><Double-1>-<Double-5> 序列,以及 Shift、Alt 和 Control 修饰符。

当事件发生时,处理器将使用单个参数(发生的事件)被调用。此事件对象将具有一个 name 属性,描述生成事件的标记。

用法

示例

from Tkinter import *
import tkMessageBox

from tkreadonly import ReadOnlyCode

# Create the main Tk window
root = Tk()

# Create the main frame
main_frame = Frame(root)
main_frame.grid(column=0, row=0, sticky=(N, S, E, W))

# Create a ReadOnlyCode widget in the main frame
read_only = ReadOnlyCode(main_frame)
read_only.grid(column=0, row=0, sticky=(N, S, E, W))

# Show a particular file
read_only.filename = '/path/to/file.py'

# Highlight a particular line in the file
read_only.line = 5

# Set up a handler for a double click on a line number
def line_handler(event):
    tkMessageBox.showinfo(message='Click on line %s' % event.line)

read_only.line_bind('<Double-1>', line_handler)

# Set up a handler for a single click on a code variable
def name_handler(event):
    tkMessageBox.showinfo(message='Click on token %s' % event.name)

read_only.name_bind('<Button-1>', name_handler)

# Run the main event loop
root.mainloop()

Ubuntu 下的已知问题

Ubuntu 的 Python 打包中省略了其基本包中的 idlelib 库。如果您在 Ubuntu 13.04 上使用 Python 2.7,可以通过运行以下命令安装 idlelib

$ sudo apt-get install idle-python2.7

对于其他版本的 Python 和 Ubuntu,您需要相应地进行调整。

Windows 下的问题

如果您在 virtualenv 中运行 Cricket,您需要设置一个环境变量,以便 Cricket 可以找到 TCL 图形库

$ set TCL_LIBRARY=c:\Python27\tcl\tcl8.5

您需要调整确切路径以反映您本地的 Python 安装。您可能希望将此行放入虚拟环境的 activate.bat 脚本中,以便每次激活 virtualenv 时自动设置。

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

tkreadonly-0.6.1.tar.gz (8.0 kB 查看散列)

上传时间

构建分布

tkreadonly-0.6.1-py2.py3-none-any.whl (9.4 kB 查看散列)

上传时间 Python 2 Python 3

由以下支持