2 '''tkinter —— text'''
3 '''可选参数有:
4 background(bg) 文本框背景色;
5 foreground(fg) 前景色;
6 selectbackground 选定文本背景色;
7 selectforeground 选定文本前景色;
8 borderwidth(bd) 文本框边框宽度;
9 font 字体;
10 show 文本框显示的字符,若为*,表示文本框为密码框;
11 state 状态;
12 width 文本框宽度
13 textvariable 可变文本,与StringVar等配合着用
14 '''
15 import tkinter as tk
16 import time
17 import threading
18 #初始化窗口
19 window = tk.Tk()
20 #窗口名称
21 window.title("My Window")
22 #窗口大小,是 x 不是 *
23 window.geometry("400x400")
24 #不能改变窗口的大小
25 window.resizable(width=False,height=False)
26 text = tk.Text(window,width=40)
27 text.place(x=0,y=200)
28 num = 1
29 def hit_insert():
30 content = entry.get()
31 text.insert("insert",content)
32 text_content()
33 def hit_end():
34 content = entry.get()
35 text.insert("end",content)
36 text_content()
37 def text_content():
38 global text,num
39 #从第一行,第0个字符开始,到最后
40 content = text.get("{}.0".format(num),"end")
41 #简单实现自己跟自己说话,insert插入只能再后,不然会乱行
42 content = "\n机器人:" + content
43 text.insert("end",content)
44 #换行读取
45 num += 2
46 def text_delete():
47 global num
48 #清除文本里面的所有内容
49 text.delete("1.0".format(str(num)),"end")
50 #行数也要清楚
51 num = 1
52 #分别将两个按钮回调不用的函数
53 button_insert = tk.Button(window,text='insert point',command=hit_insert)
54 button_insert.pack()
55 button_end = tk.Button(window,text="insert end",command=hit_end)
56 #将end按钮置于insert按钮后面
57 button_end.pack(after=button_insert)
58 #创建清空text的按钮
59 button_delete = tk.Button(window,text="text delete",command=text_delete)
60 #将delete按钮置于end按钮后面
61 button_delete.pack(after=button_end)
62 #创建编辑框,以便输入的内容,放到文本框里
63 entry = tk.Entry(window)
64 #将entry编辑框置于insert前面
65 entry.pack(before=button_insert)
66 #循环窗口
67 window.mainloop()