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()
顯示效果如下: