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

การตั้งค่าตำแหน่งบนปุ่มใน Tkinter Python?


มีบางวิธีที่วิดเจ็ต Tkinter สามารถวางตำแหน่งภายในหน้าต่างได้ ผู้จัดการ Tkinter Geometry มีสามวิธี pack(), place() และ grid() ซึ่งเราสามารถกำหนดตำแหน่งของวิดเจ็ตในหน้าต่างแอปพลิเคชันได้ แต่ละวิธีมีข้อจำกัดและการใช้งานของตัวเอง ในการกำหนดตำแหน่งของปุ่มบนหน้าต่างแอปพลิเคชัน Tkinter เราสามารถเลือกใช้ place(x-coordinates, y-coordinates) วิธีเหนือวิธีอื่นๆ ทั้งหมด ใช้ค่าของพิกัด x และ y ซึ่งจำเป็นสำหรับการกำหนดตำแหน่งของวิดเจ็ต

ตัวอย่าง

โค้ดตัวอย่างมีวิดเจ็ตปุ่มที่ใช้ place(x, y) วิธีการจัดตำแหน่งปุ่มในหน้าต่าง

# Import the Tkinter library
from tkinter import *
from tkinter import ttk
# Create an instance of Tkinter frame
win = Tk()
# Define the geometry
win.geometry("750x250")
def close_win():
   win.destroy()
# Create Buttons in the frame
button = ttk.Button(win, text="Click", command=close_win)
button.place(x=325, y=125)
#Create a Label
Label(win, text="Click the Button to Close the Window", font=('Consolas 15')).pack()
win.mainloop()

ผลลัพธ์

การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีปุ่มอยู่ที่ตำแหน่งกึ่งกลางของหน้าต่าง

การตั้งค่าตำแหน่งบนปุ่มใน Tkinter Python?