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

 

 

相關文章
相關標籤/搜索