Python全系列 教程
3567个小节阅读:5930.5k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
颜色选择框可以帮助我们设置背景色、前景色、画笔颜色、字体颜色等等。
【示例】颜色选择框基本用法
xxxxxxxxxx
"""askcolor颜色选择框的测试,改变背景色"""
from tkinter import *
from tkinter.colorchooser import *
root = Tk();root.geometry("400x150")
def test1():
s1 = askcolor(color="red", title="选择背景色")
print(s1)
# s1的值是:((0.0, 0.0, 255.99609375), '#0000ff')
root.config(bg=s1[1])
Button(root,text="选择背景色",command=test1).pack()
root.mainloop()
文件对话框帮助我们实现可视化的操作目录、操作文件。最后,将文件、目录的信息传入到程序中。文件对话框包含如下一些常用函数:
函数名 | 对话框 | 说明 |
---|---|---|
askopenfilename(**options) | 文件对话框 | 返回打开的文件名 |
askopenfilenames(**options) | 返回打开的多个文件名列表 | |
askopenfile(**options) | 返回打开文件对象 | |
askopenfiles(**options) | 返回打开的文件对象的列表 | |
askdirectory(**options) | 目录对话框 | 返回目录名 |
asksaveasfile(**options) | 保存对话框 | 返回保存的文件对象 |
asksaveasfilename(**options) | 返回保存的文件名 |
命名参数options的常见值如下:
参数名 | 说明 |
---|---|
defaultextension | 默认后缀:.xxx用户没有输入则自动添加 |
filetypes=[(label1,pattern1),(labe2,pattern2)] | 文件显示过滤器 |
initialdir | 初始目录 |
initialfile | 初始文件 |
parent | 父窗口,默认根窗口 |
title | 窗口标题 |
【示例】文件对话框基本用法
xxxxxxxxxx
"""文件对话框获取文件"""
from tkinter import *
from tkinter.filedialog import *
root = Tk();root.geometry("400x100")
def test1():
f = askopenfilename(title="上传文件",
initialdir="f:/file",filetypes=[("视频文件",".mp4")])
#print(f)
show["text"]=f
Button(root,text="选择编辑的视频文件",command=test1).pack()
show = Label(root,width=40,height=3,bg="green")
show.pack()
root.mainloop()
运行结果:
【示例】打开指定txt文件,并读出文件内容到窗口
xxxxxxxxxx
#coding=utf-8
#askcolor颜色选择框的测试,改变背景色
from tkinter import *
from tkinter.filedialog import *
root = Tk();root.geometry("400x100")
def test1():
with askopenfile(title="上传文件",
initialdir="d:",filetypes=[("文本文件",".txt")]) as f:
show["text"]=f.read()
Button(root,text="选择读取的文本文件",command=test1).pack()
show = Label(root,width=40,height=3,bg="green")
show.pack()
root.mainloop()