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

繼承 Frame Class ใน Tkinter Application (OOP)

ใน OOP สร้าง class สืบทอดจาก Frame เพื่อ reuse properties และ methods

ตัวอย่างโค้ด

from tkinter import *

win = Tk()
win.geometry("700x350")

class NewFrame(Frame):
    def __init__(self, win):
        super().__init__()
        self["height"] = 200
        self["width"] = 200
        self["bd"] = 10
        self["relief"] = RAISED
        self["bg"] = "#aa11bb"

frame_a = NewFrame(win)
frame_b = NewFrame(win)
frame_a.grid(row=0, column=0)
frame_b.grid(row=0, column=1)

win.mainloop()

ผลลัพธ์

สอง Frame มีคุณสมบัติเหมือนกัน (ขนาด, สี, border) จาก class เดียวกัน

繼承 Frame Class ใน Tkinter Application (OOP)