Python内置包Tkinter的重要控件(上)

简介: 学习了这么久的Tkinter,基本上把Tkinter的重要控件都学了一遍,本文主要对其所有重要控件以及重要函数做一个总结,加深对Tkinter的理解与应用。

前言

包括但不限于:
① Label(标签)
② Button (按钮)
③ Entry(输入框)
④ Text(文本框)
⑤ Menu(菜单栏)
⑥ Messagebox(提示框)
⑦ Canvas(画布)
⑧ Listbox(选项框)
⑨ Checkbutton(复选框)
⑩ Radiobutton(单选框)
本文主要介绍前五个,即Label,Button,Entry,Text ,Menu

控件

1. Label

import tkinter as tk
root=tk.Tk()
root.title('Label')
root.geometry('500x300')
L=tk.Label(root,text='Label控件',bg='white',fg='blue',font=('宋体',12),width=20,height=2)
L.pack()
root.mainloop()

2. Button

import tkinter as tk
root=tk.Tk()
root.title('Button')
root.geometry('500x300')
B=tk.Button(root,text='Button控件',bg='blue',fg='white',width=10,height=1,command=None)
B.pack()
root.mainloop()

3. Entry

import tkinter as tk
root=tk.Tk()
root.title('Entry')
root.geometry('500x300')
var=tk.StringVar()
var.set('Entry')
E=tk.Entry(root,textvariable=var)
E.place(x=20,y=20)
root.mainloop()

4. Text

import tkinter as tk
root=tk.Tk()
root.title('Text')
root.geometry('500x300')
T=tk.Text(root,bg='white',fg='black',width=20,height=3)
T.place(x=20,y=20)
T.insert(1.0,'Text')
root.mainloop() 

5. Menu

import tkinter as tk
root=tk.Tk()
root.title('Menu')
root.geometry('500x300')
m=tk.Menu(root)
fm=tk.Menu(m,tearoff=0)
cm=tk.Menu(m,tearoff=0)
m.add_cascade(label='菜单',menu=fm)
m.add_cascade(label='选择',menu=cm)
fm.add_command(label='开始',command=None)
fm.add_separator()
fm.add_command(label='结束',command=None)
root.config(menu=m)
root.mainloop()

总结

本文介绍的五个控件是最基本的控件,下文将对后面五个控件进行简单总结。

目录
相关文章
|
2天前
|
安全 Python 容器
|
1天前
|
Python
Python中字典解包
【6月更文挑战第21天】
7 2
|
1天前
|
Python
Python中解包使用星号(*)进行灵活解包
【6月更文挑战第21天】
7 2
|
1天前
|
安全 Python 容器
Python中解包元素数量匹配
【6月更文挑战第21天】
7 2
|
4天前
|
Python
Python中元组解包
【6月更文挑战第18天】
12 5
|
4天前
|
Python
如何查询Python包的所有历史版本
如何查询Python包的所有历史版本
14 5
|
5天前
|
Python
python中迭代器的解包
【6月更文挑战第17天】
15 4
|
4天前
|
存储 Python
Python中列表解包
【6月更文挑战第18天】
12 2
|
1天前
|
索引 Python
技术好文共享:用Python的Pygame包做飞行棋
技术好文共享:用Python的Pygame包做飞行棋
|
4天前
|
Python
Python包 - networkx详细拆解
Python包 - networkx详细拆解