简体   繁体   English

尝试使用Tkinter中的类来创建多个帧

[英]Trying to use classes in Tkinter to create multiple frames

I'm trying to create 2 frames in my main parent widget - one using the grid() method and the other the pack() method. 我正在尝试在我的主要父窗口小部件中创建2个框架 - 一个使用grid()方法,另一个使用pack()方法。 I wrote it first in a procedural way and it was way too messy, so I'm trying to use classes to clean up the code. 我首先以程序方式编写它并且它太乱了,所以我正在尝试使用类来清理代码。 Below is the relevant code, where I try to create 2 frames using the init method but it throws me all sorts of errors. 下面是相关代码,我尝试使用init方法创建2个帧,但它会抛出各种错误。

from tkinter import *

class Application(Frame):
    """This class creates packed frames for the GUI"""
    def __init__(self, master):
        Frame.__init__(self, master)
        self.framepack = Frame(master)
        self.framepack.pack(side=BOTTOM, fill=X)
        self.framegrid = Frame(master)
        self.framegrid.pack(side=TOP)
        self.create_widgets()

    def create_widgets(self):
        #Container 1 using LabelFrame, houses email and password labels and entries
        self.inputlabels1 = LabelFrame(self, self.framepack, text="Input email login information here")
        self.inputlabels1.grid(row=0, column=0, padx=10, pady=10)
        self.emailfield = Label(self.inputlabels1, text="Email Address")     #Labels
        self.passfield = Label(self.inputlabels1, text="Password")

root = Tk()
app = Application(root)
root.mainloop()

The entire thing just falls apart. 整个事情就崩溃了。 The problem code is this: 问题代码是这样的:

        self.inputlabels1 = LabelFrame(self, self.framepack, text="Input email login information here")

I'm trying to put this Labelframe widget into one of the two frames I initialized earlier, framepack but it throws me errors. 我正在尝试将这个Labelframe小部件放入我之前初始化的两个帧之一,framepack但它会引发我的错误。 Is there something wrong with my syntax? 我的语法有问题吗?

I amended your example, so that it works, and displays something. 我修改了你的例子,以便它可以工作,并显示一些东西。 At the current form, it does not work. 在目前的形式,它不起作用。 Also, I added colors to frames, so that they are easily seen. 另外,我在框架中添加了颜色,因此很容易看到它们。 This helps in organizing your layout, and understanding what is happening: 这有助于组织您的布局,并了解正在发生的事情:

from tkinter import *

class Application(Frame):
    """This class creates packed frames for the GUI"""
    def __init__(self, master):
        Frame.__init__(self, master)

        # added width=180, height=40, background='red'
        self.framepack = Frame(master, width=180, height=40, background='red')
        self.framepack.pack(side=BOTTOM, fill=X)

        # added width=180, height=40, background='green'
        self.framegrid = Frame(master, width=180, height=40, background='green')
        self.framegrid.pack(side=TOP)
        self.create_widgets()

    def create_widgets(self):
        # Container 1 using LabelFrame, houses email and password labels and entries

        # Removed `self,` from the orginal code.
        self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")
        self.inputlabels1.grid(row=0, column=0, padx=10, pady=10)        
        self.emailfield = Label(self.inputlabels1, text="Email Address")     #Labels

        # Called pack()
        self.emailfield.pack()
        self.passfield  = Label(self.inputlabels1, text="Password")

        # Called pack()
        self.passfield.pack()

root = Tk()
app = Application(root)
root.mainloop()

在此输入图像描述

Hope this helps. 希望这可以帮助。

You're trying to give your LabelFrame two parents. 你试图给你的LabelFrame两个父母。 The parent should be self.framepack . 父级应该是self.framepack Removing the first argument should fix the problem: 删除第一个参数应解决问题:

self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")

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

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