【问题标题】:Why does a redundant window appear in a TKinter checkbox?为什么 TKinter 复选框中会出现冗余窗口?
【发布时间】:2022-09-23 06:11:51
【问题描述】:

感谢@Bryan Oakley,因为我在这篇文章中使用了他的答案:(Variable size list of Checkboxes in Tkinter?)。一切都很好,除了当我运行脚本时,出现了一个多余的窗口。我怎样才能摆脱它?

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()
  • 主窗口 root = tk.Tk() 在创建子窗口时自动创建。您需要隐藏它,或者将您的小部件放在上面,如示例中所做的那样。
  • 谢谢,我是 TKinter 的新手,我该如何隐藏它?还是放在里面?
  • 阅读关于 tkinter windows tkdocs.com/tutorial/windows.html

标签: python tkinter tkinter-canvas


【解决方案1】:

如果要在 Toplevel 窗口中创建 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()

【讨论】:

  • 非常好,伙计。我使用全局 arr 和 users 来获取我没有故意在代码中编写的每个检查按钮的检查状态。顺便谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
相关资源
最近更新 更多