1

我从网上获取了一个代码示例,它作为创建为 Tk() 的根窗口的子窗口工作。当我试图将它概括为 Toplevel() 的子级时,我收到一个错误,表明您不能将滚动条放入 Toplevel().Frame()。我不明白为什么自 Tk() 和 Toplevel() 以来的差异应该产生相似的结果。

确切的错误信息是:

_tkinter.TclError: can't put .19084144 inside .18651952.19084112 or... can't put scrollbar inside Toplevel().Frame()

from tkinter import *
from tkinter.ttk import *

class columnList(object):
    def __init__(self,parent,header,dataList):
        self.tree = None
        self._setup_widgets(parent,header)
        self._build_tree(header,dataList)

    def _setup_widgets(self,parent,header):
        treeFrame = Frame(parent)
        treeFrame.pack(fill='both', expand=True)
        # create a treeview with a vertical scrollbar
        self.tree = Treeview(treeFrame, columns=header, show="headings")
        vsb = Scrollbar(orient="vertical",command=self.tree.yview)
        self.tree.configure(yscrollcommand=vsb.set)
        self.tree.grid(column=0, row=0, sticky='nsew', in_=treeFrame)
        vsb.grid(column=1, row=0, sticky='ns', in_=treeFrame)
        treeFrame.grid_columnconfigure(0, weight=1)
        treeFrame.grid_rowconfigure(0, weight=1)

    def _build_tree(self,header,dataList):
        for col in header:
            self.tree.heading(col, text=col.title())
        for item in dataList:
            self.tree.insert('', 'end', values=item)
# the test data ...
oil_header = ['oil', 'filter', 'cost']
oil_list = [
('synthetic', 'paper', '63.38') ,
('natural', 'reusable', '47.59') ]
car_header = ['car', 'repair', 'cost', 'code']
car_list = [
('Hyundai', 'brakes', '273.80', '34') ,
('Honda', 'light', '85.70', '78')]

root = Tk()
top = Toplevel()

# this succeeds
invoiceMenu = columnList(root,car_header,car_list)
# this fails as
# _tkinter.TclError: can't put .19084144 inside .18651952.19084112
#   or... can't put scrollbar inside Toplevel().Frame()
oilChangeMenu = columnList(top,oil_header,oil_list)
root.mainloop()

我已经广泛使用 stackoverflow 来学习很多东西,并且非常感谢这里令人难以置信的丰富专业知识。坦率地说,我很尴尬,我无法弄清楚这一点。感谢您期待您能提供的任何帮助。

4

1 回答 1

1

您的问题中没有足够的细节可以确定,但问题可能源于您parent在创建滚动条时没有给出论点的事实。这意味着滚动条将始终在根窗口中创建。如果您随后尝试将其放在另一个顶层,您将收到与您报告的类似的错误。

于 2013-10-23T11:34:19.387 回答