简体   繁体   English

如何使用 OOP 在 Tkinter 中制作多个帧?

[英]How to make multiple Frames in Tkinter using OOP?

So, I was learning about class and methods and was trying to make multiple frames by using init method inside a class.所以,我正在学习 class 和方法,并试图通过在 class 中使用init方法来制作多个帧。 The following is what I did:以下是我所做的:

    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()

My question is how can I set different Frame background, border and other Frame attributes to make each Frame have unique attributes.我的问题是如何设置不同的 Frame 背景、边框和其他 Frame 属性,以使每个 Frame 具有独特的属性。

The easiest way to specify frame arguments while using a class is by passing key word args through to the frame.在使用 class 时指定框架 arguments 的最简单方法是将关键字 args 传递给框架。 This can be done pretty easily by adding **kwargs to the end of the arguments in init.这可以通过在 init 中将**kwargs添加到 arguments 的末尾来轻松完成。 then you can pass through all the arguments as you normally would while declaring a frame.然后您可以像通常在声明框架时一样通过所有 arguments。

The code would look like:代码如下所示:

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()

Note: If you want to specify any default arguments to be applied to all the frames, add them before ,**kwargs in the declaration.注意:如果要指定任何默认 arguments 应用于所有框架,请在声明中的,**kwargs之前添加它们。 Ex: Frame(window, bg="white", **kwargs)例如: Frame(window, bg="white", **kwargs)

Edit: *args, **kwargs: *args is basically unpacking in a list format.编辑: *args, **kwargs: *args 基本上是以列表格式解包。 *args accepts as many values as possible. *args 接受尽可能多的值。 if you print args, it'll output a list.如果您打印 args,它将是 output 一个列表。

**kwargs is basically unpacking in a dictionary value. **kwargs 基本上是在字典值中解包。 **kwargs accepts key-value pairs. **kwargs 接受键值对。 if you print kwargs, it'll output a dictionary.如果您打印 kwargs,它将 output 字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM