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

จะสลับระหว่างสองเฟรมใน Tkinter ได้อย่างไร?


ในกรณีส่วนใหญ่ คุณต้องมีหลายหน้าจอเพื่อให้ผู้ใช้สามารถสลับไปมาระหว่างส่วนต่างๆ ของโปรแกรมของคุณได้ วิธีหนึ่งในการบรรลุเป้าหมายนี้คือการสร้างเฟรมแยกจากกันซึ่งอยู่ภายในหน้าต่างหลัก

วิดเจ็ต A-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()

ผลลัพธ์

การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีเฟรมสองเฟรมที่แตกต่างกัน

จะสลับระหว่างสองเฟรมใน Tkinter ได้อย่างไร?

เฟรมต่างๆ สามารถเปลี่ยนจากเฟรมหนึ่งไปอีกเฟรมหนึ่งได้โดยใช้ปุ่มที่กำหนดไว้ในเฟรม

จะสลับระหว่างสองเฟรมใน Tkinter ได้อย่างไร?


จะสลับระหว่างสองเฟรมใน Tkinter ได้อย่างไร?