如何使用 OOP 在 Tkinter 中制作多个框架?
How to make multiple Frames in Tkinter using OOP?
所以,我正在学习 class 和方法,并试图通过在 class 中使用 init 方法来制作多个帧。以下是我所做的:
from tkinter import *
import random
from PIL import ImageTk, Image
win = Tk()
win.attributes('-fullscreen', True)
# Define Frame Class
class MyFrame:
def __init__(self, master):
frame = Frame(master, width = win.winfo_screenwidth(),
height = win.winfo_screenheight(),
bg='black')
frame.pack()
def FrameOne():
frameone = MyFrame(win)
def FrameTwo():
frametwo = MyFrame(win)
#Call Frame (This is where I want the following frames to have different unique attributes)
FrameOne()
FrameTwo()
win.mainloop()
我的问题是如何设置不同的Frame背景、边框和其他Frame属性,使每个Frame具有独特的属性。
使用 class 指定框架参数的最简单方法是将关键字参数传递给框架。这可以很容易地通过在 init 中的参数末尾添加 **kwargs
来完成。然后你可以在声明一个框架时像往常一样传递所有参数。
代码如下所示:
from tkinter import *
import random
from PIL import ImageTk, Image
win = Tk()
win.attributes('-fullscreen', True)
# Define Frame Class
class MyFrame:
def __init__(self, master, **kwargs):
frame = Frame(master, **kwargs)
frame.pack()
def FrameOne():
frameone = MyFrame(win, width = win.winfo_screenwidth(),
height = win.winfo_screenheight()//2,
bg='black')
def FrameTwo():
frametwo = MyFrame(win, width=win.winfo_screenwidth(),
height = win.winfo_screenheight()//2,
bg='blue')
#Call Frame (This is where I want the following frames to have different unique attributes)
FrameOne()
FrameTwo()
win.mainloop()
注意:如果您想指定任何默认参数应用于所有框架,请在声明中的 ,**kwargs
之前添加它们。
例如:Frame(window, bg="white", **kwargs)
编辑:*args,**kwargs:
*args 基本上是以列表格式解包。 *args 接受尽可能多的值。如果你打印 args,它会输出一个列表。
**kwargs 基本上是在字典值中解包。 **kwargs 接受键值对。如果你打印 kwargs,它会输出一个字典。
所以,我正在学习 class 和方法,并试图通过在 class 中使用 init 方法来制作多个帧。以下是我所做的:
from tkinter import *
import random
from PIL import ImageTk, Image
win = Tk()
win.attributes('-fullscreen', True)
# Define Frame Class
class MyFrame:
def __init__(self, master):
frame = Frame(master, width = win.winfo_screenwidth(),
height = win.winfo_screenheight(),
bg='black')
frame.pack()
def FrameOne():
frameone = MyFrame(win)
def FrameTwo():
frametwo = MyFrame(win)
#Call Frame (This is where I want the following frames to have different unique attributes)
FrameOne()
FrameTwo()
win.mainloop()
我的问题是如何设置不同的Frame背景、边框和其他Frame属性,使每个Frame具有独特的属性。
使用 class 指定框架参数的最简单方法是将关键字参数传递给框架。这可以很容易地通过在 init 中的参数末尾添加 **kwargs
来完成。然后你可以在声明一个框架时像往常一样传递所有参数。
代码如下所示:
from tkinter import *
import random
from PIL import ImageTk, Image
win = Tk()
win.attributes('-fullscreen', True)
# Define Frame Class
class MyFrame:
def __init__(self, master, **kwargs):
frame = Frame(master, **kwargs)
frame.pack()
def FrameOne():
frameone = MyFrame(win, width = win.winfo_screenwidth(),
height = win.winfo_screenheight()//2,
bg='black')
def FrameTwo():
frametwo = MyFrame(win, width=win.winfo_screenwidth(),
height = win.winfo_screenheight()//2,
bg='blue')
#Call Frame (This is where I want the following frames to have different unique attributes)
FrameOne()
FrameTwo()
win.mainloop()
注意:如果您想指定任何默认参数应用于所有框架,请在声明中的 ,**kwargs
之前添加它们。
例如:Frame(window, bg="white", **kwargs)
编辑:*args,**kwargs: *args 基本上是以列表格式解包。 *args 接受尽可能多的值。如果你打印 args,它会输出一个列表。
**kwargs 基本上是在字典值中解包。 **kwargs 接受键值对。如果你打印 kwargs,它会输出一个字典。