Python下的TK(二)控件之Label

本文介绍了Python GUI编程中TKinter库的Label控件,详细讲解了如何设置Label的宽度、高度、文本内容、换行、对齐方式、定位、位图、前景色与背景色等属性,以及compound属性用于控制文本与图像的显示位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控件属性设置有三种方式:

1.创建对象时,指定宽度与高度

2.使用属性widthheight来指定宽度与高度

3.使用configureconfig方法来指定宽度与高度

以上三种方式效果相同。

from Tkinter import *
root = Tk() 
one = Label(root,text ='helloworld',width = 30,height = 2)
one.pack() 
 
two = Label(root,text =helloworld')
two['width'] = 30
two['height'] = 3 
two.pack()
 
two = Label(root,text ='helloworld')
two.configure(width =30,height = 3)
two.pack()


一下在说明控件时省略的代码上下文如下:

mport Tkinter
def helloButton():
    print 'hello world!'

root=Tkinter.Tk()
#下面这行依据不同
button=Tkinter.Button(root,text='hello world',\
                      command=helloButton)
button.pack()
root.mainloop()

Lable 标签

Label=Tkinter.Lable(master,text=’helloworld!’)

属性:

master

说明: 指定控件的父窗口

 

text

说明:要显示的文字

Label=Tkinter.Lable(master,text=’helloworld!’)


wraplength

说明:指定text中文本多少宽度后开始换行

label=Tkinter.Label(root,text='指定text中文本多少单位后开始换行',\

                   wraplength=50)


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')


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')


bitmap

说明: 显示内置位图。如果image选项被指定了,则这个选项被忽略。下面的位图在所有平台上都有效:error, gray75, gray50, gray25, gray12, hourglass, info, questhead,question, warning


label=Tkinter.Label(root,bitmap='error')


fg  bg

说明:设置前景色和背景色

label=Tkinter.Label(root,text='helloworld!',fg='red',bg='blue')

 

(1).使用颜色名称 Red Green Blue Yellow LightBlue ......  (2).使用#RRGGBB  label = Label(root,fg = 'red',bg ='#FF00FF',text = 'Hello I am Tkinter') 指定背景色为绯红色  (3).除此之外,Tk还支持与OS相关的颜色值,如Windows支持SystemActiveBorder,  SystemActiveCaption,  SystemAppWorkspace,  SystemBackground, .....

width height

   说明:设置宽度和高度

label=Tkinter.Label(root,text='helloworld!',\

                    fg='red',bg='blue',\

                    width=50,height=10)


compound

说明:指定文本(text)与图像(bitmap/image)是如何在Label上显示,缺省为None当指定image/bitmap时,文本(text)将被覆盖,只显示图像了。

可以使用的值:

left      图像居左

right:      图像居右

top      图像居上

bottom   图像居下     

center   文字覆盖在图像上

label=Tkinter.Label(root,text='error',bitmap='error', compound='left')



### Python Tkinter 常见控件列表及用法 #### 1. Button (按钮) 用于创建按钮控件,可以触发特定事件。 ```python import tkinter as tk root = tk.Tk() button = tk.Button(root, text='点击这里') button.pack() # 使用 pack 方法放置按钮 ``` 为了使按钮占据整个窗口宽度,可以通过设置 `fill` 参数来实现[^2]: ```python button_fill_x = tk.Button(root, text='屠龙宝刀点击就送!') button_fill_x.pack(fill='x') ``` #### 2. Label (标签) 显示文本或图像的简单控件。 ```python label = tk.Label(root, text="这是一个标签") label.pack() ``` #### 3. Entry (输入框) 允许用户输入单行文本。 ```python entry = tk.Entry(root) entry.pack() ``` #### 4. Text (多行文本框) 支持多行文本编辑。 ```python text_widget = tk.Text(root) text_widget.pack() ``` #### 5. Checkbutton 和 Radiobutton (复选框和单选钮) 分别表示可以选择多个选项或多选一的选择项。 ```python check_var = tk.IntVar() checkbox = tk.Checkbutton(root, text="勾选我", variable=check_var) checkbox.pack() radio_var = tk.StringVar() radiobutton_1 = tk.Radiobutton(root, text="选项A", value="A", variable=radio_var) radiobutton_2 = tk.Radiobutton(root, text="选项B", value="B", variable=radio_var) radiobutton_1.pack(); radiobutton_2.pack() ``` #### 6. Listbox (列表框) 展示一系列项目供选择。 ```python listbox = tk.Listbox(root) for item in ["苹果", "香蕉", "橙子"]: listbox.insert(tk.END, item) listbox.pack() ``` #### 7. Combobox (组合框) 提供下拉菜单形式的数据选择功能。 ```python from tkinter import ttk combobox = ttk.Combobox(root, values=["上海", "北京", "广州"]) combobox.pack() ``` #### 8. PanedWindow (窗格容器) 能够分割空间并容纳其他部件,在不同部分之间拖动调整尺寸。 ```python paned_window = ttk.PanedWindow(root, orient=tk.HORIZONTAL) pane_left = tk.Frame(paned_window, background="lightblue", width=100, height=100) pane_right = tk.Frame(paned_window, background="pink", width=100, height=100) paned_window.add(pane_left); paned_window.add(pane_right) paned_window.pack(fill=tk.BOTH, expand=True) ``` 值得注意的是,`ttk.Panedwindow` 可以写作 `ttk.PanedWindow` 来保持名称一致性[^3]。 #### 9. TableCanvas (表格视图) 虽然不是标准Tkinter的一部分,但是通过第三方模块tkintertable可以获得更强大的数据呈现能力。 ```python from tkintertable import TableCanvas, TableModel frame = tk.Frame(root) table_model = TableModel(rows=10, columns=2) table_canvas = TableCanvas(frame, model=table_model) table_canvas.createTableFrame() frame.pack(expand=1, fill='both') ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值