简体   繁体   English

为什么冗余 window 出现在 TKinter 复选框中?

[英]Why does a redundant window appear in a TKinter checkbox?

Thanks to @Bryan Oakley, as I use an answer of his in this post: ( Variable size list of Checkboxes in Tkinter? ).感谢@Bryan Oakley,因为我在这篇文章中使用了他的答案:( Tkinter 中复选框的可变大小列表? )。 Everything is OK, except that when I run the script, a redundant window appears.一切正常,除了当我运行脚本时,出现了一个冗余的 window。 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()

If you want to create Checkbutton widgets in the Toplevel window:如果要在顶层 window 中创建 Checkbutton 小部件:

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()

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

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