tkinter TEXT


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import Tkinter as tk
import ttk
import tkMessageBox as tkMB


class Application(tk.Tk):
    def __init__(self, master=None):
        tk.Tk.__init__(self, master)
        # super(self.__class__, self).__init__( master )
        self.title('test')
        label_frame_center = tk.LabelFrame(self)
        label_frame_center.grid()
        lfc_field_1 = tk.LabelFrame(label_frame_center)
        lfc_field_1.pack(fill="x")
        self.lfc_field_1_l = tk.Label(lfc_field_1, text="文件路径:", width=10)
        self.lfc_field_1_l.pack(fill="y", expand=0, side=tk.LEFT)
        self.lfc_field_1_b = tk.Button(lfc_field_1, text="清除:", width=10, height=1, command=self.clearText)
        self.lfc_field_1_b.pack(fill="none", expand=0, side=tk.RIGHT, anchor=tk.SE)

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

        self.lfc_field_1_t = tk.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=tk.RIGHT, anchor=tk.N)
        self.lfc_field_1_t_sh.pack(fill="x", expand=0, side=tk.BOTTOM, anchor=tk.N)
        self.lfc_field_1_t.pack(fill="x", expand=1, side=tk.LEFT)

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

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

        self.grid()
        self.createWidgets()

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

        pass

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

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

    def createWidgets(self):
        self.quitButton = tk.Button(self, text='Quit', command=self.confirm)
        self.quitButton.grid()

    def confirm(self):
        result = str(self.lfc_field_1_t.get(1.0, tk.END))

        print result
        self.CI_LIST = []
        for i in result.split('
'):
            tmp = []
            tmp = i.split(',')
            self.CI_LIST.append(tmp)
        print self.CI_LIST
        self.CI_LIST.pop(len(self.CI_LIST) - 1)
        msg = '是否添加SLA监控
'
        for i in self.CI_LIST:
            tmp = 'CI : ' + i[0] + '      ip : ' + i[1] + '      CI描述 : ' + i[2]
            msg += tmp
        flag = tkMB.askokcancel("温馨提示", msg)
        if flag == True:
            print 'exe'


if __name__ == "__main__":
    app = Application()
    app.mainloop()


原文地址:https://www.cnblogs.com/jian-pan/p/6654103.html