python Tkinter组件 - Label

转载:https://www.cnblogs.com/jiayou888888/p/7881803.html

Label

#!/usr/bin/python
# -*- coding:utf-8 -*-

from Tkinter import *    

root = Tk()
root.title('title')
root.geometry('300x200')
label = Label(root, text = 'Hello world')
label.pack()
root.mainloop()

label 中增加图片的方法 :

使用image 属性,代码:

from Tkinter import *


root = Tk()
root.title('我是title')
#root.geometry()
label = Label(root,text='您下载的影片包含未成年人禁止观看的内容!!!' )

photo = PhotoImage(file='C:\\Users\\Administrator\\Desktop\\timg.gif')    #python 默认支持gif格式
imgLabel = Label(root,image = photo)
imgLabel.pack(side = RIGHT)
label.pack(side = LEFT)
root.mainloop()

#这种方法是创建两个 Label

 

 

 

改变前背景色 和 后背景色

前背景色 fg (一般就是打酱油的,前背景色其实就是字体颜色)

后背景色 bg

#!/usr/bin/python
# -*- coding:utf-8 -*-

from Tkinter import *


root = Tk()
root.title('我是title')
#root.geometry()
label = Label(root,text='您下载的影片包含未成年人禁止观看的内容!!!',\
              bg = 'red',fg = 'blue' )                                  #设置前背景色,和后背景色

photo = PhotoImage(file='C:\\Users\\Administrator\\Desktop\\timg.gif')
imgLabel = Label(root,image = photo)
imgLabel.pack(side = RIGHT)
label.pack(side = LEFT)
root.mainloop()

 

 

设置 宽度 与 高度

宽度 width

高度 height

#!/usr/bin/python
# -*- coding:utf-8 -*-

from Tkinter import *


root = Tk()
root.title('我是title')
#root.geometry()
label = Label(root,text='18禁!!!',\
              bg = 'red',width = 10,height = 3 )   # 设置宽度和高度
label2 = Label(root,text='18禁!!!',\
              bg = 'blue',width = 10,height = 3 )
label3= Label(root,text='18禁!!!',\
              bg = 'yellow',width = 10,height = 3 )

photo = PhotoImage(file='C:\\Users\\Administrator\\Desktop\\timg.gif')
imgLabel = Label(root,image = photo)
imgLabel.pack(side = RIGHT)
label.pack(side = TOP)
label3.pack(side = TOP)
label2.pack(side = TOP)
root.mainloop()

 

同时使用 文字和 图片

compound   指定文本 text与图片image 如何在 Label 上显示

from Tkinter import *

root = Tk()

photo = PhotoImage(file = 'C:\\Users\\Administrator\\Desktop\\timg.gif')
Label(root,text='text',compound='top',image= photo).pack()
Label(root,text='text',compound='bottom',image= photo).pack()
Label(root,text='text',compound='left',image= photo).pack()
Label(root,text='text',compound='right',image= photo).pack()
Label(root,text='text',compound='center',image= photo).pack()
Label(root,text='text',compound='none',image= photo).pack()
root.mainloop()

 

 经常用到的是 compound = center,

#!/usr/bin/python
# -*-coding:utf-8 -*-

from Tkinter import *

root = Tk()

photo = PhotoImage(file = 'C:\\Users\\Administrator\\Desktop\\timg.gif')

Label(root,text='人生苦短 \n我用python',image=photo,fg = 'red',\
      compound = CENTER,font = ('微软雅黑',30)).pack()

root.mainloop()

 

文字对齐的设置 justify

文字在Label中 默认居中。 justify 可以设置文字 左对齐 和右对齐 ,直接上代码

#!/usr/bin/python
# -*- coding:utf-8 -*-

from Tkinter import *
root = Tk()
root.title('文本对齐方式') 

photo = PhotoImage(file = 'C:\\Users\\Administrator\\Desktop\\timg.gif')
Label(root,text='我在\n很努力\n的学习python,\n然而还是很菜\n。。。。。。',justify\  
='left').pack(side = LEFT)    #设置为左对齐 justify = ‘left’ 同理右对齐为justify = ‘right’
Label2=Label(root,image = photo).pack(side=RIGHT) 12 13 root.mainloop()

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值