ในกรณีส่วนใหญ่ คุณต้องการหลายหน้าจอเพื่อให้ผู้ใช้สลับระหว่างส่วนต่างๆ ของโปรแกรม วิธีหนึ่งคือสร้าง Frame แยกกันที่อยู่ภายในหน้าต่างหลัก
Widget Frame ใช้จัดกลุ่ม widget หลายตัวในแอปพลิเคชัน เราสามารถเพิ่ม widget แยกต่างหากในสอง Frame ผู้ใช้สามารถสลับไปมาระหว่าง Frame ได้โดยกดปุ่ม
ตัวอย่าง
ในแอปพลิเคชันนี้ สร้างสอง Frame แยกกัน: greet frame และ order frame แต่ละ Frame ประกอบด้วยอ็อบเจกต์ที่ต่างกัน ปุ่มจะใช้สลับระหว่าง Frame ทั้งสอง
# Import the required libraries
from tkinter import *
from tkinter import font
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Create two frames in the window
greet = Frame(win)
order = Frame(win)
# Define a function for switching the frames
def change_to_greet():
greet.pack(fill='both', expand=1)
order.pack_forget()
def change_to_order():
order.pack(fill='both', expand=1)
greet.pack_forget()
# Create fonts for making difference in the frame
font1 = font.Font(family='Georgia', size='22', weight='bold')
font2 = font.Font(family='Aerial', size='12')
# Add a heading logo in the frames
label1 = Label(greet, text="Hey There! Welcome to TutorialsPoint.", foreground="green3", font=font1)
label1.pack(pady=20)
label2 = Label(order, text="Find all the interesting Tutorials.", foreground="blue", font=font2)
label2.pack(pady=20)
# Add a button to switch between two frames
btn1 = Button(win, text="Switch to Greet", font=font2, command=change_to_order)
btn1.pack(pady=20)
btn2 = Button(win, text="Switch to Order", font=font2, command=change_to_greet)
btn2.pack(pady=20)
win.mainloop()ผลลัพธ์
การรันโค้ดจะแสดงหน้าต่างที่มีสอง Frame ต่างกัน

Frame สามารถสลับจากหนึ่งไปอีกอันโดยใช้ปุ่มที่กำหนด

