在配置ttk时出现“未知选项”,“错误命令”。Panedwindow和窗格

时间:2018-12-07 18:54:16

标签: python tkinter ttk

我正在尝试ttk.Panedwindow,但使用有效选项会出错。某些选项有效(例如weight),但大多数无效。请帮助我了解发生了什么!我正在使用python 3.6.3,tk 8.6和ttk 0.3.1

工作摘要

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“未知选项”

如果我尝试更改Panedwindow的sashwidth:

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

我收到“未知选项”错误:

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"

子面板“未知选项”

如果我尝试在add()方法中配置子面板,则会发生相同的错误:

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

收益:

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"

子面板“错误命令”

由于在add()方法中不起作用,我尝试单独配置它:

paned_w.paneconfig(left_p, minsize=10)

这给了我一个“错误的命令”错误,尽管有智能才意识到:

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

即使我没有任何选择地运行它,也会发生相同的错误。

打印paned_w.paneconfig()的输出(不指定面板),正如我所期望的那样,会出现TypeError异常,因此该命令显然可用:

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

1 个答案:

答案 0 :(得分:0)

错误告诉您确切的问题是:您使用的是ttk PanedWindow小部件,并且该小部件不支持sashwidth选项,也不支持minsize选项{1}}方法。两者仅受tkinter paneconfigure小部件支持。

换句话说,您似乎依赖于tkinter PanedWindow小部件的文档,但实际上是在使用ttk PanedWindow小部件。这两个小部件的外观和风格相似,但是配置它们的方式却大不相同。