1 import tkinter 2 3 wuya = tkinter.Tk() 4 wuya.title("wuya") 5 wuya.geometry("300x200+10+20") 6 7 # 建立文本框text,設置寬度100,high不是高度,是文本顯示的行數設置爲3行 8 text = tkinter.Text(wuya, width='30', height='3') 9 text.pack() 10 11 # 設置文本框內容 12 txt = 'China urges the U.S. to abide by the one-China principle and the principles of the three Sino-U.S.' \ 13 ' Joint Communiques, and stop all forms of military contact with Taiwan including arms sales, Wu said.' 14 # 將文本內容插入文本框 15 text.insert('insert',txt) 16 17 18 19 wuya.mainloop()
結果爲:ide
能夠觀察到內容不太多,顯示不下,加個滾動條使顯示,能夠上下滾動:oop
1 import tkinter 2 3 wuya = tkinter.Tk() 4 wuya.title("wuya") 5 wuya.geometry("300x50+10+20") 6 7 # 建立滾動條 8 scroll = tkinter.Scrollbar() 9 # 建立文本框text,設置寬度100,high不是高度,是文本顯示的行數設置爲3行 10 text = tkinter.Text(wuya) 11 # 將滾動條填充 12 scroll.pack(side=tkinter.RIGHT,fill=tkinter.Y) # side是滾動條放置的位置,上下左右。fill是將滾動條沿着y軸填充 13 text.pack(side=tkinter.LEFT,fill=tkinter.Y) # 將文本框填充進wuya窗口的左側, 14 # 將滾動條與文本框關聯 15 scroll.config(command=text.yview) # 將文本框關聯到滾動條上,滾動條滑動,文本框跟隨滑動 16 text.config(yscrollcommand=scroll.set) # 將滾動條關聯到文本框 17 18 # 設置文本框內容 19 txt = 'China urges the U.S. to abide by the one-China principle and the principles of the three Sino-U.S.' \ 20 ' Joint Communiques, and stop all forms of military contact with Taiwan including arms sales, Wu said.' 21 # 將文本內容插入文本框 22 text.insert('insert',txt) 23 24 25 26 wuya.mainloop()
結果以下:spa