Search code examples
pythontkinterttk

"Unknown option", "Bad command" on configuring ttk.Panedwindow and panes


I'm experimenting with ttk.Panedwindow, but get errors with supposedly valid options. A few options work (e.g. weight), but most didn't. Please help me understand what's going on! I'm using python 3.6.3, tk 8.6, and ttk 0.3.1

Working Snippet

import tkinter as tk
from tkinter import ttk

# setup the panedwindow
root = tk.Tk()
paned_w = ttk.Panedwindow(root, orient=tk.HORIZONTAL, width=300, height=300)
paned_w.pack(fill='both', expand=True)

# setup the panels
left_p = ttk.Label(paned_w, background='red')
right_p = ttk.Label(paned_w, background='yellow')
paned_w.add(left_p, weight=1)
paned_w.add(right_p, weight=3)

root.mainloop()

Panedwindow "unknown option"

If I try to change the sashwidth of the Panedwindow:

paned_w = ttk.Panedwindow(root, orient=tk.HORIZONTAL, width=300, height=300, sashwidth=20)

I get an "unknown option" error:

Traceback (most recent call last):
  File "panedwindow.py", line 6, in <module>
paned_w = ttk.Panedwindow(root, orient=tk.HORIZONTAL, width=300, height=300, sashwidth=20)
  File "C:\Users\david\Anaconda3\lib\tkinter\ttk.py", line 956, in __init__
Widget.__init__(self, master, "ttk::panedwindow", kw)
  File "C:\Users\david\Anaconda3\lib\tkinter\ttk.py", line 559, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\Users\david\Anaconda3\lib\tkinter\__init__.py", line 2293, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-sashwidth"

Child panel "unknown option"

The same error occurs if I try to configure child panels in the add() method:

paned_w.add(left_p, weight=1, minsize=10)

Yields:

Traceback (most recent call last):
  File "panedwindow.py", line 12, in <module>
    paned_w.add(left_p, weight=1, minsize=10)
  File "C:\Users\david\Anaconda3\lib\tkinter\__init__.py", line 3817, in add
    self.tk.call((self._w, 'add', child) + self._options(kw))
_tkinter.TclError: unknown option "-minsize"

Child panel "bad command"

Since it didn't work in the add() method, I tried configuring it separately:

paned_w.paneconfig(left_p, minsize=10)

This gives me a "bad command" error, despite the intellisense recognizing it:

Traceback (most recent call last):
  File "panedwindow.py", line 15, in <module>
    paned_w.paneconfig(left_p, minsize=10)
  File "C:\Users\david\Anaconda3\lib\tkinter\__init__.py", line 3972, in paneconfigure
    self._options(cnf, kw))
_tkinter.TclError: bad command "paneconfigure": must be add, configure, cget, forget, identify, insert, instate, pane, panes, sashpos, or state

The same error occurs even if I run it without any options.

Printing the output of paned_w.paneconfig() (without specifying a panel) gives me a TypeError, as I would expect, so clearly the command is available:

TypeError: paneconfigure() missing 1 required positional argument: 'tagOrId'

Solution

  • The error is telling you exactly what is wrong: you are using the ttk PanedWindow widget, and that widget doesn't support sashwidth option, nor the minsize option for the paneconfigure method. Those are both only supported by the tkinter PanedWindow widget.

    In other words, you seem to be relying on the documentation for the tkinter PanedWindow widget, but are actually using the ttk PanedWindow widget. Those two widgets have a similar look and feel, but the way you configure them are quite different.