Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 是否可以将渐变颜色应用于Tkinter python小部件的背景_Python 2.7_Tkinter - Fatal编程技术网

Python 2.7 是否可以将渐变颜色应用于Tkinter python小部件的背景

Python 2.7 是否可以将渐变颜色应用于Tkinter python小部件的背景,python-2.7,tkinter,Python 2.7,Tkinter,我只是想添加背景色作为渐变色!这可能看起来我的Tkinter GUI比普通的颜色代码更有吸引力。有什么方法可以在python中实现这一点,有人可以帮助我吗 它能包含这样的东西吗 bg="40%,#207cca 40%,#2989d8 50%" def createwidgets(self): master_f = Tkinter.Frame (objMG.root, relief='sunken', bd=2,height =10,bg='#54596d')

我只是想添加背景色作为渐变色!这可能看起来我的Tkinter GUI比普通的颜色代码更有吸引力。有什么方法可以在python中实现这一点,有人可以帮助我吗

它能包含这样的东西吗

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')

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

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

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作为tk#py2导入
#将tkinter作为tk#py3导入
类示例(tk.Frame):
定义初始化(自身,父级):
tk.Frame.\uuuu init\uuuuu(自,父)
f1=渐变框架(自,边框宽度=1,浮雕=“凹陷”)
f2=渐变框架(自身,“绿色”、“蓝色”,边框宽度=1,浮雕=“凹陷”)
f1.打包(side=“top”,fill=“both”,expand=True)
包装(side=“bottom”,fill=“both”,expand=True)
类GradientFrame(tk.Canvas):
''使用画布绘制背景的渐变帧''
定义初始值(自身、父项、color1=“红色”、color2=“黑色”**kwargs):
tk.Canvas.\uuuuu init\uuuuuuuuu(自我、父级、**kwargs)
self.\u color1=color1
self.\u color2=color2
self.bind(“,self.\u draw\u gradient)
def _draw _渐变(自,事件=无):
“绘制渐变”
删除(“梯度”)
width=self.winfo_width()
高度=self.winfo_高度()
极限=宽度
(r1,g1,b1)=self.winfo_rgb(self._color1)
(r2,g2,b2)=self.winfo_rgb(self._color2)
r_比率=浮动(r2-r1)/极限
g_比率=浮动(g2-g1)/极限
b_比率=浮动(b2-b1)/极限
对于范围内的i(限制):
nr=int(r1+(r_比率*i))
ng=int(g1+(g_比*i))
nb=int(b1+(b_比*i))
color=“#%4.4x%4.4x%4.4x”%(nr、ng、nb)
创建线(i,0,i,高度,标记=(“渐变”),填充=颜色)
自低(“梯度”)
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
示例(root).pack(fill=“both”,expand=True)
root.mainloop()

@kakyo:“似乎坏了”有点模糊。@BryanOakley使用此方法,我无法在
示例
框架上呈现标签文本,我只看到渐变。你有什么建议吗?标签是否在渐变下?使用标准的
框架
可以很好地显示文本。有没有办法将渐变应用于菜单按钮?有没有办法在画布矩形上实现这一点?@CoolCloud:没有,矩形只能有一种颜色。但是,为了模拟具有渐变的矩形,您当然可以在任何像素范围内使用类似的技术来绘制矩形。