Tkinter--Text文本框样例


#-*- coding:utf-8 -*-

"""
Text    文本框样例
实现功能有:Ctrl+a全选文本, 竖向滚动条,横向滚动条(不自动换行) 自动缩放

有谁知道全选文本的方法为会要 return 'break' 吗?
http://blog.csdn.net/xxb2008
"""

import Tkinter


class MainFrame(Tkinter.Frame):
    def __init__(self, master=None):
        Tkinter.Frame.__init__(self, master)
        self.grid(row=0, column=0, sticky="nsew")
        self.createFrame()

    def createFrame(self):
        label_frame_top = Tkinter.LabelFrame(self)
        #label_frame_top.pack()

        label_frame_center = Tkinter.LabelFrame(self)
        label_frame_center.pack(fill="x")

        lfc_field_1 = Tkinter.LabelFrame(label_frame_center)
        lfc_field_1.pack(fill="x")

        self.lfc_field_1_l = Tkinter.Label(lfc_field_1, text="文件路径:", width=10)
        self.lfc_field_1_l.pack(fill="y", expand=0, side=Tkinter.LEFT)

        self.lfc_field_1_b = Tkinter.Button(lfc_field_1, text="清除:", width=10, height=1, command=self.clearText)
        self.lfc_field_1_b.pack(fill="none", expand=0, side=Tkinter.RIGHT, anchor=Tkinter.SE)

        ##########文本框与滚动条
        self.lfc_field_1_t_sv = Tkinter.Scrollbar(lfc_field_1, orient=Tkinter.VERTICAL)  #文本框-竖向滚动条
        self.lfc_field_1_t_sh = Tkinter.Scrollbar(lfc_field_1, orient=Tkinter.HORIZONTAL)  #文本框-横向滚动条

        self.lfc_field_1_t = Tkinter.Text(lfc_field_1, height=15, yscrollcommand=self.lfc_field_1_t_sv.set,
                                          xscrollcommand=self.lfc_field_1_t_sh.set, wrap='none')  #设置滚动条-不换行
        #滚动事件
        self.lfc_field_1_t_sv.config(command=self.lfc_field_1_t.yview)
        self.lfc_field_1_t_sh.config(command=self.lfc_field_1_t.xview)

        #布局
        self.lfc_field_1_t_sv.pack(fill="y", expand=0, side=Tkinter.RIGHT, anchor=Tkinter.N)
        self.lfc_field_1_t_sh.pack(fill="x", expand=0, side=Tkinter.BOTTOM, anchor=Tkinter.N)
        self.lfc_field_1_t.pack(fill="x", expand=1, side=Tkinter.LEFT)

        #绑定事件
        self.lfc_field_1_t.bind("<Control-Key-a>", self.selectText)
        self.lfc_field_1_t.bind("<Control-Key-A>", self.selectText)


        ##########文本框与滚动条end



        label_frame_bottom = Tkinter.LabelFrame(self)
        #label_frame_bottom.pack()

        pass

    #文本全选
    def selectText(self, event):
        self.lfc_field_1_t.tag_add(Tkinter.SEL, "1.0", Tkinter.END)
        #self.lfc_field_1_t.mark_set(Tkinter.INSERT, "1.0")
        #self.lfc_field_1_t.see(Tkinter.INSERT)
        return 'break'  #为什么要return 'break'

    #文本清空
    def clearText(self):
        self.lfc_field_1_t.delete(0.0, Tkinter.END)


def main():
    root = Tkinter.Tk()
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    root.geometry('640x360')  #设置了主窗口的初始大小960x540 800x450 640x360

    main_frame = MainFrame(root)
    main_frame.mainloop()


if __name__ == "__main__":
    main()
    pass

http://blog.csdn.net/xxb2008




  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值