鱼C论坛

 找回密码
 立即注册
查看: 2527|回复: 0

python Tkinter中的Label编写文本包含图像显示

[复制链接]
发表于 2016-11-29 19:46:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#!/usr/bin/Python
#coding=utf-8
from Tkinter import *
#导入tk模块
top = Tk()
#初始化Tk
top.title('label test')
#标题显示为label test
label = Label(top, text = 'this is my first label')
#创建一个label,它属于top窗口,文本显示内容为.....
label.pack()
top.mainloop()
#进入消息循环


来一个可以显示图片的,PhotoImage似乎读不了.jpg i格式的
#!/usr/bin/python
#coding=utf-8
from Tkinter import *
#导入tk模块
top = Tk()
#初始化Tk
top.title('label test')
#标题显示为label test
label = Label(top, text = 'this is my first label')
#创建一个label,它属于top窗口,文本显示内容为.....
label.pack()
bm = PhotoImage(file = '/home/fangxu/图片/4.png')
label2 = Label(top, image = bm)
label2.bm = bm
label2.pack()
top.mainloop()
#进入消息循环



然后来一个可以更新图片的

#coding=utf-8
from Tkinter import *
def change():
    label.configure(image = bm2)
top = Tk()
bm = PhotoImage(file = "/home/fangxu/图片/4.png")
bm2 = PhotoImage(file = "/home/fangxu/图片/5.png")
label = Label(top, image = bm)
label.pack()
button = Button(top, text = "changepicture", command = change)
button.pack()
top.mainloop()

上图


按下changepicture 显示另一张图片



前景色和背景色

fg bg

有六种颜色可以使用

Red
Green
Blue
Yellow
LightBlue

置宽度与高度

width:    宽度
height:    高度
一个程序演示这两个

from Tkinter import *
top = Tk()
label1 = Label(top, fg = 'blue', bg = 'red',width = 30, height = 12, text = "color")
label1.pack()
top.mainloop()

截图这个就不截了,比较简单~

图片和文字并存

compound:    指定文本(text)与图像(bitmap/image)是如何在Label上显示,缺省为None,
当指定image/bitmap时,文本(text)将被覆盖,只显示图像了。可以使用的值:
    left:    图像居左
    right:    图像居右
    top:    图像居上
    bottom:图像居下
    center:文字覆盖在图像上

#图像居下
Label(root,text = '图在下',compound = 'bottom',bitmap = '....').pack()
#图像居上
Label(root,text = '图在上',compound = 'top',bitmap = '....').pack()
#图像居右
Label(root,text = '图在右',compound = 'right',bitmap = '...').pack()
#图像居左
Label(root,text = '图在左',compound = 'left',bitmap = '.....').pack()
#文字覆盖在图像上
Label(root,text = '我在中间',compound = 'center',bitmap = '......').pack()
在图片上尝试以下文字覆盖在图片上的,改动一下上边的程序

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-24 05:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表