简体   繁体   English

_tkinter.TclError:错误的 window 路径名“.!checkbutton”

[英]_tkinter.TclError: bad window path name “.!checkbutton”

I am trying to create an app that finds certain fields from a checkbox list.我正在尝试创建一个从复选框列表中查找某些字段的应用程序。 I am stuck at self.text.window_create("end", window=cb) while calling search function.我在调用search function 时卡在self.text.window_create("end", window=cb) Although it works fine when running the line in the __init__ it throws an error here.尽管在__init__中运行该行时它可以正常工作,但它会在此处引发错误。

import tkinter as tk

class App:

    def search(self):
        searched_field = self.search_box.get()
        found_indexes = []

        for i in range(len(self.name_list)):
            if searched_field in self.name_list[i]:
                found_indexes.append(i)

        self.text.delete("1.0", "end")

        for i in range(len(found_indexes)):
            cb = self.check_buttons[found_indexes[i]]
            self.text.window_create("end", window=cb)
            self.text.insert("end", "\n")

    def write_names(self):

        name_file = "names.txt"

        with open(name_file, "w") as file:

            for name in self.returned_list:
                file.write(name + "\n")

    def __init__(self, root, name_list):

        self.check_buttons = []
        self.var_list = []
        self.returned_list = []

        self.name_list = name_list
        self.start = 0

        self.search_box = tk.Entry(root)
        self.search_box.pack(side="top")
        self.search_button = tk.Button(root, text='Search', command=self.search)
        self.search_button.pack()

        self.scroll_bar = tk.Scrollbar(orient="vertical")
        self.text = tk.Text(root, width=20, height=10, yscrollcommand=self.scroll_bar.set)
        self.scroll_bar.config(command=self.text.yview)
        self.scroll_bar.pack(side="right", anchor="ne", fill="y")
        self.text.pack(side="top", fill="both", expand=True)

        for name in name_list:
            var = tk.BooleanVar()
            cb = tk.Checkbutton(text=name, padx=0, pady=0, bd=0, variable=var)
            self.check_buttons.append(cb)
            self.var_list.append(var)

        for cb in self.check_buttons:
            self.text.window_create("end", window=cb)
            self.text.insert("end", "\n")

        tk.Button(root, text='Write names', command=self.write_names()).pack()
        tk.Button(root, text='Quit', command=root.quit).pack()

name_list = ["aaa", "bbbb", "cccc", "abcd"]
root = tk.Tk()
app = App(root, name_list)
root.mainloop()

Error:错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\RO100162\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/RO100162/Desktop/proiect/eoIUPS/scrollbar.py", line 19, in search
    self.text.window_create("end", window=cb)
  File "C:\Users\RO100162\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3423, in window_create
    + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!checkbutton"

You need to pass in the tkinter root您需要传入 tkinter 根

cb = tk.Checkbutton(root, text=name, padx=0, pady=0, bd=0, variable=var)

Similar to your other buttons类似于您的其他按钮

self.search_button = tk.Button(root, text='Search', command=self.search)

When you delete the contents of the text widget, that causes the checkbuttons to be deleted as well.当您删除文本小部件的内容时,也会导致检查按钮被删除。

This is from the official tcl/tk documentation on the text widget [1] :这是来自文本小部件[1]上的官方 tcl/tk 文档:

The embedded window's position on the screen will be updated as the text is modified or scrolled, and it will be mapped and unmapped as it moves into and out of the visible area of the text widget.屏幕上嵌入窗口的 position 将随着文本的修改或滚动而更新,并且在移入和移出文本小部件的可见区域时将被映射和取消映射。 Each embedded window occupies one unit's worth of index space in the text widget, and it may be referred to either by the name of its embedded window or by its position in the widget's index space.每个嵌入的 window 在文本小部件中占用一个单位的索引空间,它可以通过其嵌入的 window 的名称或在小部件的索引空间中的 position 来引用。 If the range of text containing the embedded window is deleted then the window is destroyed.如果包含嵌入的 window 的文本范围被删除,则 window 被破坏。 Similarly if the text widget as a whole is deleted, then the window is destroyed.同样,如果整个文本小部件被删除,则 window 将被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM