tkinter小部件4

13.滚动条Scrollbar

常用参数:

  • activebackground光标经过滚动条时滚动条的颜色
  • command
  • elementborderwidth滚动条宽度
  • jump触发command的条件
  • orient可选值horizonal(水平滚动条)/vertical(垂直滚动条)
  • troughcolor滚动条凹槽颜色
  • takefocus是否开始Tap键快速获取焦点
    scrollbar.py
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

scrollbar = ttk.Scrollbar(root)
scrollbar.pack(fill='y', side='right')

listbox = tk.Listbox(root, yscrollcommand=scrollbar.set)
listbox.pack(side='left', fill='both' ,expand=True)

for i in range(1, 100):
    listbox.insert(tk.END, i)
# 设置滚动条可以持续拖动
scrollbar.config(command=listbox.yview)

root.mainloop()

14.选项列表OptionMenu

从预设的选项中选择
tk版本
optionmenu = tk.OptionMenu(root, lang_var, *choices)
ttk版本
optionmenu = ttk.OptionMenu(root, lang_var, *choices)
位置参数:

  • 第一个,小部件放置的位置,常传入根窗口root或空白面板Frame
  • 第二个,变量StringVar类型
  • 第三个及其之后,选项值,为了书写简便可以将选项值写入一个列表或元组中,然后在第三个位置参数传入*列表名
    optionmenu.py
import tkinter as tk 
from tkinter import ttk


root = tk.Tk()

lang_var = tk.StringVar()
choices = ('python', 'css', 'html')
# tk版本
# optionmenu = tk.OptionMenu(root, lang_var, *choices)
# ttk版本
optionmenu = ttk.OptionMenu(root, lang_var, *choices)
optionmenu.pack()

def on_submit():
    print(lang_var.get())

button = ttk.Button(root, text='提交', command=on_submit)
button.pack()

root.mainloop()

15.下拉列表框ttk.Combobox

ttk中新增的小部件,相当于Listbox和Entry的结合版,可以使用键盘输入的下拉列表框
常用参数:

  • textvariable变量,StringVar类型
  • values可选值列表
  • postcommand在listbox显示之前执行的回调函数
  • justify框中文本的对齐方式,可选值left/right/center
    常用方法:
  • current()传入选项的下标,设置默认选项
    combobox.py
import tkinter as tk
from tkinter import ttk


root = tk.Tk()

choices = ['python', 'css', 'html']
lang_var = tk.StringVar()
combobox = ttk.Combobox(root, textvariable=lang_var, values=choices)
# 通过选项列表下标设置默认选项
combobox.current(1)
combobox.pack()

def on_submit():
    print(lang_var.get())

button = ttk.Button(root, text='提交', command=on_submit)
button.pack()

root.mainloop()

16.箭头输入框Spinbox

Spinbox 类似于 Entry,但具有箭头按钮,可以增加或减少框中的数字。
spinbox.py

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# tk版本
# spinbox = tk.Spinbox(root, from_=0, to=1000, increment=1)
# ttk版本
spinbox = ttk.Spinbox(root, from_=0, to=1000, increment=1)
spinbox.pack()

def on_submit():
    print(spinbox.get())

button = tk.Button(root, text='提交', command=on_submit)
button.pack()

root.mainloop()

常用参数:

  • from_可以递减或递增到的最小值,特殊值负无穷大’-infinity’/‘-inf’
  • to可以递减或递增到的最大值。特殊值无穷大’infinity’/‘inf’。指定了from_或to中的一个,另一个会自动默认为0。
  • increment设置箭头按钮增加或减少数字的量
  • textvariable变量
  • command回调函数,当任一按钮被按下时要执行的回调
  • values按钮将滚动浏览的选择集。覆盖from_和to值。列表
  • format格式化的字符串
  • repeatdelay
  • wrap单击上下方向按键可以让数值重新开始
  • xscrollcommand在x轴使用滚动条

17.面板PanedWindow

类似于Frame和Labelframe,用于排版
常用参数:

  • handlepad面板显示宽度,默认8
  • orient面板方向
  • sashcursor分割线光标
  • sashrelief面板分割线外框,默认raised
  • showhandle滑块属性
    常用方法:
  • add()在面板上添加小部件
