1. 程式人生 > >tkinter text widget 第一節

tkinter text widget 第一節

inter 3.0 widget nor normal 2.0 rep 換行 long

>>> from tkinter import *
>>> root=Tk()
>>> text=Text(root,width=40,height=10) #創建一個text 文本框。長度是40 pixel 高度是10pixel
>>> text.pack() #排版
>>> text.config(wrap = ‘word‘) #以word 來wrap? 最短單位是單詞
>>> text.get(‘1.0‘,‘end‘) #1表示第一行(從1 開始),。0 表示第一個字節(0開始),end表示整個文本框的結束
‘This is a long message in the text box which is more than 40 characters.\n\n\n\n\n\n\nIf the message hits the bottom of the thext box it will run off the screen.\n‘
>>> text.get(‘1.0‘,‘1.end‘) #從第一行第一個字節開始到第一行最後
‘This is a long message in the text box which is more than 40 characters.‘
>>> text.insert(‘1.0+2 lines‘,‘Inserted message‘) # 第一行過後兩行。第三行 插入內容“Inserted message”
>>> text.get(‘2.0‘) #get第二行的第一個字節。 第二行是空的換行所以為\n

‘\n‘
>>> text.get(‘3.0‘)
‘I‘
>>> text.get(‘3.0‘,‘end‘)
‘Inserted message\n\n\n\n\nIf the message hits the bottom of the thext box it will run off the screen.\n‘
>>> text.get(‘3.0‘,‘3.end‘)
‘Inserted message‘
>>> text.get(‘4.0‘,‘4.end‘) #從第四行第一個字節開始。到底四行結尾
‘‘
>>> text.insert(‘1.0+2lines lineend‘,‘and\nmore and \nmore...‘)
>>> text.delete(‘1.0‘) #刪除第一行第一個字節
>>> text.delete(‘1.0‘,‘1.0 lineend‘) #刪除第一行第一個字節開始到第一行最後
>>> text.delete(‘1.0‘,‘3.0 lineend +1 chars‘) #刪除第一行開始 第三行結束。 並且加上最後一個字節
>>> text.replace(‘1.0‘,‘3.0 lineend‘,‘This is the first line.‘)
>>> text.config(state=‘disabled‘) #配置text的狀態。disable就無法輸入了
>>> text.delete(‘1.0‘,‘end‘)
>>> text.config(state=‘normal‘)
>>>

tkinter text widget 第一節