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

widget.rowconfigure หรือ widget.grid_rowconfigure ใน Tkinter ใช้อันไหนถูกต้อง?


ขณะสร้างแอปพลิเคชันด้วย Tkinter เราสามารถใช้คอมโพเนนต์และ widget หลายอย่างเพื่อขยายแอปพลิเคชัน เพื่อแสดง widget เราใช้ Geometry Manager

Geometry manager กำหนดตำแหน่งและขนาดของ widget ภายในหน้าต่าง Grid Geometry manager วาง widget ในแถวและคอลัมน์

หากต้องการให้ widget ขยายไปอีกเซลล์หรือคอลัมน์ ใช้ widget.rowconfigure() หรือ widget.grid_rowconfigure() รับพารามิเตอร์ เช่น weight และค่า row/col

widget.rowconfigure() บางครั้งใช้แทน widget.grid_rowconfigure() การใช้เมธอดเหล่านี้จะทำให้ widget มีคุณสมบัติ weight ที่นำไปใช้กับแถวและคอลัมน์ได้

ตัวอย่าง

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")

# Add a new Frame
f1=Frame(win, background="bisque", width=10, height=100)
f2=Frame(win, background="blue", width=10, height=100)

# Add weight property to span the widget in remaining space
f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")
win.columnconfigure(0, weight=1)
win.rowconfigure(1, weight=0)
win.mainloop()

ผลลัพธ์

การรันโค้ดจะแสดงแถบสีในหน้าต่าง แถบสามารถให้ weight property เพื่อเพิ่มพื้นที่ในคอลัมน์ที่กำหนด

widget.rowconfigure หรือ widget.grid_rowconfigure ใน Tkinter ใช้อันไหนถูกต้อง?