Tkinter的Label与Button

简介: Tkinter是Python的一个内置包,主要用于简单的界面设计,使用起来非常方便。

一、创建界面

1. 具体步骤

1.1 导入tkinter包

import tkinter as tk

1.2 tk.Tk()函数:创建一个主界面,并命名为root

root=tk.Tk()

1.3 root.title()函数:给root界面设置一个标题

root.title('Tkinter界面设计')

(1)直接输入一个字符串即可

1.4 root.geometry()函数:设置root界面的大小

root.geometry('500x300')

(1)500表示界面的宽,300表示界面的高,可自行设置
(2)这里的'500x300'是个字符串,且x是字符串'x'
(3)还可以设置两个参数,一个是距离屏幕原点的x轴距离,另一个是y轴距离

1.5 root.mainloop()函数:将root界面停留在桌面上

root.mainloop()

2. 完整程序

2.1 算法设计

import tkinter as tk
root=tk.Tk()
root.title('Tkinter界面设计')
root.geometry('500x300')
root.mainloop()

2.2 运行结果

1.png

二、简单控件

1. Label控件

1.1 tk.Label()函数:创建一个标签控件L

L=tk.Label(root,text='Welcome',font=('宋体',20),width=20,height=3,bg='white',fg='blue')

(1)root为Label控件所在界面
(2)text为Label标签的内容
(3)font为标签内容字体与字体大小
(4)width为标签的宽
(5)height为标签的高
(6)bg为标签的背景颜色
(7)fg为标签的字体颜色

1.2 L.place()函数:放置标签L

L.pack()

(1)将L标签放在界面顶部居中

2. Button控件

2.1 tk.Button()函数:创建一个按钮B

B=tk.Label(root,text='确定',bg='blue',fg='white',width=5,height=2,command=None)

(1)控件的root,text,bg,fg,width,height都是通用的
(2)command是点击该按钮会触发的效果,后面跟一个自定义函数

2.2 B.pack()函数:放置按钮B

B.pack()

(1)pack()函数在控件中通用

3. 完整程序

3.1 算法设计

import tkinter as tk
root=tk.Tk()
root.title('Tkinter界面设计')
L=tk.Label(root,text='Welcome',font=('宋体',20),width=20,height=3,bg='white',fg='blue')
L.pack()
B=tk.Label(root,text='确定',bg='blue',fg='white',width=5,height=2,command=None)
B.pack()
root.geometry('500x300')
root.mainloop()

3.2 运行结果

2.png

目录
相关文章
|
4月前
|
容器
Qt6学习笔记七(ToolButton、RadioButton、GroupBox、CheckBox、ListWidget、TreeWidget、TableWidget)
Qt6学习笔记七(ToolButton、RadioButton、GroupBox、CheckBox、ListWidget、TreeWidget、TableWidget)
49 0
|
1月前
|
Shell Python
Tkinter:功能按钮Button
Tkinter:功能按钮Button
|
5月前
|
Python
tkinter之Radiobutton
tkinter之Radiobutton
28 1
|
5月前
|
Python
tkinter之Button按钮
tkinter之Button按钮
20 1
|
5月前
|
小程序 Python
Tkinter 中的标签(Label)
Tkinter 中的标签(Label)组件是一种用于显示文本或图像的控件。它可以通过 tk.Label() 函数创建,常用的属性包括: - text:设置标签显示的文本内容。
33 1
|
5月前
|
Python
tkinter之button添加背景图片
tkinter之button添加背景图片
50 1
|
5月前
label控件
label控件
24 1
|
5月前
|
Python
tkinter之button简单使用
tkinter之button简单使用
27 1
|
Python
Tkinter的Radiobutton控件
Tkinter的Radiobutton是一个含有多个选项的控件,但是只能选择其中的一个选项
80 0
Tkinter的Radiobutton控件
|
Python
【tkinter学习笔记 - 2】:Entry的使用、Button按钮的使用
【tkinter学习笔记 - 2】:Entry的使用、Button按钮的使用
187 0
【tkinter学习笔记 - 2】:Entry的使用、Button按钮的使用