खोज…


परिचय

Tkinter में जारी Python की सबसे लोकप्रिय GUI (ग्राफिकल यूजर इंटरफेस) लाइब्रेरी है। यह विषय इस पुस्तकालय और इसकी विशेषताओं के उचित उपयोग की व्याख्या करता है।

टिप्पणियों

टिक्चर मॉड्यूल का पूंजीकरण पायथन 2 और 3 के बीच अलग है। पायथन 2 के लिए निम्नलिखित का उपयोग करें:

from Tkinter import *  # Capitalized

पायथन 3 के लिए निम्नलिखित का उपयोग करें:

from tkinter import *  # Lowercase

कोड के लिए जो पायथन 2 और 3 दोनों के साथ काम करता है, आप या तो कर सकते हैं

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

या

from sys import version_info
if version_info.major == 2:
    from Tkinter import *
elif version_info.major == 3:
    from tkinter import *

अधिक विवरण के लिए टिंकर दस्तावेज़ देखें

एक न्यूनतम टेंकर अनुप्रयोग

tkinter एक GUI टूलकिट है जो Tk / Tcl GUI लाइब्रेरी के चारों ओर एक आवरण प्रदान करता है और इसे Python के साथ शामिल किया जाता है। निम्नलिखित कोड tkinter का उपयोग करके एक नई विंडो बनाता है और विंडो बॉडी में कुछ टेक्स्ट tkinter है।

नोट: पायथन 2 में, पूंजीकरण थोड़ा अलग हो सकता है, नीचे दिए गए रिमार्क्स अनुभाग देखें।

import tkinter as tk

# GUI window is a subclass of the basic tkinter Frame object
class HelloWorldFrame(tk.Frame):
    def __init__(self, master):
        # Call superclass constructor
        tk.Frame.__init__(self, master)
        # Place frame into main window
        self.grid()
        # Create text box with "Hello World" text
        hello = tk.Label(self, text="Hello World! This label can hold strings!")
        # Place text box into frame
        hello.grid(row=0, column=0)

# Spawn window
if __name__ == "__main__":
    # Create main window object
    root = tk.Tk()
    # Set title of window
    root.title("Hello World!")
    # Instantiate HelloWorldFrame object
    hello_frame = HelloWorldFrame(root)
    # Start GUI
    hello_frame.mainloop()

ज्यामिति प्रबंधक

Tkinter में ज्यामिति प्रबंधन के लिए तीन तंत्र हैं: place , pack और grid

place प्रबंधक निरपेक्ष पिक्सेल निर्देशांक का उपयोग करता है।

pack प्रबंधक 4 पक्षों में से एक में विगेट्स रखता है। मौजूदा विजेट्स के आगे नए विजेट्स लगाए गए हैं।

grid प्रबंधक एक ग्रिड में डायनामिक रूप से आकार बदलने वाली स्प्रेडशीट के समान होता है।

स्थान

widget.place लिए सबसे आम कीवर्ड तर्क इस प्रकार हैं:

  • x , विजेट का पूर्ण x- समन्वय
  • y , विजेट का पूर्ण y- समन्वय
  • height , विजेट की पूर्ण ऊंचाई
  • width , विजेट की पूर्ण चौड़ाई

place का उपयोग करके एक कोड उदाहरण:

class PlaceExample(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        top_text=Label(master,text="This is on top at the origin")
        #top_text.pack()
        top_text.place(x=0,y=0,height=50,width=200)
        bottom_right_text=Label(master,text="This is at position 200,400")
        #top_text.pack()
        bottom_right_text.place(x=200,y=400,height=50,width=200)
# Spawn Window
if __name__=="__main__":
    root=Tk()
    place_frame=PlaceExample(root)
    place_frame.mainloop()

पैक

widget.pack निम्नलिखित खोजशब्द तर्क ले सकता है:

  • माता-पिता द्वारा छोड़ी गई जगह को भरना है या नहीं, इसका expand
  • fill , सभी जगह भरने के लिए विस्तार करना है (NONE (डिफ़ॉल्ट), X, Y या BOTH)
  • side , (TOP (डिफ़ॉल्ट), BOTTOM, LEFT, या अधिकार) के खिलाफ पैक करने का पक्ष

ग्रिड

widget.grid सबसे अधिक उपयोग किए जाने वाले कीवर्ड तर्क निम्नानुसार हैं:

  • row , विजेट की पंक्ति (डिफ़ॉल्ट सबसे छोटा)
  • rowspan , colums की संख्या एक विजेट स्पैन (डिफ़ॉल्ट 1)
  • column , विजेट का कॉलम (डिफ़ॉल्ट 0)
  • columnspan , स्तंभों की संख्या एक विजेट स्पैन (डिफ़ॉल्ट 1)
  • sticky , जहां विजेट सेल (यदि एन, एनई, ई, एसई, एस, एसडब्ल्यू, डब्ल्यू, एनडब्ल्यू का संयोजन) से बड़ा है, जहां रखा जाए

पंक्तियाँ और स्तंभ शून्य अनुक्रमित हैं। पंक्तियों में वृद्धि कम होती जा रही है, और कॉलम सही बढ़ते जा रहे हैं।

grid का उपयोग करके एक कोड उदाहरण:

from tkinter import *

class GridExample(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        top_text=Label(self,text="This text appears on top left")
        top_text.grid() # Default position 0, 0
        bottom_text=Label(self,text="This text appears on bottom left")
        bottom_text.grid() # Default position 1, 0
        right_text=Label(self,text="This text appears on the right and spans both rows",
                         wraplength=100)
        # Position is 0,1
        # Rowspan means actual position is [0-1],1
        right_text.grid(row=0,column=1,rowspan=2)

# Spawn Window
if __name__=="__main__":
    root=Tk()
    grid_frame=GridExample(root)
    grid_frame.mainloop()

कभी भी एक ही फ्रेम के भीतर pack और grid न मिलाएं! ऐसा करने से आवेदन गतिरोध पैदा होगा!



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow