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

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

Tkinter के साथ, हम Threading का उपयोग करके एक समय में multiple functions call कर सकते हैं। यह एप्लिकेशन में कुछ functions के asynchronous execution को प्रदान करता है।

Python में thread का उपयोग करने के लिए, हम threading नामक module import कर सकते हैं और इसकी Thread class को subclass कर सकते हैं। अपने नए class के अंदर, हमें Run method को overwrite करना होगा और वहां अपनी logic perform करनी होगी।

तो basically threading के साथ, हम एक समय में multiple work कर सकते हैं। अपने एप्लिकेशन में threading achieve करने के लिए, Tkinter Thread() फंक्शन प्रदान करता है।

आइए एक उदाहरण लें और एक thread बनाएं जो कुछ समय के लिए sleep करेगी और फिर parallel में दूसरी function execute करेगी।

इस उदाहरण के लिए, हम Time module और Tkinter library में परिभाषित threading module import करेंगे।

उदाहरण

# Import all the necessary libraries
from tkinter import *
import time
import threading

# Define the tkinter instance
win = Tk()

# Define the size of the tkinter frame
win.geometry("700x400")

# Define the function to start the thread
def thread_fun():
    label.config(text="You can Click the button or Wait")
    time.sleep(5)
    label.config(text="5 seconds Up!")
    label = Label(win)
    label.pack(pady=20)

# Create button
b1 = Button(win, text="Start", command=threading.Thread(target=thread_fun).start())
b1.pack(pady=20)

win.mainloop()

आउटपुट

उपरोक्त code चलाने से एक button और एक thread बनेगा जो label पर काम करता है।

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

5 seconds के बाद, thread automatically pause हो जाएगा।

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

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

    Tkinter एक window या frame बनाता है जो program execute करने के बाद दिखाई देता है। चूंकि Tkinter में सभी functions और modules independent हैं, हम window attributes को customize करने के लिए विशेष रूप से एक particular function का उपयोग कर सकते हैं। Tkinter हर एप्लिकेशन के लिए एक default root window बना

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

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

  1. Python Tkinter में Canvas पर Image Center कैसे करें? 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,