0

Thanks for taking time to look at this. I've been struggling with this for almost a week and its driving me crazy.

I have a horizontal Paned Window which is supposed to stretch from the bottom of my toolbar to the bottom of my window, but it's sticking only to the bottom of the root window. Eventually I want to have a Treeview widget in the left pane and thumbnails in the right pane.

Can anyone help me to get the Paned Window to stick NSEW? Do I need to put it inside another frame?

I'm using Python 2.7 on Windows 7. (This isn't my whole program, just a sample to demonstrate the problem.)

#!/usr/bin/env python
# coding=utf-8

from Tkinter import *
from ttk import *

class MainWindow:

    def null(self):
        pass

    def __init__(self):
        self.root = Tk()
        self.root.geometry("700x300")
        self.root.resizable(width=TRUE, height=TRUE)
        self.root.rowconfigure(0, weight=1)
        self.root.columnconfigure(0, weight=1)

        self.menubar = Menu(self.root)
        File_menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Pandoras Box", menu=File_menu)
        File_menu.add_command(label="Black Hole", command=self.null)
        self.root.config(menu=self.menubar)

        self.toolbar = Frame(self.root, relief=RAISED)
        self.toolbar.grid(row=0, column=0, sticky='NEW')
        self.toolbar.grid_columnconfigure(0, weight=1)
        self.toolbar.rowconfigure(0, weight=1)

        dummy = Button(self.toolbar, text="Tool Button")
        dummy.grid(row=0, column=0, sticky='EW')
        Find = Label(self.toolbar, text="Search")
        Search = Entry(self.toolbar)
        Find.grid(row=0, column=5, sticky='E', padx=6)
        Search.grid(row=0, column=6, sticky='E', padx=8)

        self.info_column = Frame(self.root, relief=RAISED, width=100)
        self.info_column.grid(row=0, column=5, rowspan=3, sticky='NSW')
        self.info_column.grid_rowconfigure(0, weight=1)
        self.info_column.grid_columnconfigure(0, weight=1)

        self.rootpane = PanedWindow(self.root, orient=HORIZONTAL)
        self.rootpane.grid(row=1, column=0, sticky='NS')
        self.rootpane.grid_rowconfigure(0, weight=1)
        self.rootpane.grid_columnconfigure(0, weight=1)

        self.leftpane = Frame(self.rootpane, relief=RAISED)
        self.leftpane.grid(row=0, column=0, sticky='NSEW')

        self.rightpane = Frame(self.rootpane, relief=RAISED)
        self.rightpane.grid(row=0, column=0, sticky='NSEW')

        ''' THESE BUTTONS ARE SUPPOSED TO BE INSIDE PANED WINDOW STUCK TO THE TOP!'''
        but_left = Button(self.leftpane, text="SHOULD BE IN LEFT PANE UNDER TOOLBAR FRAME")
        but_left.grid(row=0, column=0, sticky='NEW')

        but_right = Button(self.rightpane, text="SHOULD BE IN RIGHT PANE UNDER TOOLBAR FRAME")
        but_right.grid(row=0, column=0, sticky='NEW')

        self.rootpane.add(self.leftpane)
        self.rootpane.add(self.rightpane)

        self.SbarMesg = StringVar()
        self.label = Label(self.root, textvariable=self.SbarMesg, font=('arial', 8, 'normal'))
        self.SbarMesg.set('Status Bar:')
        self.label.grid(row=3, column=0, columnspan=6, sticky='SEW')
        self.label.grid_rowconfigure(0, weight=1)
        self.label.grid_columnconfigure(0, weight=1)

        self.root.mainloop()


a = MainWindow()
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Biyau
  • 121
  • 1
  • 12

1 Answers1

2

Short answer: the space you see between the buttons and the toolbar frame is because you allow the row containing the toolbar to resize, instead of the row containing the PanedWindow... To get what you want, replace:

self.root.rowconfigure(0, weight=1)

with

self.root.rowconfigure(1, weight=1)

Other comments:

  • Try to avoid wildcard imports. In this case, it makes it difficult to differentiate between tk and ttk widgets
  • To allow resizing of widgets aligned using grid(), .rowconfigure(..., weight=x) must be called on the widget's parent not the widget itself.
  • background colors are very useful to debug alignment issues in tkinter.

Code:

import Tkinter as tk
import ttk

class MainWindow:

    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("700x300")
        self.root.resizable(width=tk.TRUE, height=tk.TRUE)
        self.root.rowconfigure(1, weight=1)
        self.root.columnconfigure(0, weight=1)

        self.toolbar = tk.Frame(self.root, relief=tk.RAISED, bg="yellow")
        self.toolbar.grid(row=0, column=0, sticky='NEW')
        self.toolbar.columnconfigure(0, weight=1)

        dummy = ttk.Button(self.toolbar, text="Tool Button")
        dummy.grid(row=0, column=0, sticky='EW')
        Find = tk.Label(self.toolbar, text="Search")
        Search = ttk.Entry(self.toolbar)
        Find.grid(row=0, column=5, sticky='E', padx=6)
        Search.grid(row=0, column=6, sticky='E', padx=8)

        self.info_column = tk.Frame(self.root, relief=tk.RAISED, width=100, bg="orange")
        self.info_column.grid(row=0, column=5, rowspan=2, sticky='NSW')

        self.rootpane = tk.PanedWindow(self.root, orient=tk.HORIZONTAL, bg="blue")
        self.rootpane.grid(row=1, column=0, sticky='NSEW')

        self.leftpane = tk.Frame(self.rootpane, bg="pink")
        self.rootpane.add(self.leftpane)

        self.rightpane = tk.Frame(self.rootpane, bg="red")
        self.rootpane.add(self.rightpane)

        ''' THESE BUTTONS ARE SUPPOSED TO BE INSIDE PANED WINDOW STUCK TO THE TOP!'''
        but_left = ttk.Button(self.leftpane, text="SHOULD BE IN LEFT PANE UNDER TOOLBAR FRAME")
        but_left.grid(row=0, column=0, sticky='NEW')

        but_right = ttk.Button(self.rightpane, text="SHOULD BE IN RIGHT PANE UNDER TOOLBAR FRAME")
        but_right.grid(row=0, column=0, sticky='NEW')

        self.label = tk.Label(self.root, text="Status:",  anchor="w")
        self.label.grid(row=3, column=0, columnspan=6, sticky='SEW')

        self.root.mainloop()

a = MainWindow()
Josselin
  • 2,593
  • 2
  • 22
  • 35
  • 1
    thank you so much for taking the time to help me, I really appreciate that. I noticed that when I commented out the rowconfigure for the root window things would go to the top, but then the other direction was a problem. So I put rowconfigure everywhere to see if it worked. Also your tip about the background color is a great idea. Thank You – Biyau Jul 24 '17 at 21:22
  • just a quick follow up question. What does that "1" you changed in the rowconfigure mean? What does increasing it do? (Besides fix my problem). – Biyau Jul 24 '17 at 21:37
  • You're welcome. The "1" is the [index of the row](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html) that you want to make stretchable (the 2nd row). Previously, using "0" was making the 1st row stretchable. – Josselin Jul 24 '17 at 23:38