डायलॉग बॉक्स किसी भी एप्लिकेशन का एक बहुत ही आवश्यक घटक हैं। यह आमतौर पर उपयोगकर्ता और एप्लिकेशन इंटरफ़ेस के साथ बातचीत करने के लिए उपयोग किया जाता है। हम टॉपलेवल विंडो और अन्य विजेट्स का उपयोग करके किसी भी टिंकर एप्लिकेशन के लिए डायलॉग बॉक्स बना सकते हैं। टॉपलेवल विंडो अन्य सभी विंडो के ऊपर की सामग्री को पॉप अप करती है। इस प्रकार, हम डायलॉग बॉक्स बनाने के लिए टॉपलेवल विंडो पर और सामान जोड़ सकते हैं।
उदाहरण
इस उदाहरण में, हमने एक मोडल डायलॉग बनाया है जिसमें दो भाग हैं,
- शीर्ष स्तरीय विंडो का प्रारंभ।
- पॉपअप डायलॉग इवेंट के लिए फंक्शन डेफिनिशन।
- शीर्ष स्तरीय विंडो में विजेट जोड़ना।
- संवाद विकल्पों के लिए कार्य परिभाषा।
# Import required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the window size
win.geometry("700x350")
style = ttk.Style()
style.theme_use('clam')
# Define a function to implement choice function
def choice(option):
pop.destroy()
if option == "yes":
label.config(text="Hello, How are You?")
else:
label.config(text="You have selected No")
win.destroy()
def click_fun():
global pop
pop = Toplevel(win)
pop.title("Confirmation")
pop.geometry("300x150")
pop.config(bg="white")
# Create a Label Text
label = Label(pop, text="Would You like to Proceed?",
font=('Aerial', 12))
label.pack(pady=20)
# Add a Frame
frame = Frame(pop, bg="gray71")
frame.pack(pady=10)
# Add Button for making selection
button1 = Button(frame, text="Yes", command=lambda: choice("yes"), bg="blue", fg="white")
button1.grid(row=0, column=1)
button2 = Button(frame, text="No", command=lambda: choice("no"), bg="blue", fg="white")
button2.grid(row=0, column=2)
# Create a Label widget
label = Label(win, text="", font=('Aerial', 14))
label.pack(pady=40)
# Create a Tkinter button
ttk.Button(win, text="Click Here", command=click_fun).pack()
win.mainloop() आउटपुट
जब हम उपरोक्त कोड चलाते हैं, तो यह मोडल डायलॉग बॉक्स खोलने के लिए एक बटन के साथ एक विंडो प्रदर्शित करेगा।

बटन पर क्लिक करने पर मोडल डायलॉग बॉक्स खुल जाएगा।
