David Collins
David Collins

Reputation: 900

Python tkinter frame parent widget line up columns

I'm placing a scrollbar in a frame, and the frame in a widget. The frame has a label above it. The label above has three columns. The frame with the scrollbar has three columns. I can't get the three columns in the frame and above the frame to line up.

I saw and used the following two questions to get this far, but got stuck.
Adding a scrollbar to a group of widgets in Tkinter

Tkinter - Add scrollbar for each LabelFrame

Any help on lining up the columns would be greatly appreciated. Thanks.

Here's a picture of the widget without the lined up columns: Columns don't line up

Here's a MWE:

import tkinter as tk

class Selections(tk.Frame): 
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

    self.FifthLabelLeft = tk.Label(self,
                     text="""Riding""",
                     justify = tk.CENTER,
                     padx = 10).grid(row=4, column = 0, pady=5)

    self.FifthLabelCenter = tk.Label(self,
                     text="""Winning Candidate""",
                     justify = tk.CENTER,
                     padx = 10).grid(row=4, column = 1, pady=5)

    self.FifthLabelRight = tk.Label(self,
                     text="""Percent of Vote""",
                     justify = tk.CENTER,
                     padx = 10).grid(row=4, column = 2, pady=5)

    mybox = tk.LabelFrame(self, padx=5, pady=4)
    mybox.grid(row=5, column=0, columnspan=3)

    canvas = tk.Canvas(mybox, borderwidth=5, background="#70ff33")
    frame = tk.Frame(canvas, background="#33f4ff")
    vsb = tk.Scrollbar(mybox, orient="vertical", command=canvas.yview)
    canvas.configure(yscrollcommand=vsb.set, width=450, heigh=50)       

    vsb.pack(side="right", fill="y")
    canvas.pack(side="left", fill="both", expand=True)
    canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")

    # be sure that we call OnFrameConfigure on the right canvas
    frame.bind("<Configure>", lambda event: self.OnFrameConfigure(canvas))

    self.fillWindow(frame)

    self.QuitButton = tk.Button(self, 
                                text="QUIT", 
                                command=root.destroy, 
                                padx=25, pady=0)
    self.QuitButton.grid(column = 0, columnspan=3)


def fillWindow(self, frame): 
    PartyWinnersList = [['Some list of places', "Somebody's Name", 0.37448599960838064], 
                        ['A shorter list', 'Three Long Names Here', 0.52167817821240514],
                        ['A much longer, longer entry', 'Short Name', 0.41945832387008858]]
    placement = 2
    for i in PartyWinnersList:
        ShowYear = tk.Label(frame,
                                  text="""%s """ % i[0]
                                  )
        ShowYear.grid(row=placement, column = 0, sticky=tk.S)
        ShowSystem = tk.Label(frame,
                                  text="""%s """ % i[1]
                                  )
        ShowSystem.grid(row=placement, column = 1, sticky=tk.N)
        PercentVotes = i[2]*100
        ShowVotes = tk.Label(frame,
                                  text="""%3.1f""" % PercentVotes
                                  )
        ShowVotes.grid(row=placement, column = 2, sticky=tk.N)
        placement += 1

def OnFrameConfigure(self, canvas):
    canvas.configure(scrollregion=canvas.bbox("all"))

if __name__ == "__main__": 
    root = tk.Tk()
    main = Selections(root)
    main.pack(side="top", fill="both", expand=True)
    root.mainloop()  

The two defined functions are in the class, but I couldn't get them to line up.

Upvotes: 0

Views: 461

Answers (1)

Ron Norris
Ron Norris

Reputation: 2690

I usually don't solve the problem this way, but you can force alignment by specifying label widths. The problem will come when you dynamically resize the window -- everything is static. But this will solve the specified problem -- alignment.

import tkinter as tk

class Selections(tk.Frame): 
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        self.FifthLabelLeft = tk.Label(self,
                     text="""Riding""",
                     justify = tk.CENTER,
                     width=25,padx = 10)
        self.FifthLabelLeft.grid(row=4, column = 0, pady=5)

        self.FifthLabelCenter = tk.Label(self,
                     text="""Winning Candidate""",
                     justify = tk.CENTER,
                     width=25,
                     padx = 10).grid(row=4, column = 1, pady=5)

        self.FifthLabelRight = tk.Label(self,
                     text="""Percent of Vote""",
                     justify = tk.CENTER,
                     padx = 10).grid(row=4, column = 2, pady=5)

        mybox = tk.LabelFrame(self, padx=5, pady=4)
        mybox.grid(row=5, column=0, columnspan=3)

        canvas = tk.Canvas(mybox, borderwidth=5, background="#70ff33")
        frame = tk.Frame(canvas, background="#33f4ff")
#        frame.grid_columnconfigure((0,1,2), weight=3)
        vsb = tk.Scrollbar(mybox, orient="vertical", command=canvas.yview)
        canvas.configure(yscrollcommand=vsb.set, width=450, heigh=50)       

        vsb.pack(side="right", fill="y")
        canvas.pack(side="left", fill="both", expand=True)
        canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")

        # be sure that we call OnFrameConfigure on the right canvas
        frame.bind("<Configure>", lambda event: self.OnFrameConfigure(canvas))

        self.fillWindow(frame)

        self.QuitButton = tk.Button(self, 
                                    text="QUIT", 
                                    command=root.destroy, 
                                    padx=25, pady=0)
        self.QuitButton.grid(column = 0, columnspan=3)


    def fillWindow(self, frame): 
        PartyWinnersList = [['Some list of places', "Somebody's Name", 0.37448599960838064], 
                            ['A shorter list', 'Three Long Names Here', 0.52167817821240514],
                            ['A much longer, longer entry', 'Short Name', 0.41945832387008858]]
        placement = 2
        for i in PartyWinnersList:
            ShowYear = tk.Label(frame, text="""%s """ % i[0], width=25)
            ShowYear.grid(row=placement, column = 0, sticky=tk.S)
            ShowSystem = tk.Label(frame,
                                      text="""%s """ % i[1],
                                      width=25
                                      )
            ShowSystem.grid(row=placement, column = 1, sticky=tk.N)
            PercentVotes = i[2]*100
            ShowVotes = tk.Label(frame,
                                      text="""%3.1f""" % PercentVotes,
                                      width=15
                                      )
            ShowVotes.grid(row=placement, column = 2, sticky=tk.N)
            placement += 1

    def OnFrameConfigure(self, canvas):
        canvas.configure(scrollregion=canvas.bbox("all"))

if __name__ == "__main__": 
    root = tk.Tk()
    main = Selections(root)
    main.pack(side="top", fill="both", expand=True)
    root.mainloop()

Upvotes: 1

Related Questions