zoukankan      html  css  js  c++  java
  • 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')
  • 相关阅读:
    NullPointerException
    面试oracle 经常问的一个问题- 事务
    python 之 import、from、as 关键字的 白话 解释与例子
    python 学习 之 第二章(条件、循环和其他语句)
    python学习 之 第一章 (简单例子与常用数据类型)
    python中常用函数含义记录
    python 2 版本中的input() 和 raw_input() 函数的比较
    字符串处理关键字str 和 repr
    文件操作-一个可以直接复制文件数据的小程序
    C语言 32个关键字
  • 原文地址:https://www.cnblogs.com/yfacesclub/p/9737338.html
Copyright © 2011-2022 走看看