代码11

"""askcolor颜色选择框测试,改变背景色"""
from tkinter import *
from tkinter.colorchooser import *

# 创建主窗口
root = Tk()
root.geometry("400x150")  # 设置窗口大小

# 定义函数,用于调用颜色选择框并改变背景色
def func():
    # 调用颜色选择框,初始颜色为红色,对话框标题为"选择背景色"
    color_tuple = askcolor(color="red", title="选择背景色")
    # color_tuple的格式为:((r, g, b), '#RRGGBB'),其中第二个元素为十六进制颜色值
    selected_color = color_tuple[1]
    # 将主窗口的背景色设置为选择的颜色
    root.config(bg=selected_color)


# 创建按钮,点击按钮调用func函数
Button(root, text="选择背景色", command=func).pack()

# 启动主事件循环
root.mainloop()

posted @ 2024-04-29 15:02  thanksDay  阅读(20)  评论(0)    收藏  举报