import tkinter as tk
from tkinter import ttk


root = tk.Tk()

panedwindow = ttk.PanedWindow(root, orient='horizontal')
panedwindow.pack(fill='both', expand=True)

labelframe1 = ttk.Labelframe(root, text='标签1', width=120, height=150)
# 三个labelframe小部件的weight=1,可以确保均分窗口宽度
panedwindow.add(labelframe1, weight=1)
labelframe2 = ttk.Labelframe(root, text='标签2', width=120)
panedwindow.add(labelframe2, weight=1)
labelframe3 = ttk.Labelframe(root, text='标签3', width=120)
panedwindow.add(labelframe3, weight=1)

root.mainloop()

18.进度条ttk.Progressbar

常用参数:

  • length进度条长度,默认100像素
  • mode进度条模式,可选值determination(默认,一次)/indetermination(循环)
  • maximum进度条最大值,默认100
  • name进度条名称
  • orient进度条方向
  • value进度条当前值
  • variable变量
    常用方法:
  • start()传入时间,每隔多少时间移动一次指针,单位毫秒
  • step()传入一个数字,每次移动增加的值
  • stop()停止start()
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

progress = ttk.Progressbar(root, orient="horizontal", length=200, mode="determinate")
progress.pack()
progress.start(100)  # 开始进度条

root.mainloop()

19.选项卡ttk.Notebook

常用参数:

  • padding外边框
    常用方法:add()在选项卡添加小部件,第一个参数是要添加的小部件
    add()的可选参数:
  • compound
  • image
  • padding外边距
  • state状态
  • sticky
  • text
  • underline
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

notebook = ttk.Notebook(root)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text="选项卡1")
notebook.add(frame2, text="选项卡2")
label = ttk.Label(frame1, text='测试')
label.pack()
notebook.pack()

root.mainloop()

20.菜单Menu

常用参数:

  • tearoff菜单上方的分割线,可选值布尔值
  • underline下划线,alt快捷键
    常用方法:
  • add_cascade()建立分层菜单
  • add_command()增加菜单列表
  • add_separator()增加菜单列表的分割线
import tkinter as tk
from tkinter import ttk, messagebox


root = tk.Tk()
menu_bar = tk.Menu(root)
def begin():
    messagebox.showinfo('开始', '欢迎使用Tkinter!')

menu_bar.add_command(label='开始', command=begin)
menu_bar.add_command(label='退出', command=root.destroy)
root.config(menu=menu_bar)

root.mainloop()
import tkinter as tk 
from tkinter import ttk, messagebox


root = tk.Tk()

menu_bar = tk.Menu(root)
# 创建一个选项卡‘文件’的菜单
file_menu = tk.Menu(menu_bar, tearoff=False)# 设置tearoff=False去除分割线
menu_bar.add_cascade(label='File文件', menu=file_menu, underline=0)# 使用underline创建快捷键,有下划线的字母就是快捷键,弃用快捷键需要先按alt键
# 文件的子菜单查找
find_menu = tk.Menu(file_menu, tearoff=False)
file_menu.add_cascade(label='Find查找', menu=find_menu)
def find_pre():
    messagebox.showinfo('上一个', '查找上一个')
find_menu.add_command(label='Pre上一个', command=find_pre)
def find_next():
    messagebox.showinfo('下一个', '查找下一个')
find_menu.add_command(label='Next下一个', command=find_next)

def new_file():
    messagebox.showinfo('新建', '新建文档')
file_menu.add_command(label='New新建', command=new_file, underline=0)

def open_file():
    messagebox.showinfo('打开', '打开文档')
file_menu.add_command(label='Open打开', command=open_file, underline=0)

file_menu.add_separator()# 添加分割线

def save_file():
    messagebox.showinfo('保存', '保存文档')
file_menu.add_command(label='Save保存', command=save_file, underline=0)

def save_as_file():
    messagebox.showinfo('另存为', '另存为其他格式')
file_menu.add_command(label='Save As另存为', command=save_as_file, underline=2)

file_menu.add_separator()# 添加分割线

file_menu.add_command(label='Exit退出', command=root.destroy, underline=0)

