• GUI库之Tkinter组件(二)


    一、Lable组件

    Lable组件是用于在界面上输出描述的标签;

    1.举个例子、

    # Lable组件
    from tkinter import *
    
    root = Tk()
    root.title("YfacesClub")
    # 创建一个文本Lable对象
    textLable = Label(root,text="请稍后再点击观看!")
                      # text="很抱歉!
    请稍后再点击观看!",justify=LEFT,padx = 10)
    textLable.pack(side=LEFT)
    # 创建一个图像Lable对象
    # 用PhotoImage实例化一个图像对象
    photo = PhotoImage(file="12.gif")
    imgLable = Label(root,image=photo)
    imgLable.pack(side=RIGHT)
    
    mainloop()

    如图:

    2.使用 对标签文本进行断行

    # Lable组件
    from tkinter import *
    
    root = Tk()
    root.title("YfacesClub")
    # 创建一个文本Lable对象
    textLable = Label(root,
                       text="很抱歉!
    请稍后再点击观看!",justify=LEFT,padx = 10) # 将文字部分左对齐,并在水平位置与边框留有一定的距离,只要设置Lable的justify和padx选项即可
    textLable.pack(side=LEFT)
    # 创建一个图像Lable对象
    # 用PhotoImage实例化一个图像对象
    photo = PhotoImage(file="12.gif")
    imgLable = Label(root,image=photo)
    imgLable.pack(side=RIGHT)
    
    mainloop()

    执行代码,如图:

     参数justify:多行文本的对齐方式(LEFT,RIGHT,CENTER)

    3.组件参数

       
       master         制定控件的父窗口
       Anchor     标签中文本(text)或图像(bitmap/image)的位置;默认为center; 值和布局:
    nw n ne w center e sw s se

      background(bg)    背景色;
        foreground(fg)     前景色;
        borderwidth(bd)    边框宽度;
        width             标签宽度;
        height            标签高度;
        bitmap            标签中的内置位图;如果image选项被指定了,则这个选项被忽略。下面的位图在所有平台上都有效:error, gray75, gray50, gray25, gray12, hourglass, info, questhead,question, 和 warning。
        font               字体;
        image             标签中的图片;
        justify           多行文本的对齐方式;
        text            标签中的文本,可以使用'
    '表示换行
        textvariable      显示文本自动更新,与StringVar等配合着用
       wraplength      指定text中文本多少宽度后开始换行
       compound       指定文本(text)与图像(bitmap/image)是如何在Label上显示,缺省为None,当指定image/bitmap时,文本(text)将被覆盖,只显示图像了。(
    left: 图像居左,right: 图像居右,top:图像居上,bottom: 图像居下 ,center: 文字覆盖在图像上),设置文本和图像的混合模式

     例如:

    (1)wraplength 
    说明:指定text中文本多少宽度后开始换行 label
    =Tkinter.Label(root,text='指定text中文本多少单位后开始换行',                    wraplength=50) (2)justify 说明:text中多行文本的对齐方式 label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',                    wraplength=50,justify='left')
    label
    =Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',                    wraplength=50,justify='right') label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',                    wraplength=50, justify='center') (3)anchor 说明:文本(text)或图像(bitmap/image)在Label的位置。默认为center 值和布局:                nw        n         ne                w       center      e                sw        s          se label=Tkinter.Label(root,text='abcdefghikjlmnopqrstu',                   wraplength=50,width=30,height=10,                        bg='blue',fg='red',anchor='nw') (4)bitmap 说明: 显示内置位图。如果image选项被指定了,则这个选项被忽略。下面的位图在所有平台上都有效:error, gray75, gray50, gray25, gray12, hourglass, info, questhead,question,和 warning。 label=Tkinter.Label(root,bitmap='error') (5)fg  bg 说明:设置前景色和背景色 label=Tkinter.Label(root,text='helloworld!',fg='red',bg='blue')   (a).使用颜色名称 Red Green Blue Yellow LightBlue ...... 
    (
    b).使用#RRGGBB  label = Label(root,fg = 'red',bg ='#FF00FF',text = 'Hello I am Tkinter') 指定背景色为绯红色 
    (c).除此之外,Tk还支持与OS相关的颜色值,如Windows支持SystemActiveBorder,  SystemActiveCaption,  SystemAppWorkspace,  SystemBackground, ..... (6)width height 说明:设置宽度和高度 label=Tkinter.Label(root,text='helloworld!',                     fg='red',bg='blue',                     width=50,height=10) (7)compound 说明:指定文本(text)与图像(bitmap/image)是如何在Label上显示,缺省为None,当指定image/bitmap时,文本(text)将被覆盖,只显示图像了。 可以使用的值: left: 图像居左 right:  图像居右 top:   图像居上 bottom:图像居下      center:文字覆盖在图像上 label=Tkinter.Label(root,text='error',bitmap='error', compound='left')
  • 相关阅读:
    [转] 64位Oracle 11g R2的客户端连接时报ORA-01019错误
    [转]Oracle11g链接提示未“在本地计算机注册“OraOLEDB.Oracle”解决方法
    [转]通过Net Manager 配置Oracle 11g本地监听服务(listener service)
    [转]jQuery Mobile动态刷新页面样式
    [转]SSIS
    [转]在SSIS中,使用“包配置”时的常见错误与解析
    使用JMH做Java微基准测试
    秒杀系统解决方案
    cookie机制和session机制
    app如何节省流量
  • 原文地址:https://www.cnblogs.com/yfacesclub/p/9737338.html
Copyright © 2020-2023  润新知