tkinter Label标签相关

                创建标签
label = ttk.Label(parent, text='Full name:')


通过创建StringVar对象,可以将需要显示的文字内容设置为变量对象
label6 = ttk.Label(frame5)
txt = StringVar()
label6["textvariable"] = txt
txt.set("文字变量")


加入图片
创建PhotoImage对象,指定image属性为该对象。不能识别jpg格式,支持gif格式
label7 = ttk.Label(root)
image_p = PhotoImage(file="python.gif")
label7["image"] = image_p


需同时显示图片和文字时,需设定compound属性,可指定值为
image,text,center,top,left,right
label7 = ttk.Label(root)
image_p = PhotoImage(file="python.gif")
label7["image"] = image_p
label7["text"] = "PYTHON"
label7["compound"] = "left"  # image,text,center,top,left,right
label7.pack(padx=10, pady=10)


如果默认组件区域远远大于组件大小,可以设置anchor属性,设置组件在区域内的摆放位置,可选参数
"n" (north, or top edge), "ne", (north-east, or top right corner), "e", "se", "s", "sw", "w", "nw" or "center"


文本内容的换行,可以通过两种方式:
1.在文本中输入\n
label_2["text"] = "这是多行\n文字\n这是另一行"
2.设置wraplength属性,单位为像素
label_3 = ttk.Label(frame, wraplength=50)


针对多行文字的对齐,可设置justify属性,可选值"left", "center" or "right"


#!/usr/bin/env python3# coding=utf-8from tkinter import *from tkinter import ttk__author__ = 'Administrator'root = Tk()frame = ttk.Frame(root, padding=10, relief="solid", borderwidth=2)frame.grid(padx=10, pady=10)label_1 = ttk.Label(frame, relief="solid", borderwidth=1, justify="left")label_2 = ttk.Label(frame, relief="solid", borderwidth=1, justify="center")label_3 = ttk.Label(frame, relief="solid", borderwidth=1, justify="right")label_4 = ttk.Label(frame, relief="solid", borderwidth=1, wraplength="100")label_5 = ttk.Label(frame, relief="solid", borderwidth=1)label_1["text"] = "这是多行\n文字\n这是另一行"label_2["text"] = "这是多行\n文字\n这是另一行"label_3["text"] = "这是多行\n文字\n这是另一行"label_4["text"] = "这是一行长文字,根据长度自动换行这是一行长文字,根据长度自动换行"label_5["text"] = "PYTHON"image = PhotoImage(file="python.gif")label_5["image"] = imagelabel_5["compound"] = "left"label_1.grid(row=0, column=0)label_2.grid(row=0, column=1)label_3.grid(row=0, column=2)label_4.grid(row=1, column=0, columnspan=3)label_5.grid(row=2, column=0, columnspan=3)for child in frame.winfo_children():    child.grid(padx=3, pady=3)root.mainloop()



           

猜你喜欢

转载自blog.csdn.net/qq_44952766/article/details/89458078