กล่องโต้ตอบเป็นส่วนประกอบที่สำคัญมากของแอปพลิเคชันใดๆ โดยทั่วไปจะใช้เพื่อโต้ตอบกับผู้ใช้และส่วนต่อประสานแอปพลิเคชัน เราสามารถสร้างไดอะล็อกบ็อกซ์สำหรับแอปพลิเคชัน tkinter โดยใช้หน้าต่างระดับบนสุดและวิดเจ็ตอื่น ๆ หน้าต่างระดับบนสุดจะปรากฏขึ้นเหนือหน้าต่างอื่นๆ ทั้งหมด ดังนั้นเราจึงสามารถเพิ่มสิ่งต่างๆ ได้มากขึ้นในหน้าต่างระดับบนสุดสำหรับกล่องโต้ตอบการสร้าง
ตัวอย่าง
ในตัวอย่างนี้ เราได้สร้างไดอะล็อกโมดอลซึ่งมีสองส่วน
- การเริ่มต้นของหน้าต่างระดับบนสุด
- นิยามฟังก์ชันสำหรับเหตุการณ์กล่องโต้ตอบป๊อปอัป
- การเพิ่มวิดเจ็ตในหน้าต่างระดับบนสุด
- นิยามฟังก์ชันสำหรับตัวเลือกไดอะล็อก
# 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() ผลลัพธ์
เมื่อเราเรียกใช้โค้ดข้างต้น จะแสดงหน้าต่างที่มีปุ่มเพื่อเปิดกล่องโต้ตอบโมดอล

การคลิกปุ่มจะเปิดกล่องโต้ตอบ Modal
