我从网上获取了一个代码示例,它作为创建为Tk()的根窗口的子代。当我试图将它作为Toplevel()的孩子的目的推广时,我得到一个错误,表明你不能将滚动条放入Toplevel()。Frame()。我无法弄清楚为什么Tk()和Toplevel()之间的差异会产生类似的结果。
确切的错误消息是:
_tkinter.TclError:无法将.19084144放入.18651952.19084112 或者...不能将滚动条放在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来学习很多东西,并非常欣赏这里丰富的专业知识。坦率地说,我很尴尬,我无法想象这一个。感谢您提供的任何帮助。
答案 0 :(得分:1)
您的问题中没有足够的细节可以肯定地知道,但问题可能源于您在创建滚动条时没有给出parent
参数的事实。这意味着将始终在根窗口中创建滚动条。如果您然后尝试将其放在另一个顶层中,您将收到类似于您报告的错误。