是否可以将渐变颜色应用于 Tkinter python 小部件的 BG

编程入门 行业动态 更新时间:2024-10-26 23:37:09
本文介绍了是否可以将渐变颜色应用于 Tkinter python 小部件的 BG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是想将背景色添加为渐变色!这可能看起来我的 Tkinter-GUI 比常见的颜色代码更有吸引力.有没有办法在python中实现这一点,任何人都可以帮助我

I am just trying to add background colors as gradient colors! This may look my Tkinter-GUI more attractive than common color codes. Is there any way to achieve this in python, can anyone please help me

它可以包含这样的东西吗?

Can it incorporate something like this?

bg="40%,#207cca 40%,#2989d8 50%" def createwidgets(self): master_f = Tkinter.Frame (objMG.root, relief='sunken', bd=2,height =10,bg='#54596d') master_f.pack (side='top', fill='both', expand=1) self.Up_frame = Tkinter.Frame(master_f,relief='sunken',height=50,bg="#C0C0C0") self.Up_frame.pack(side='top',fill='x',expand='no') #self.Up_frame = Tkinter.Frame(master_f,relief='sunken',height=50,bg="40%,#207cca 40%,#2989d8 50%") #self.Up_frame.pack(side='top',fill='x',expand='no')

推荐答案

您不能为大多数小部件提供渐变背景,但您可以在画布上绘制渐变并使用画布代替框架作为容器.

You can't give most widgets a gradient background, but you can draw a gradient on a canvas and use the canvas instead of a frame as a container.

这个例子有点计算密集型,但它显示了绘制渐变背景的一般想法.优化留给读者作为练习.

This example is a little bit compute-intensive, but it shows the general idea of drawing a gradient background. Optimizations are left as an exercise for the reader.

import Tkinter as tk # py2 # import tkinter as tk # py3 class Example(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) f1 = GradientFrame(self, borderwidth=1, relief="sunken") f2 = GradientFrame(self, "green", "blue", borderwidth=1, relief="sunken") f1.pack(side="top", fill="both", expand=True) f2.pack(side="bottom", fill="both", expand=True) class GradientFrame(tk.Canvas): '''A gradient frame which uses a canvas to draw the background''' def __init__(self, parent, color1="red", color2="black", **kwargs): tk.Canvas.__init__(self, parent, **kwargs) self._color1 = color1 self._color2 = color2 self.bind("<Configure>", self._draw_gradient) def _draw_gradient(self, event=None): '''Draw the gradient''' self.delete("gradient") width = self.winfo_width() height = self.winfo_height() limit = width (r1,g1,b1) = self.winfo_rgb(self._color1) (r2,g2,b2) = self.winfo_rgb(self._color2) r_ratio = float(r2-r1) / limit g_ratio = float(g2-g1) / limit b_ratio = float(b2-b1) / limit for i in range(limit): nr = int(r1 + (r_ratio * i)) ng = int(g1 + (g_ratio * i)) nb = int(b1 + (b_ratio * i)) color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb) self.create_line(i,0,i,height, tags=("gradient",), fill=color) self.lower("gradient") if __name__ == "__main__": root = tk.Tk() Example(root).pack(fill="both", expand=True) root.mainloop()

更多推荐

是否可以将渐变颜色应用于 Tkinter python 小部件的 BG

本文发布于:2023-07-20 17:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   部件   颜色   Tkinter   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!