मान लीजिए हम Tkinter का उपयोग करके GUI-based एप्लिकेशन बना रहे हैं और हम Tkinter canvas में एक image load करना चाहते हैं।
Default रूप से, canvas images को इसकी width और height के अनुसार load करता है। हालांकि, हम 'Direction' value को anchor parameter में pass करके किसी भी direction (N, S, E, W, NS, EW, आदि) में image की position manipulate कर सकते हैं। Anchor एक parameter है जिसे image function के साथ invoke किया जाता है; यह canvas में image की direction या position define करता है।
Anchor parameters का उपयोग करके, हम text और images को किसी भी direction में align कर सकते हैं। अभी, हम Label function का उपयोग करके एक image label बनाएंगे जैसे कि:
Label(root, text=" ", other Options(color, width, height, ..))
उपरोक्त function का उपयोग करके image label बनाने के बाद, हम इसकी position 'anchor' property का उपयोग करके adjust करेंगे। चूंकि हमें image को center में place करना है, हम anchor का value "CENTER" pass करेंगे।
उदाहरण
# Import the tkinter library in the notebook
from tkinter import *
# Creating an instance of the tkinter canvas
win = Tk()
# Define the size of the window
win.geometry("700x150")
# Define the image label having some properties
label_img = Label(win, text="Hello World", font="sans-serif", relief="solid", width=20, height=8, anchor=CENTER)
label_img.pack()
# Displaying the canvas without closing the window
win.mainloop()
उपरोक्त snippet चलाने से output generate होगा और canvas के center में image place होगी।
आउटपुट
