转载: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()