색상 그라디언트는 위치 종속 색상의 범위를 정의합니다. 좀 더 구체적으로 말하자면, 일부 색상 범위(그라데이션)가 포함된 애플리케이션에서 직사각형 스케일을 생성하려는 경우 다음 단계를 따를 수 있습니다. -
-
캔버스 위젯으로 사각형을 만들고 너비와 높이를 정의합니다.
-
범위의 색을 채우는 함수를 정의합니다. 색상을 채우기 위해 튜플 내부에 16진수 값을 사용할 수 있습니다.
-
색상 범위를 반복하고 사각형을 채웁니다.
예시
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the window
win.geometry("700x350")
win.title("Gradient")
# Define a function for filling the rectangle with random colors
def rgb(r, g, b):
return "#%s%s%s" % tuple([hex(c)[2:].rjust(2, "0")
for c in (r, g, b)])
# Define gradient
gradient = Canvas(win, width=255 * 2, height=25)
gradient.pack()
# Iterate through the color and fill the rectangle with colors(r,g,0)
for x in range(0, 256):
r = x * 2 if x < 128 else 255
g = 255 if x < 128 else 255 - (x - 128) * 2
gradient.create_rectangle(x * 2, 0, x * 2 + 2, 50, fill=rgb(r, g, 0), outline=rgb(r, g, 0))
win.mainloop() 출력
위의 코드를 실행하면 몇 가지 색상 범위가 정의된 스케일 그라디언트가 표시됩니다.
