首页 > IT职业 > IT职场 > python中Label是什么

说明
1、Label是PythonTkinter标签组件之一,主要显示指定窗口中的文本和图像。
2、继承Widget类的Label。
3、只显示的文本和图像可以用来显示一行或多行文本,不允许用户修改。
语法
W = Label(master,option)
master:父容器
options:可选项,以键-值对的形式设置标签的属性,并以分号隔开
实例
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
# super()代表的是父类的定义,而不是父类对象
super().__init__(master)
self.master = master
self.pack()
# self.createwidget()
self.createWidget()
def createwidget(self):
self.but1 = Button(self)
self.but1["text"] = "add"
self.but1.pack()
self.but1["command"] = self.addinfo
def addinfo(self):
messagebox.showinfo("提示","添加成功")
def createWidget(self):
self.lab1 = Label(self,text="这是标签1",width = 10,height = 2,bg = "pink",fg="white")
self.lab1.pack()
self.lab2 = Label(self,text= "这是标签2",width = 10,height =2,bg = "orange", fg = "black",font=("黑体",30) )
self.lab2.pack()
global photo
photo = PhotoImage(file="heart.gif")
self.lab3 = Label(self,image = photo)
self.lab3.pack()
root = Tk()
root.geometry("400x200+200+300")
root.title("MyfirstAPP")
app = Application(master=root)
root.mainloop()
以上就是python中Label的介绍,希望对大家有所帮助。更多Python学习指路:python基础教程
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1678/viewspace-2828185/ ,如需转载,请注明出处,否则将追究法律责任。