Tkinter มีฟังก์ชันและโมดูลที่ฝังอยู่ภายในจำนวนมากซึ่งใช้งานแล้วใน Python กล่องข้อความ โมดูลใน Tkinter เป็นหนึ่งในนั้นที่สามารถใช้ในแอปพลิเคชันใด ๆ เพียงแค่ใช้ฟังก์ชันที่เกี่ยวข้อง ข้อจำกัดเดียวของแพ็คเกจเหล่านี้คือเราไม่สามารถแก้ไขหรือเปลี่ยน กล่องข้อความ แม่แบบ ดังนั้น เพื่อใช้งาน Custom Popup MessageBox เราสามารถทำตามขั้นตอนเหล่านี้ได้
- สร้างปุ่มและเพิ่มคำสั่งเพื่อกำหนดฟังก์ชัน
- กำหนดฟังก์ชันเพื่อสร้างหน้าต่างระดับบนสุดและเพิ่มวิดเจ็ตอื่นๆ
- เพิ่มปุ่มและข้อความป้ายกำกับยืนยันในหน้าต่างระดับบนสุด
- เพิ่มคำสั่งปุ่มเพื่อแสดงข้อความในหน้าต่างหลักแบบโต้ตอบ
ตัวอย่าง
# Import required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the window size
win.geometry("700x250")
# 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")
def click_fun():
global pop
pop = Toplevel(win)
pop.title("Confirmation")
pop.geometry("700x250")
pop.config(bg="green3")
# Create a Label Text
label = Label(pop, text="Would You like to Proceed?", bg="green3", fg="white", font=('Aerial', 12))
label.pack(pady=20)
# Add a Frame
frame = Frame(pop, bg="green3")
frame.pack(pady=10)
# Add Button for making selection
button1 = Button(frame, text="Yes",
command=lambda: choice("yes"), bg="green")
button1.grid(row=0, column=1)
button2 = Button(frame, text="No",
command=lambda: choice("no"), bg="green")
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() ผลลัพธ์
การรันโค้ดด้านบนจะแสดงหน้าต่างพร้อมปุ่ม

เมื่อเราคลิกที่ปุ่ม มันจะแสดงกล่องข้อความป๊อปอัปที่กำหนดเอง
