Python GUI tkinter库 自学20

颜色框 文件选择框及读取文件内容

  • 1、颜色框
  • 2、文件选择框
  • 3、读取文件内容

1、颜色框

from tkinter import *
from tkinter.colorchooser import *

window = Tk()
window.geometry("500x200")


def test1():
    s1 = askcolor(color="red", title="选择背景色")
    print(s1)
    # s1 的值是:((255.99609375, 0.0, 0.0), '#ff0000')
    window.config(bg=s1[1])


Button(window, text="选择背景色", command=test1).pack()

window.mainloop()

2、文件选择框

from tkinter import *
from tkinter.filedialog import *

window = Tk()
window.geometry("500x200")


def test1():
    f1 = askopenfilename(title="上传文件",
                         initialdir="d:", filetypes=[("视频文件", ".mp4")])
    # print(f1)
    show["text"] = f1


Button(window, text="选择编辑的视频文件", command=test1).pack()

show = Label(window, width=10, height=5, bg="green")
show.pack()

window.mainloop()

3、读取文件内容

from tkinter import *
from tkinter.filedialog import *

window = Tk()
window.geometry("500x200")


def test1():
    with askopenfile(title="上传文件",
                     initialdir="d:", filetypes=[("文本文件", ".txt")]) as f:

        show["text"] = f.read()


Button(window, text="选择读取的文本文件", command=test1).pack()

show = Label(window, width=10, height=5, bg="green")
show.pack()

window.mainloop()

你可能感兴趣的:(Python,GUI,tkinter,库,tkinter,python)