Home > Back-end >  Why does a redundant window appear in a TKinter checkbox?
Why does a redundant window appear in a TKinter checkbox?

Time:09-23

Thanks to @Bryan Oakley, as I use an answer of his in this post: (Variable size list of Checkboxes in Tkinter?). Everything is OK, except that when I run the script, a redundant window appears. How can I get rid of it?

import Tkinter as tk

class PageCanvas1(tk.Toplevel):
    def __init__(self, parent):
        global arr # why use global? set it as an attribute?
        global users # same as above?
        arr = {}
        tk.Toplevel.__init__(self, parent)
        self.title('Canvas')
        self.geometry('400x600')
        canvas = tk.Canvas(self, bg='white', scrollregion=(0, 0, 400, 20000))
        canvas.pack(fill='both', expand=True)

        vbar = tk.Scrollbar(canvas, orient='vertical')
        vbar.pack(side='right', fill='y')
        vbar.config(command=canvas.yview)
        canvas.config(yscrollcommand=vbar.set)
        canvas.create_text(5, 0, anchor='nw', text="Choose users: ")
        # we need a container widget to put into the canvas
        f = tk.Frame(canvas)
        # you need to create a window into the canvas for the widget to scroll
        canvas.create_window((200, 0), window=f, anchor="n")
        for i in range(0, 1000):
            arr[i] = tk.IntVar()
            # widget must be packed into the container, not the canvas
            tk.Checkbutton(f, text=str(i), variable=arr[i]).pack()#.grid(row=i, sticky=W)

if __name__ == "__main__":
    app = PageCanvas1(None)
    app.mainloop()

CodePudding user response:

If you want to create Checkbutton widgets in the Toplevel window:

import tkinter as tk


class PageCanvas1(tk.Toplevel):
    def __init__(self, parent):
        global arr  # why use global? set it as an attribute?
        global users  # same as above?
        arr = {}
        tk.Toplevel.__init__(self, parent)
        self.title('Canvas')
        self.geometry('400x600')
        canvas = tk.Canvas(self, bg='white', scrollregion=(0, 0, 400, 20000))
        canvas.pack(fill='both', expand=True)

        vbar = tk.Scrollbar(canvas, orient='vertical')
        vbar.pack(side='right', fill='y')
        vbar.config(command=canvas.yview)
        canvas.config(yscrollcommand=vbar.set)
        canvas.create_text(5, 0, anchor='nw', text="Choose users: ")
        # we need a container widget to put into the canvas
        f = tk.Frame(canvas)
        # you need to create a window into the canvas for the widget to scroll
        canvas.create_window((200, 0), window=f, anchor="n")
        for i in range(0, 1000):
            arr[i] = tk.IntVar()
            # widget must be packed into the container, not the canvas
            tk.Checkbutton(f, text=str(i), variable=arr[i]).pack()  # .grid(row=i, sticky=W)


if __name__ == "__main__":
    root = tk.Tk()  # Create the main window explicitly.
    root.withdraw()  # Hiding it is easy, but now when the child window is closed,
    # the application will continue to "hang".
    app = PageCanvas1(None)
    # we intercept the event - the closing of the child window, and close the main one.
    app.protocol("WM_DELETE_WINDOW", root.destroy)
    root.mainloop()
  • Related