当前位置: 首页 > news >正文

python使用 tkinter 生成随机颜色

先看效果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

只要不停点击底部的按钮,每次都会生成新的颜色。炫酷啊。

import random
import tkinter
import tkinter.messagebox
from tkinter import Button# todo
"""
1. 设置一个按钮,来让用户选择是否显示颜色值
2. 把按钮换成 Label, 用户可以复制颜色。 
3. 换一个版本,自动生成随机颜色, 每2秒自动更换颜色。
"""win = tkinter.Tk()
win.title("Daily Tools")
win.geometry("1250x850+300+200")# 使用一个 frame, 后面用来清理其他的组件
frame = tkinter.Frame(win)
frame.pack(side="top", expand=True, fill="both")colors = ['red', 'green', 'blue', ' yellow', 'orange', 'white', 'cyan', 'purple']# 默认的调用函数。 测试使用的函数。
def what_todo():print("ok")# tkinter.messagebox.showinfo(title='Hey', message='Nice!')def make_button(button_text="这是一个按钮", button_function=what_todo):  ## 只有 button_text 和 button_function 是可变参数,其他都设置为一样的。多个按钮,样式一致,整整齐齐。color_text = "#" + ("%06x" % random.randint(0, 16777215))b = Button(frame,text=color_text,  # button_text,# 生成随机颜色,这里有很多种写法。 # https://stackoverflow.com/questions/19812288/how-to-make-the-foreground-and-background-colors-of-a-button-in-tkinter-random# bg=random.choice(colors),bg=color_text,  #command=button_function,fg="black",font=('黑体', 15),width=40,height=8,wraplength=0,  # 控制多少行来显示文本  默认是 0 可以修改为 3 多行显示。justify="center",  # 字体的对齐方向anchor="center",  # 这个 pos 控制的是文本内容在 label 框内的朝向relief="ridge",  # 边框效果 raised  ridge)print("cur color: ", color_text)return b# 生成3*3=9个随机颜色
def make_9_buttons():for i in range(3):for j in range(3):bb = make_button()bb.grid(row=i, column=j)# 生成新的颜色。
def generate_new_colors():for widget in frame.winfo_children():widget.destroy()  # 1. clear old onesmake_9_buttons()  # 2.print("\n\n")def main_button():b = Button(win,  # !!!!!!!!!! 注意这里是 win 不是 frametext='生成新的颜色',bg='#e05c5b',  # # cyancommand=generate_new_colors,fg="black",font=('黑体', 16),width=40,height=12,wraplength=0,  # 控制多少行来显示文本  默认是 0 可以修改为 3 多行显示。justify="center",  # 字体的对齐方向anchor="center",  # 这个 pos 控制的是文本内容在 label 框内的朝向relief="ridge",  # 边框效果 raised  ridge)b.pack(side="bottom", padx=10, pady=10)return b# 初始布局
make_9_buttons()
main_button()
win.mainloop()
http://www.lryc.cn/news/405975.html

相关文章:

  • 【Linux学习 | 第1篇】Linux介绍+安装
  • 设计模式-抽象工厂
  • Ubunton-24.04 简单配置使用
  • 什么是STP环路保护
  • Python算法基础:解锁冒泡排序与选择排序的奥秘
  • QtCMake工程提升类后找不到头文件
  • Docker核心技术:Docker原理之Cgroups
  • union的特性和大小端
  • 个性化IT服务探索实践
  • UE4-打包游戏,游戏模式,默认关卡
  • Unity ShaderLab基础
  • 用代理IP会频繁掉线是什么原因?HTTP和SOCKS5协议优劣势是什么?
  • MongoDB教程(十三):MongoDB覆盖索引
  • 快速认识EA(Enterprise Architecture)
  • 词云图制作
  • AndroidStudio与手机进行无线调试
  • 脉冲编码调制(PCM,Pulse Code Modulation)简介
  • Pytorch transforms 的研究
  • 一个C++模板工厂的编译问题的解决。针对第三方库的构造函数以及追加了的对象构造函数。牵扯到重载、特化等
  • 《昇思 25 天学习打卡营第 20 天 | Pix2Pix实现图像转换 》
  • 关于c#的简单应用三题
  • (十三)Spring教程——依赖注入之工厂方法注入
  • Redission中的Lua脚本写法、理解
  • 视频共享融合赋能平台LntonCVS视频监控管理平台视频云解决方案
  • GraphRAG + GPT-4o mini 低成本构建 AI 图谱知识库
  • 全国区块链职业技能大赛第十套区块链产品需求分析与方案设计
  • 分布式Apollo配置中心搭建实战
  • Android monkey命令和monkey脚本详解
  • vue 实现对图片的某个区域点选, 并在该区域上方显示该部分内容
  • 配置文件格式 INI 快速上手