# 创建一个选项卡‘帮助’的菜单
help_menu = tk.Menu(menu_bar, tearoff=False)# 设置tearoff=False去除分割线
menu_bar.add_cascade(label='Help帮助', menu=help_menu, underline=0)

def about():
    messagebox.showinfo('关于', '联系我们')
help_menu.add_command(label='About关于', command=about, underline=1)

root.config(menu=menu_bar)


# 右键菜单
right_menu = tk.Menu(root, tearoff=False)
def minimize():
    root.iconify()
right_menu.add_command(label='最小化', command=minimize)
right_menu.add_command(label='退出', command=root.destroy)
# 与鼠标右键绑定
def show_menu(event):
    # 在鼠标光标处弹出右键菜单
    right_menu.post(event.x_root, event.y_root)
root.bind('<Button-3>', show_menu)

root.mainloop()

21.树状表格ttk.Treeview

常用参数:

  • columns
  • displaycolumns栏的显示顺序
  • padding内容与表格单元格边框的间距
  • selectmode选择模式,可选值browse(默认,单选)/extended(多选)/none(禁止鼠标选中)
  • show可选值tree(默认,显示#0栏)/headings(不显示#0栏)
  • takefocus
import tkinter as tk
from tkinter import ttk


root = tk.Tk()

tree = ttk.Treeview(root, columns=("姓名", "年龄"))
tree.heading("#0", text="ID")
tree.heading("姓名", text="姓名")
tree.heading("年龄", text="年龄")
tree.insert("", "end", text="1", values=("张三", 25))
tree.insert("", "end", text="2", values=("李四", 30))
tree.pack()

root.mainloop()

22.分割线ttk.Separator

常用参数:

  • orient分割线方向,可选值horizontal(水平分割线)/vertical(垂直分割线)
import tkinter as tk
from tkinter import ttk


root = tk.Tk()

label1_text = 'HTML(超文本标记语言)'
label1 = ttk.Label(root, text=label1_text, font=('微软雅黑', 25, 'bold'))
label2_text = """
HTML(超文本标记语言——HyperText Markup Language)是构成 Web 世界的一砖一瓦。它定义了网页内容的含义和结构。除 HTML 以外的其他技术则通常用来描述一个网页的表现与展示效果(如 CSS),或功能与行为(如 JavaScript)。
“超文本”(hypertext)是指连接单个网站内或多个网站间的网页的链接。链接是网络的一个基本方面。只要将内容上传到互联网,并将其与他人创建的页面相链接,你就成为了万维网的积极参与者。
"""
label2 = ttk.Label(root, text=label2_text)
separator = ttk.Separator(root, orient='horizontal')
label1.pack(padx=20, pady=10)
separator.pack(fill='x', padx=400)
label2.pack(padx=20, pady=10)

root.mainloop()

23.画布Canvas

常用方法:
  1. create_line()绘制线条,参数是点的坐标值,即x1,y1,x2,y2,…
    可选参数:
    • arrow箭头,可选值first(第一条线末端箭头)/last(最后一条线末端箭头)/both(两端箭头)
    • arrowshap使用三元素元组自定义箭头,三角形
    • capstyle线条终点样式,可选值butt/projecting/pound
    • dash自定义虚线
    • dashoffset自定义虚线
    • fill线条颜色
    • joinstyle线条相交设置,可选值pound/bevel/miter
    • stipple绘制位图bitmap线条
    • width线条宽度
import tkinter as tk
from math import sin, cos, pi


root = tk.Tk()

canvas = tk.Canvas(root, width=640, height=480)
canvas.pack()

x_center, y_center, r = 320, 240, 100
x, y = [], []

for i in range(12):
    x.append(x_center + r * cos(30 * i *pi / 180))
    y.append(y_center + r * sin(30 * i *pi / 180))

for i in range(12):
    for j in range(12):
        canvas.create_line(x[i], y[i], x[j], y[j])

root.mainloop()
  1. create_tectangle()绘制矩形
  2. create_arc()绘制圆弧
  3. create_oval()绘制圆形或椭圆
  4. create_polygon()绘制多边形
  5. create_text()输出文字
  6. create_image()插入图像
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

复习法处理好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值