Reputation: 618
I'm trying to create 2 frames in my main parent widget - one using the grid()
method and the other the pack()
method. 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.
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. Is there something wrong with my syntax?
Upvotes: 1
Views: 2605
Reputation: 238995
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.
Upvotes: 2
Reputation: 871
You're trying to give your LabelFrame
two parents. The parent should be self.framepack
. Removing the first argument should fix the problem:
self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")
Upvotes: 1