1

在我的 MVC 应用程序中,我想从 xml 文件加载数据,并在一个窗格窗口的两个不同视图中显示它。在左窗格中,我想在 tk.ttk.Treeview 中显示数据。在右窗格中,我想在 tk.tkk.Canvas 中显示数据。

主要应用:

from lscController import Controller

if __name__ == '__main__':
    c = Controller()
    c.run()

控制器:

try:
    import Tkinter as Tk # python 2
except ModuleNotFoundError:
    import tkinter as Tk # python 3

from lscView import LSCGUI

class Controller:
    def __init__(self):
        self.root = Tk.Tk()
        self.model = LSCDocument('name')
        self.view = LSCGUI(self.root, self.model)

    def run(self):
        self.root.title("MVC Charts")
        self.root.deiconify()
        self.root.mainloop(

文件lscTreeView.py

class LSCTree():
    def __init__(self, window,model):
        super().__init__()
        frame = tk.Frame(window)
        frame.pack()

我想将视图添加到文件lscView.py中 PanedWindow 的左窗格:

class LSCGUI():
    def __init__(self, window, model):
        super().__init__()
        self.window = window
        self.window.title("Model")
        self.model = model
        ''' Full Screen Or not '''
        self.window.attributes('-fullscreen',False)
        self.window.geometry('800x600')


        ''' ________Frame as paned windows (left / right)________ '''
        pwstyle = tk.ttk.Style()
        pwstyle.configure('TPanedwindow ', foreground='green')
        self.frame = tk.ttk.PanedWindow(style='TPanedwindow',orient=tk.VERTICAL)
        self.frame.pack(fill=tk.BOTH, expand=True)
        self.treeview=TREE.LSCTree(self.frame,model)
        self.frame.add(self.treeview)

并得到这个错误:

_tkinter.TclError: bad window path name "<lscTreeView.LSCTree object at 0x00000222D62B5910>"

VS Code 说错误在这里: self.frame.add(self.treeview)

4

1 回答 1

0

在这里,您将 self.treeview 传递给需要一个小部件作为参数的函数:

self.frame.add(self.treeview)

这是您定义 self.treeview 的地方:

self.treeview=TREE.LSCTree(self.frame,model)

这是 LSCTree 的定义:

class LSCTree():
    def __init__(self, window,model):
        ...

LSCTree显然不是小部件。您不能将非小部件添加到窗格窗口。

最好的解决方案是进行LSCTree继承,Frame以便可以将其视为小部件,而不是使用组合将框架作为LSCTree.

class LSCTree(tk.Frame):
    def __init__(self, window,model):
        super().__init__(window)
        ...
        self.tree = ttk.Treeview(self, ...)
        ...
于 2020-09-26T15:27:12.053 回答