tkinter Label标签相关
2015-10-26 15:14
567 查看
创建标签
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"
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-8 from 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"] = image label_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()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法