Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python Tkinter में Canvas पर Image Center कैसे करें?

मान लीजिए हम 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 होगी।

आउटपुट

Python Tkinter में Canvas पर Image Center कैसे करें?

  1. Tkinter Canvas कैसे क्लियर करें? Tkinter Canvas कैसे क्लियर करें?

    Tkinter विंडो में कैनवास जोड़ने का एक तरीका प्रदान करता है और जब हम कैनवास बनाते हैं, तो यह मेमोरी के अंदर कुछ स्टोरेज रैप करता है। Tkinter में कैनवास बनाते समय, यह प्रभावी रूप से कुछ मेमोरी खाता है जिसे क्लियर या डिलीट करने की आवश्यकता होती है। कैनवास क्लियर करने के लिए, हम delete() मेथड का उपयोग

  1. Tkinter Python में Thread का उपयोग कैसे करें? Tkinter Python में Thread का उपयोग कैसे करें?

    Tkinter के साथ, हम Threading का उपयोग करके एक समय में multiple functions call कर सकते हैं। यह एप्लिकेशन में कुछ functions के asynchronous execution को प्रदान करता है। Python में thread का उपयोग करने के लिए, हम threading नामक module import कर सकते हैं और इसकी Thread class को subclass कर सकते हैं। अपन

  1. Tkinter में Image को Button के रूप में कैसे उपयोग करें? Tkinter में Image को Button के रूप में कैसे उपयोग करें?

    इस उदाहरण में, हम एक window में rounded button बनाएंगे जिसका उपयोग forms, games, dialogue boxes आदि जैसे कई अन्य एप्लिकेशन में किया जा सकता है। Tkinter में rounded buttons बनाने का सबसे अच्छा तरीका buttons की desired images का उपयोग करना और इसे frame में clickable button में बदलना है। यह PhotoImage(