Label是标签
用法
Label(根对象, [属性列表])
属性
- text 要现实的文本
- bg 背景颜色
- font 字体(颜色, 大小)
- width 控件宽度
- height 控件高度
控件属性设置有三种方式:
1.创建对象时,指定宽度与高度
2.使用属性width和height来指定宽度与高度
3.使用configure或config方法来指定宽度与高度
以上三种方式效果相同。
# -*- coding: utf-8 -*-
from Tkinter import *
root = Tk()
one = Label(root, text='one', width=30, height=1,bg="green", font=("Arial", 12))
one.pack()
two = Label(root, text='two',bg="red", font=("Arial", 12))
two['width'] = 30
two['height'] = 2
two.pack()
three = Label(root, text='three',bg="blue", font=("Arial", 12))
three.configure(width=30, height=3)
three.pack()
root.mainloop()
结果显示如下:
调整代码,让三个label同列显示:
# -*- coding: utf-8 -*-
from Tkinter import *
root = Tk()
one = Label(root, text='one', width=30, height=1,bg="green", font=("Arial", 12))
one.pack(side=LEFT) #这里的side可以赋值为LEFT RTGHT TOP BOTTOM
two = Label(root, text='two',bg="red", font=("Arial", 12))
two['width'] = 30
two['height'] = 2
two.pack(side=RIGHT)
three = Label(root, text='three',bg="blue", font=("Arial", 12))
three.configure(width=30, height=3)
three.pack()
root.mainloop()
显示效果:
最后设置好Tkinter窗口的大小属性,一起看看效果
# -*- coding: utf-8 -*-
from Tkinter import *
root = Tk()
root.title("hello world")
root.geometry('800x600')
root.resizable(width=False, height=False)
one = Label(root, text='one', width=30, height=1,bg="green", font=("Arial", 12))
one.pack(side=LEFT) #这里的side可以赋值为LEFT RTGHT TOP BOTTOM
two = Label(root, text='two',bg="red", font=("Arial", 12))
two['width'] = 30
two['height'] = 2
two.pack(side=RIGHT)
three = Label(root, text='three',bg="blue", font=("Arial", 12))
three.configure(width=30, height=3)
three.pack()
root.mainloop()
显示效果如下: