Python 图形界面Tkinter Label 详解

#coding=utf-8
'''1.第一个Label例子'''
from Tkinter import *
#初始化TK
root=Tk()
label = Label(root,text="Hello Tkinter")

#显示label 必须含有此语句
label.pack()

#root.pack()
#但root 是不需要(严格地说是必须不这样使用),否则解释器抱怨
#进入消息循环
root.mainloop()

#控件的显示步骤:
#1.创建这个控件
#2.指定这个控件的master 即这个控件属于哪个
#告诉GM(geometer manager) 有一个控件产生了


#设置窗口的标题
from Tkinter import *
root = Tk()
root.title('hello Tkinter')
root.mainloop()


'''2.Label使用内置位图'''
#bitmap:指定显示的位图
from Tkinter import *
root = Tk()
label = Label(root,bitmap='warning')
label.pack()
root.mainloop()

其他可用的位图:
* error
* hourglass 
* info
* questhead
 * question 
* warning 
* gray12
* gray25
* gray50
* gray75


'''改变Label的前景色和背景色'''
#fg:前景色                  (字体颜色)
#bg:背景色                 (背景颜色)
from Tkinter import *
root = Tk()
Label(root,fg='red',bg='blue',text='Hello I am Tkinter').pack()
Label(root,fg = 'red',bg = '#FF00FF',text = 'Hello I am Tkinter' ).pack()
root.mainloop()




'''设置label宽度与高度'''
#width:宽度
#height:高度
from Tkinter import *
root = Tk()
#创建三个label 分别显示 red blue yellow
#注意三个Label 的大小, 他们均与文本的长度有关
Label(root,
      text='red',
      bg='red'
      ).pack()
Label(root,
      text='blue',
      bg='blue'
      ).pack()
Label(root,
      text='yellow',
      bg='yellow'
      ).pack()
#再创建三个Label与上次不同的是这三个Label 均使用width 和heigth属性
Label(root,
      text='red',
      bg='red',
      width=10,
      height=3
      ).pack()
Label(root,
      text='blue',
      bg='blue',
      width=10,
      height=3
      ).pack()
Label(root,
      text='yellow',
      bg='yellow',
      width = 10,
      height = 3
      ).pack()
root.mainloop()


#coding=utf-8
from Tkinter import *
root = Tk()
#图像居上
Label(root,
      text='botton',
      width = 300,
      height = 40,
      fg='red',
      bg='blue',
      compound='bottom',
      bitmap='error'
      ).pack()

#图像居下
Label(root,
      text = 'botton',
      fg='blue',
      bg='red',
      width = 300,
      height = 40,
      compound = 'bottom',
      bitmap = 'error'
      ).pack()


#图像居左
Label(root,
      text = 'left',
      fg='yellow',
      bg='gray',
      width = 300,
      height = 40,
      compound = 'left',
      bitmap = 'error'
      ).pack()


#文字覆盖在图像上
Label(root,text = 'center',
      compound = 'center',
      fg='yellow',
      bg='gray',
      width=300,
      height=40,
      bitmap = 'error'
      ).pack()

root.mainloop()


'''在 Tk004 中,使用 width 和 heigth 来指定控件的大小,如果指定的大小无法满足文本的要求 是,会出现什么现象呢?如下代码:
Label(root,bg = 'welcome to jcodeer.cublog.cn',width = 10,height = 3).pack() 运行程序,超出 Label 的那部分文本被截断了,常用的方法是:使用自动换行功能,及当文 本长度大于控件的宽度时,文本应该换到下一行显示,Tk 不会自动处理,但提供了属性: wraplength: 指定多少单位后开始换行
justify: 指定多行的对齐方式
ahchor: 指定文本(text)或图像(bitmap/image)在 Label 中的显示位置
可用的值:
e/w/n/s/ne/se/sw/sn/center'''
#文本的多行显示
from Tkinter import *
root = Tk()
#左对齐, 文本居中
Label(root,
      text='welcome to jcodeer.cublog,cn',
      bg='yellow',
      width=40,
      height=3,
      wraplength = 80,
      justify='left'
      ).pack()


#居中对齐, 文本居左
Label(root,text='welcome to jcodeer.cublog.cn',
      bg='red',
      width=40,
      height=3,
      wraplength=80,
      anchor='w'
      ).pack()


#居中对齐,文本居右
Label(root,
      text = 'welcome to jcodeer.cublog.cn',
      bg = 'blue',
      width = 40,
      height = 3,
      wraplength = 80,
      anchor = 'e'
      ).pack()

root.mainloop()

猜你喜欢

转载自blog.csdn.net/clarence20170301/article/details/76619113