Biyau
Biyau

Reputation: 121

Python Tkinter: Paned Window not sticking to top

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()

Upvotes: 0

Views: 1403

Answers (1)

Josselin
Josselin

Reputation: 2643

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()

Upvotes: 2

Related Questions