Python下的TK(二)控件之Label

本文介绍了Python GUI编程中TKinter库的Label控件,详细讲解了如何设置Label的宽度、高度、文本内容、换行、对齐方式、定位、位图、前景色与背景色等属性,以及compound属性用于控制文本与图像的显示位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控件属性设置有三种方式:

1.创建对象时,指定宽度与高度

2.使用属性widthheight来指定宽度与高度

3.使用configureconfig方法来指定宽度与高度

以上三种方式效果相同。

from Tkinter import *
root = Tk() 
one = Label(root,text ='helloworld',width = 30,height = 2)
one.pack() 
 
two = Label(root,text =helloworld')
two['width'] = 30
two['height'] = 3 
two.pack()
 
two = Label(root,text ='helloworld')
two.configure(width =30,height = 3)
two.pack()


一下在说明控件时省略的代码上下文如下:

mport Tkinter
def helloButton():
    print 'hello world!'

root=Tkinter.Tk()
#下面这行依据不同
button=Tkinter.Button(root,text='hello world',\
                      command=helloButton)
button.pack()
root.mainloop()

Lable 标签

Label=Tkinter.Lable(master,text=’helloworld!’)

属性:

master

说明: 指定控件的父窗口

 

text

说明:要显示的文字

Label=Tkinter.Lable(master,text=’helloworld!’)


wraplength

说明:指定text中文本多少宽度后开始换行

label=Tkinter.Label(root,text='指定text中文本多少单位后开始换行',\

                   wraplength=50)


justify

说明:text中多行文本的对齐方式

label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',\

                   wraplength=50,justify='left')

label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',\

                   wraplength=50,justify='right')


label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',\

                   wraplength=50, justify='center')


anchor

说明:文本(text)或图像(bitmap/image)在Label的位置。默认为center

值和布局:

               nw        n         ne

               w       center      e

               sw        s          se

label=Tkinter.Label(root,text='abcdefghikjlmnopqrstu',\

                  wraplength=50,width=30,height=10,\

                       bg='blue',fg='red',anchor='nw')


bitmap

说明: 显示内置位图。如果image选项被指定了,则这个选项被忽略。下面的位图在所有平台上都有效:error, gray75, gray50, gray25, gray12, hourglass, info, questhead,question, warning


label=Tkinter.Label(root,bitmap='error')


fg  bg

说明:设置前景色和背景色

label=Tkinter.Label(root,text='helloworld!',fg='red',bg='blue')

 

(1).使用颜色名称 Red Green Blue Yellow LightBlue ......  (2).使用#RRGGBB  label = Label(root,fg = 'red',bg ='#FF00FF',text = 'Hello I am Tkinter') 指定背景色为绯红色  (3).除此之外,Tk还支持与OS相关的颜色值,如Windows支持SystemActiveBorder,  SystemActiveCaption,  SystemAppWorkspace,  SystemBackground, .....

width height

   说明:设置宽度和高度

label=Tkinter.Label(root,text='helloworld!',\

                    fg='red',bg='blue',\

                    width=50,height=10)


compound

说明:指定文本(text)与图像(bitmap/image)是如何在Label上显示,缺省为None当指定image/bitmap时,文本(text)将被覆盖,只显示图像了。

可以使用的值:

left      图像居左

right:      图像居右

top      图像居上

bottom   图像居下     

center   文字覆盖在图像上

label=Tkinter.Label(root,text='error',bitmap='error', compound='left')



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值