tkinter -- Label使用图像与文本

发布时间:2018-05-28 20:14:43编辑:Run阅读(3357)

    tkinter同时使用图像与文本

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

    left:   图像居左

    right:  图像居右

    top:    图像居上

    bottom: 图像居下

    center: 文件覆盖在图像上


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

    text: 显示在Label上的文本


    示例:

    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



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

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

    需要传入一个图片路径

    示例:

    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

关键字