在 Tkinter 应用程序中是否继承 Frame
在面向对象编程范式中,继承用于获取基类的属性并将它们用于派生类。对于 Tkinter 应用程序,我们可以将基类中定义的 Frame 的所有属性(例如背景颜色、前景色、字体属性等)继承到派生类或 Frame 中。
为了支持继承,我们必须定义一个包含 Frame 的一些基本属性的类,例如高度、宽度、背景、前景色、字体等。
示例
# Import Tkinter Library
from tkinter import *
# Create an instance of Tkinter frame
win= Tk()
# Set the size of the application window
win.geometry("700x350")
# Create a class to define the frame
class NewFrame(Frame):
def __init__(self, win):
super().__init__()
self["height"] = 200
self["width"] = 200
self["bd"] = 10
self["relief"] = RAISED
self["bg"] = "#aa11bb"
# Create Frame object
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,具有其在类中定义的 Frame 的相同属性。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP