1、Text的基本屬性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南風丶輕語') # 標題 screenwidth = win.winfo_screenwidth() # 屏幕寬度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 text = Text( master=win, # 父容器 bg='pink', # 背景顏色 fg='red', # 文本顏色 relief='sunken', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 邊框的大小 height=3, # 高度 width=20, # 寬度 padx=1, # 內間距,字體與邊框的X距離 pady=1, # 內間距,字體與邊框的Y距離 state='normal', # 設置狀態 normal、active、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字體 wrap='char', # 字數夠width后是否換行 char, none, word ) text.pack() win.mainloop()
備注:
①支持的字體(通過tkinter.font.families獲取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/
②鼠標樣式選項
"arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus","shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"
③邊框樣式,組件狀態閱覽
2、插入字符串和獲取信息
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
def end_insert():
text.insert('end', 'A') # 插入到末尾
def point_insert():
text.insert('insert', 'B') # 插入到光標處
def insert_x_y():
# 插入指定位置(x.y),1.0表示1行1列,1.2表示1行3列,行x從1開始,列y從0開始
# 如果x.y之前沒內容,則添加到前面
text.insert(1.2, 'C')
def get():
# 獲取輸入的信息(x.y),1.0表示1行1列,1.2表示1行3列,行x從1開始,列y從0開始
msg = text.get(1.0, 1.6) # 獲取1行1列至1行7列
print('1行1列至1行7列:{}'.format(msg))
msg = text.get(1.0, '1.end') # 獲取1行1列至1行末尾
print('1行1列至1行末尾:{}'.format(msg))
msg = text.get(1.4, '2.end') # 獲取1行5列至2行末尾
print('獲取1行5列至2行末尾:{}'.format(msg))
msg = text.get(1.2, 'end') # 獲取1行3列至內容結尾
print('獲取1行3列至內容結尾:{}'.format(msg))
def delete():
text.delete(1.0, 1.6) # 刪除1行1列至1行7列
text.delete(1.0, '1.end') # 刪除1行1列至1行末尾
text.delete(1.4, '2.end') # 刪除1行5列至2行末尾
text.delete(1.2, 'end') # 刪除1行3列至內容結尾
if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 500
height = 300
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
text = Text(
master=win, # 父容器
bg='pink', # 背景顏色
fg='red', # 文本顏色
relief='sunken', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。
bd=3, # 邊框的大小
width=5, # 寬度
height=5, # 高度
state='normal', # 設置狀態 normal、readonly、 disabled
cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus...
font=('黑體', 20), # 字體
wrap='char', # 字數夠width后是否換行 char, none, word
)
text.pack()
Button(text='插入到末尾', command=end_insert).pack()
Button(text='插入到鼠標位置', command=point_insert).pack()
Button(text='插入到幾行幾列', command=insert_x_y).pack()
Button(text='獲取輸入的信息', command=get).pack()
Button(text='刪除', command=delete).pack()
win.mainloop()
3、添加滾動條
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南風丶輕語') # 標題 screenwidth = win.winfo_screenwidth() # 屏幕寬度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 800 height = 600 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 frame = Frame(win) frame.pack() s_y = Scrollbar(frame, ) s_y.pack(side=RIGHT, fill=Y) s_x = Scrollbar(frame, orient=HORIZONTAL) s_x.pack(side=BOTTOM, fill=X) text = Text( master=frame, # 父容器 bg='pink', # 背景顏色 fg='red', # 文本顏色 relief='sunken', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 邊框的大小 height=3, # 高度 width=10, # 寬度 padx=1, # 內間距,字體與邊框的X距離 pady=1, # 內間距,字體與邊框的Y距離 state='normal', # 設置狀態 normal、active、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字體 wrap='none', # 字數夠width后是否換行 char, none, word yscrollcommand=s_y.set, # 滾動條 xscrollcommand=s_x.set, # 滾動條 ) s_y.config(command=text.yview) s_x.config(command=text.xview) text.pack() win.mainloop()
備注:
①添加水平滾動條時需要保證wrap='none', 讓字體不自動換行
4、插入組件和圖片
# -*- encoding=utf-8 -*- import threading import time import tkinter from tkinter import * from PIL import Image from PIL import ImageTk def add_btn(): btn = Button(text='按鈕', relief='g', width=20) text.window_create(INSERT, window=btn) # 鼠標處插入 text.update() # 更新 # text.window_create(1.2,window=btn)#1行3列插入 # text.update() def delete(): time.sleep(5) btn.destroy() # 銷毀 thread = threading.Thread(target=delete) thread.start() def add_image(): # 圖片需要為全局變量 text.image_create(INSERT, image=img) text.update() if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南風丶輕語') # 標題 screenwidth = win.winfo_screenwidth() # 屏幕寬度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 img = ImageTk.PhotoImage(Image.open('19.png')) text = Text( master=win, # 父容器 bg='pink', # 背景顏色 fg='red', # 文本顏色 relief='groove', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 邊框的大小 height=3, # 高度 width=20, # 寬度 padx=1, # 內間距,字體與邊框的X距離 pady=1, # 內間距,字體與邊框的Y距離 state='normal', # 設置狀態 normal、active、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字體 wrap='char', # 字數夠width后是否換行 char, none, word ) text.pack() Button(text='添加按鈕', command=add_btn).pack() Button(text='添加圖片', command=add_image).pack() win.mainloop()
備注:
①刪除組件用組件.destroy()即可