前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tkinter -- Label使用图像与文本

tkinter -- Label使用图像与文本

作者头像
py3study
发布2018-08-02 17:48:40
1.6K0
发布2018-08-02 17:48:40
举报
文章被收录于专栏:python3python3

tkinter同时使用图像与文本

compound: 指定文本(text)与图像(bitmap(内置图)/image(自定义图片)是如何在Label上显示,当指定image/bitmap时,会显示图像或自定义图片.

left:   图像居左

right:  图像居右

top:    图像居上

bottom: 图像居下

center: 文件覆盖在图像上

bitmap/image : 显示在Label上的图像

text: 显示在Label上的文本

示例:

代码语言:javascript
复制
from tkinter import *
root = Tk()
root.title('tkinter')

# 图像居下
label1 = Label(root, fg='red', bg='blue', text='botton', compound='bottom', bitmap='error')

# 图像居上
label2 = Label(root, fg='red', bg='yellow', text='top', compound='top', bitmap='error')

# 图像居右
label3 = Label(root, fg='red', bg='green', text='right', compound='right', bitmap='error')

# 图像居左
label4 = Label(root, fg='red', bg='lightblue', text='left', compound='left', bitmap='error')

# 文字覆盖在图像上
label5 = Label(root, fg='red', bg='#FF00FF', text='center', compound='center', bitmap='error')

for i in range(1, 6):
    eval('label' + str(i)).pack()

root.mainloop()

效果:

blob.png
blob.png

自定义image显示,可以为窗口程序添加一个背景图片

使用PhotoImage类处理图片,只能是gif格式

需要传入一个图片路径

示例:

代码语言:javascript
复制
from tkinter import *
root = Tk()
root.title('tkinter')

# 使用PhotoImage类处理图片,只能是gif格式
# 需要传入一个图片路径
bm1 = PhotoImage(file='./images/444.gif')

# 图像居下,文字居上
Label(root, fg='red', bg='yellow', text='妹子图', compound='bottom', image=bm1).pack()

root.mainloop()

效果:

blob.png
blob.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档