簡體   English   中英

為什么冗余 window 出現在 TKinter 復選框中?

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

感謝@Bryan Oakley,因為我在這篇文章中使用了他的答案:( Tkinter 中復選框的可變大小列表? )。 一切正常,除了當我運行腳本時,出現了一個冗余的 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__":
    app = PageCanvas1(None)
    app.mainloop()

如果要在頂層 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