您的位置:首页 > 编程语言 > Python开发

python学习笔记之tkinter中的label

2016-11-29 21:12 513 查看
from tkinter import *
longtext='''多行
文本'''
master = Tk()
master.title("label学习")
w0=Label(master,text=longtext)
w0.pack()
photo=PhotoImage(file="b.png")
w=Label(master,image=photo)
w.pack()
mainloop()


#!/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()


在图片上尝试以下文字覆盖在图片上的,改动一下上边的程序

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: