前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 编辑框 text

python 编辑框 text

作者头像
用户5760343
发布2022-05-13 10:55:03
8780
发布2022-05-13 10:55:03
举报
文章被收录于专栏:sktj

image.png

"a simple text or file viewer component"

print('PP4E scrolledtext') from tkinter import *

class ScrolledText(Frame): def init(self, parent=None, text='', file=None): Frame.init(self, parent) self.pack(expand=YES, fill=BOTH) # make me expandable self.makewidgets() self.settext(text, file)

代码语言:javascript
复制
def makewidgets(self):
    sbar = Scrollbar(self)
    text = Text(self, relief=SUNKEN)
    sbar.config(command=text.yview)                  # xlink sbar and text
    text.config(yscrollcommand=sbar.set)             # move one moves other
    sbar.pack(side=RIGHT, fill=Y)                    # pack first=clip last
    text.pack(side=LEFT, expand=YES, fill=BOTH)      # text clipped first
    self.text = text

def settext(self, text='', file=None):
    if file:
        text = open(file, 'r').read()
    self.text.delete('1.0', END)                     # delete current text
    self.text.insert('1.0', text)                    # add at line 1, col 0
    self.text.mark_set(INSERT, '1.0')                # set insert cursor
    self.text.focus()                                # save user a click

def gettext(self):                                   # returns a string
    return self.text.get('1.0', END+'-1c')           # first through last

if name == 'main': root = Tk() if len(sys.argv) > 1: st = ScrolledText(file=sys.argv[1]) # filename on cmdline else: st = ScrolledText(text='Words\ngo here') # or not: two lines def show(event): print(repr(st.gettext())) # show as raw string root.bind('<Key-Escape>', show) # esc = dump text root.mainloop()

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档