如何在 tkinter 中将类初始化为一个帧。

huangapple go评论68阅读模式
英文:

How to initialize a class as a frame tkinter

问题

这是您提供的代码的翻译部分:

我之前问过这个问题但我已经缩小了问题的范围至少我认为是这样我有这个文件

main_page.py
```python
class main_page(Frame):
    def __init__(self, root, controller):
        Frame.__init__(self, root)
                                               
        canvas = Canvas(
            controller,
            bg="#153246",
            height=720,
            width=1280,
            bd=0,
            highlightthickness=0,
            relief="ridge"
        )

        #canvas.place(x=0, y=0)
        canvas.pack(fill="both", expand=True)

这是我GUI所需的许多帧之一。在一个名为gui.py的文件中,我有这个:

class gui(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
         
        container = Frame(self)

        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}
        self.curr_frame = None        
        
        #TODO: 这需要有figma帧
        for F in (main_page, account_no_waiver):
            frame = F(container, self)

            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        
        self.show_frame(main_page)

frame = F(container, self)这一行是用来构造帧的,以便我可以在控制器中使用它,但它没有创建一个帧。

我不太确定要尝试什么,因为我对tkinter没有太多经验。我期望将帧添加到我的帧列表中,然后我可以使用show_frame函数,该函数只是调用tkraise()来在需要时提升帧。


<details>
<summary>英文:</summary>

I asked this earlier but I&#39;ve narrowed down the issue, at least I think. I have this file 

main_page.py:

class main_page(Frame):
def init(self, root, controller):
Frame.init(self, root)

    canvas = Canvas(
        controller,
        bg = &quot;#153246&quot;,
        height = 720,
        width = 1280,
        bd = 0,
        highlightthickness = 0,
        relief = &quot;ridge&quot;
    )

    #canvas.place(x = 0, y = 0)
    canvas.pack(fill=&quot;both&quot;, expand=True)
which is one of the many frames I need for my gui. And in a file called gui.py I have this:

class gui(Tk):
def init(self, *args, **kwargs):
Tk.init(self, *args, **kwargs)

    container = Frame(self)

    container.pack(side=&quot;top&quot;, fill=&quot;both&quot;, expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)
    
    self.frames = {}
    self.curr_frame = None        
    
    #TODO: This needs to have the figma frames
    for F in (main_page, account_no_waiver):
        frame = F(container, self)

        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky=&quot;nsew&quot;)
    
    self.show_frame(main_page)

the line `frame = F(container, self)` is meant to construct the frame so I can use it with the controller, but it is not making a frame.

I&#39;m not exactly sure what to try as I don&#39;t have much experience with tkinter. I expect the frame to be added to my list of frames and I can then use a show_frame function, which just calls tkraise(), to raise the frame when I need it.

</details>


# 答案1
**得分**: 0

The code posted is not executable. However, the primary issue I see is that you're using `controller` as the parent for the canvas instead of `self`. You should use `self` as the canvas's parent since `self` represents the instance of `main_page` that should contain the canvas.

Here's the modified code:

```python
class MainPage(Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)

        canvas = Canvas(
            self,
            bg="#153246",
            height=720,
            width=1280,
            bd=0,
            highlightthickness=0,
            relief="ridge"
        )

        canvas.pack(fill="both", expand=True)

Additionally, consider renaming the root parameter to parent for clarity and following PEP8 naming conventions by capitalizing the class name and using camel case. You might also consider using super when initializing the superclass.

英文:

The code isn't runnable as posted. However, the only real problem I see is that you're using controller for the parent of the canvas, rather than self. You should be using self, since self (the instance of main_page) is the frame that should contain the canvas.

You should rename the root parmeter to parent for clarity, and place the canvas in self rather than controller.

Also, you should use PEP8 naming conventions and capitalize the name of the class, and use camelcase. And finally, it's arguably better to use super when initializing the superclass.

class MainPage(Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)

        canvas = Canvas(
            self,
            bg = &quot;#153246&quot;,
            height = 720,
            width = 1280,
            bd = 0,
            highlightthickness = 0,
            relief = &quot;ridge&quot;
        )

        canvas.pack(fill=&quot;both&quot;, expand=True)

huangapple
  • 本文由 发表于 2023年6月29日 06:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定