terratunaz
terratunaz

Reputation: 664

Creating GUI using Tkinter

I originally had my GUI set up using the .pack method but realized that there were going to be some instances where I needed to use .grid and using the two together in the way I was doing was causing my app to freeze so I decided to go about designing my GUI in a different way. I decided to divide all major components of my GUI into frames which I packed in (TOP, LEFT, RIGHT, BOTTOM). For my labels, and buttons, I'm trying to place them within these four frame using the .grid method. It's sort of working. Though I'm not sure if I'm going about it the right way. For instance, my status bar is at the bottom of the screen but it won't stretch the entire length of the screen like it had before (when I was using pack for everything). I've tried changing the columnspan and using sticky but nothing seems to work. I'm new at Python and at using Tkinter. What am I missing? Any help or guidance would be most appreciated. Here are bits of my code so you can see how I'm setting everything up/calling for it.

    # set up frame for editing fields
    self.editFrame = Frame(self.mainGui, width=325, bd=1, relief=RIDGE,     bg="#BFC0C2")
    self.albumFrame = Frame(self.editFrame, width=315, height=265, bd=1,  relief=RIDGE, bg="#5F817C")
    self.toolFrame = Frame(self.mainGui)
    self.statusFrame = Frame(self.mainGui)
    self.mainFrame = Frame(self.mainGui)

    # setup toolbar, status bar and important buttons
    self.toolbar = Frame(self.toolFrame)
    self.importTracks = Button(self.toolbar, text="Import Tracks", command=self.importTracks)
    self.autoFill = Button(self.toolbar, text="Auto-Fill Fields", command=self.autoFillFields)
    self.exportStationBuilder = Button(self.toolbar, text="Export - StationBuilder", command=self.openStationBuilder)
    self.status = Label(self.statusFrame, text="",    bd=1, relief=SUNKEN, anchor=W, bg="#BFC0C2", padx=5)

    # place frame for editing fields
    self.editFrame.pack(side=RIGHT, fill=BOTH)
    self.editFrame.update()
    self.toolFrame.pack(side=TOP, fill=X, padx=2, pady=2)
    self.statusFrame.pack(side=BOTTOM, anchor=S, fill=X, expand=YES)
    self.mainFrame.pack(side=LEFT, expand=YES)

    # place toolbar, status bar and important buttons
    self.importTracks.grid(row=0, column=0)
    self.autoFill.grid(row=0, column=1)
    self.exportStationBuilder.grid(row=0, column=2)
    self.toolbar.pack(side=TOP, fill=X)
    self.status.grid(row=0, columnspan=1000, sticky=S)

Upvotes: 1

Views: 230

Answers (1)

Jozef Méry
Jozef Méry

Reputation: 357

The columnspan = 1000 is not needed. The columnspan defines the width relative to other widgets. Let's say you have two widgets one in column 0, the other in column 1. If you put the next widget to a row below a set columnspan = 2, this widget will the combined width of the 2 above. To make it fill the entire column you need to use

sticky = W+E or "we" or WE 

depending on python version, and how you imported tk. This is basically, stretching East and West aka. horizontally. Other than that you might need to use columnconfigure on the parent widget, most likely frame like this

framename.columnconfigure(0, weight = 1)

This tell the widget to use the extra space it gets.

PS. I don't like the grid too much and when I want widgets to fill the frame I just make one for them separately and use pack. Simple and no columnconfigure etc.

Upvotes: 1

Related Questions