Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปลี่ยนตำแหน่ง MessageBox ด้วย Python Tkinter

สมมติว่าเราต้องการสร้าง Dialogue Box ด้วย Tkinter เพื่อสร้าง Dialogue Box เราสามารถใช้ไลบรารี MessageBox ซึ่งมีฟังก์ชันหลายตัวสำหรับสร้าง Dialogue Types อย่างรวดเร็ว

เพื่อปรับตำแหน่งของ Dialogue Box ที่สร้างขึ้น เราสามารถใช้ Property "toplevel" ซึ่งให้ Priority กับกล่องปัจจุบันและเก็บ Process อื่นๆ ไว้ใน Backend

มีฟังก์ชันอื่นๆ เช่น Title, Message, และ Details เพื่อเปลี่ยนตำแหน่งของ MessageBox Widget เราจะใช้เมธอด geometry

ตัวอย่าง

# import the tkinter library
from tkinter import *

# Define the messagebox function
def messagebox():
    # Toplevel function creates MessageBox dialog which appears on top of the screen
    top = Toplevel(win)
    top.title("Click Me")
    
    # Define the position of the MessageBox
    x_position = 600
    y_position = 400
    top.geometry(f"600x200+{x_position}+{y_position}")
    
    # Define the property of the messageBox
    l1 = Label(top, text="Hello! TutorialsPoint", bg="green", fg="white", font=('Times New Roman', 24), height=50, width=50).pack()

# Create an instance of the tkinter frame
# And resize the frame
win = Tk()
win.geometry("600x200")
win.title("Window-1")

Button(win, text="Click Me", command=messagebox, width=8).pack(pady=80)

win.mainloop()

ผลลัพธ์

การรันโค้ดข้างต้นจะสร้างหน้าต่างผลลัพธ์ดังนี้:

วิธีเปลี่ยนตำแหน่ง MessageBox ด้วย Python Tkinter

เมื่อคุณคลิกปุ่ม "Click Me" จะเปิด Dialogue Box ต่อไปนี้ซึ่งสามารถจัดตำแหน่งได้ภายหลัง

วิธีเปลี่ยนตำแหน่ง MessageBox ด้วย Python Tkinter