In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "tkinter how to use js's canvas to achieve gradient", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "tkinter how to use js's canvas to achieve gradient" bar!
1. Use rgb to represent colors
Tkinter does not provide a function that uses rgb as a parameter, so you need to convert the value of hexadecimal to the value of grb. Of course, the method is also very simple, that is, to operate on hexadecimal.
Code:
Def use_rgb (rgb): "" convert rgb to hexadecimal Args: rgb: rgb color Returns: hexadecimal "rgb = str (rgb) RGB = rgb.replace ('(','). Replace (") ",'). Split (' ) # divide the RGB format into color ='# 'for i in RGB: num = int (I) # convert R, G, B to hexadecimal splicing conversion and uppercase hex () function to convert decimal integers to hexadecimal Represents color + = str (hex (num)) [- 2:] .replace ('x','0') return color as a string
Because the incoming rgb is in tuple form, it is converted to a string, and then to a hexadecimal string. Remember to add # before it.
2. Tkinter canvas component
Canvas components are used to draw things in the tkinter library, and you can draw line segments, rectangles, polygons, arcs, etc.
To use the canvas component, you need to create a window object as the parent of the canvas.
Import tkinter as tk# initializes the tkinter component first, creating a window object window = tk.Tk () # sets the title of the window, window.title ("title") window.geometry ("800x600")
Nothing happens after running, because the window still needs to be displayed
Window.mainloop ()
And then there is the little frame of nocturnal light.
The creation of canvas is also the instantiation of the creation class, which can be instantiated without parameters, and then adjusted later, or instantiated at the same time as the creation.
# use canvascanvas = window.Canvas ()
You can also:
# window is the parent object of canvas. Width and height can see the width and height of canvas. Canvas = tk.Canvas (window, width=800, height=600) # this method can set the layout mode, of course, it is also the method of displaying canvas canvas.pack ()
Of course, there is nothing after running at this time, and we need to draw something on the canvas.
Then we draw a rectangle through canvas
Canvas.create_rectangle (100,100,300,300, fill= "red") # this line of code can also write canvas.create_rectangle ((100,100,300,300), fill= "red")
So you draw a red rectangle.
3. Set gradient
The gradient here is not written directly on the rectangle, but needs to use line segments, each of which displays a color, and then forms the effect of a gradient.
The way to draw line segments is:
Canvas.create_line ()
The form of the parameters inside is similar to that of the line segment above, except that the line segment only needs two coordinates.
3.1 the principle of gradual change
The simple principle is to set one color from dark to light, and then to another color from light to deep.
It's not easy to say, but it's a little difficult to achieve.
Our train of thought is:
Circular line drawing segment
Calculate the color of each segment
When we draw a line segment, we only need to calculate these three parameters:
Length segment start point x coordinates line segment start point y coordinate of the rectangle
The starting point here is not the starting point, but the upper point of the line segment.
We also need to know the RGB values of the two colors we need to gradient.
On the other hand, we only need to know the increment of a line segment to the beginning, and then combine it with rgb, which is the color of a line segment.
3.2 instance 1
Turn the red rectangle into a red and blue gradient from left to right
Red GRB value (255,0,0)
Blue RGB value (0,0,255)
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Author: Smly# @ datetime: 2021-12-4 19 utf-8 @ Version: 1.0import tkinter as tkRED = (255,0,0) BLUE = (0,0) Def use_rgb (rgb): "" convert rgb to hexadecimal Args: rgb: rgb color Returns: hexadecimal "rgb = str (rgb) RGB = rgb.replace ('(','). Replace (") ",'). Split (' ) # divide the RGB format into color ='# 'for i in RGB: num = int (I) # convert R, G, B to hexadecimal splicing conversion and uppercase hex () function to convert decimal integers to hexadecimal Express color + = str (hex (num)) [- 2:] .replace ('xexamples,' 0') return color# initialize the tkinter component first and create a window object window = tk.Tk () # to set the title of the window Length and width window.title ("title") window.geometry ("800x600") # use canvascanvas = tk.Canvas (window, width=800, height=600) canvas.pack () A1, a2, a3, b1, b2, b3 = RED [0], RED [1], RED [2], BLUE [0], BLUE [1], BLUE [2] # rgbr, g, b = (b1-A1), (b2-a2), (b3-a3) print (r, g) B) h = 200for i in range (200): x1 = 100 + Iy1 = 100t = (x1-100) / (300,100t) rgb = (int (A1 + r * t), int (a2 + g * t), int (a3 + b * t) print (rgb) canvas.create_line ((x1, y1), (x1, y1 + h), fill=use_rgb (rgb)) window.mainloop ()
Effect:
Thank you for reading, the above is the content of "tkinter how to use js's canvas to achieve gradient". After the study of this article, I believe you have a deeper understanding of how tkinter uses js's canvas to achieve gradient, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.