कैनवास विजेट टिंकर लाइब्रेरी में बहुपक्षीय विजेट्स में से एक है जिसका उपयोग किसी भी एप्लिकेशन के लिए ग्राफिक्स प्रदान करने के लिए किया जाता है। इसका उपयोग आकृतियों, छवियों, एनिमेटिंग वस्तुओं या किसी भी जटिल दृश्य को खींचने के लिए किया जा सकता है। अल्फा आकृति का गुण परिभाषित करता है कि यदि हम अल्फा . देते हैं किसी भी आकार के लिए मूल्य, तो इसकी मूल विंडो के संबंध में कुछ पारदर्शिता व्यवहार होना चाहिए।
अल्फा गुण को परिभाषित करने के लिए, हमें यह मानना होगा कि हर आकृति में कुछ रंग होते हैं और जब भी हम किसी आकृति को अल्फा मान प्रदान करते हैं, तो उसे एक छवि में परिवर्तित किया जाना चाहिए। इमेज को कैनवास विजेट का उपयोग करके प्रदर्शित किया जा सकता है।
उदाहरण
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame
win= Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Store newly created image
images=[]
# Define a function to make the transparent rectangle
def create_rectangle(x,y,a,b,**options):
if 'alpha' in options:
# Calculate the alpha transparency for every color(RGB)
alpha = int(options.pop('alpha') * 255)
# Use the fill variable to fill the shape with transparent color
fill = options.pop('fill')
fill = win.winfo_rgb(fill) + (alpha,)
image = Image.new('RGBA', (a-x, b-y), fill)
images.append(ImageTk.PhotoImage(image))
canvas.create_image(x, y, image=images[-1], anchor='nw')
canvas.create_rectangle(x, y,a,b, **options)
# Add a Canvas widget
canvas= Canvas(win)
# Create a rectangle in canvas
create_rectangle(50, 110,300,280, fill= "blue", alpha=.3)
create_rectangle(60, 90,310,250, fill= "red", alpha=.3)
canvas.pack()
win.mainloop() आउटपुट
यह देखने के लिए उपरोक्त कोड चलाएँ कि अल्फ़ा गुण आकार में कैसे भिन्न होता है।
