tkinter的Text和Scrollbar
这两天开始尝试着做GUI,由于办公室上不了网,所以想主要用tkinter。
1 关于GUI布局
tkinter提供了三种GUI布局方法,pack、grid和place。用来用去我还是觉得place最顺手,不过需要设置得非常详细。典型的place需要提供x、y坐标和width、height数据,单位默认都是像素。
txt = tk.Text()
txt.place(x=20, y=20, width=50, height=50)
就是控件的左上角在坐标20,20处,向右向下扩展,宽度50高度也是50。
2 关于Text
2.1 Text中聚焦在末行
使用see(index)方法,index是Text中的一个位置代字。
txt.see(tk.END) # 这就是一直看着末尾
2.2 Text设置字体格式
Text中使用tag来设置格式模板。
txt.tag_add('tag_name', '1.0', 'end') # 添加tag的方法,需要指定tag的有效位置?
txt.tag_config('tag_name', foreground=‘red’, background='blue', font=('monospace', 20, 'bold', 'italic')) # 详细设定tag的格式,这里依次设置了前景、背景,和字体要素:字体、字号、加粗、斜体。
txt.insert(1.0, "This is a text bar.", 'tag_name') # 这就是使用了新的tag
3 Text 与Scrollbar关联
txt["yscrollcommand"] = scr.set
scr["command"] = txt.yview