Tkinter 中复选框的可变大小列表?

发布于 2024-11-04 19:00:40 字数 327 浏览 1 评论 0原文

我正在执行一项编程任务。我正在使用 Python 工作,并使用 Tkinter 作为我们的 GUI。我无法更改语言或 GUI 工具,也无法使用任何其他软件包(例如 Tix)。

我需要列出要拉的项目清单。我首先想到的是复选框。然而,据我所知,Tkinter 没有任何支持大量(100+)复选框的东西。该数字不是恒定的,并且可能会随着程序的每次运行而不同。在他们自己的框架中,我还没有找到使框架可滚动的方法。我尝试了Listbox,但是没有好的方法可以在此范围内选择多个。

你们中有人知道有办法做到这一点吗?

I'm working on a programming task. I'm working in Python, and using Tkinter for our GUI. I cannot change language or GUI tool, nor can I use any additional packages (for example Tix).

I need to make a list of items to pull. The first thing I thought of was a check box. However, so far as I know, Tkinter does not have anything that supports a large number(100+) of check boxes. The number is not constant, and likely will be different with every run of the program. In their own frame, I have not found a way to make the frame scrollable. I tried Listbox, but there is no good way to select multiples on this scale.

Do any of you know of a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2024-11-11 19:00:40

Tkinter 支持相对无限数量的复选框,主要受到系统内存和可用性限制等实际问题的限制。

至少有三种技术可以用来制作小部件的可滚动容器。画布和文本小部件都支持滚动,因此普遍接受的做法是将其中之一用于容器。如果您需要复杂的东西,您还可以使用 place 命令来实现一些巧妙的技巧。

如果您想要滚动包含多个对象的垂直列表的框架,那么使用画布是很好的选择。如果您只需创建一个垂直列表,那么使用文本小部件会非常方便。

这是一个简单的示例:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, root, *args, **kwargs):
        tk.Frame.__init__(self, root, *args, **kwargs)
        self.root = root

        self.vsb = tk.Scrollbar(self, orient="vertical")
        self.text = tk.Text(self, width=40, height=20, 
                            yscrollcommand=self.vsb.set)
        self.vsb.config(command=self.text.yview)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        for i in range(1000):
            cb = tk.Checkbutton(self, text="checkbutton #%s" % i)
            self.text.window_create("end", window=cb)
            self.text.insert("end", "\n") # to force one checkbox per line

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

当您了解有关 Tkinter 的更多信息时,您会意识到它的内置小部件并不像其他一些工具包那么多。希望您还会意识到 Tkinter 拥有足够的基本构建块来完成您可以想象的任何事情。

Tkinter supports a relatively unlimited number of checkboxes, limited mostly by practical matters such as system memory and usability constraints.

There are at least three techniques for making a scrollable container for widgets. Both canvases and text widgets support scrolling, so the generally accepted practice is to use one of those for the container. You can also do clever tricks with the place command if you need something complex.

Using the canvas is good if you want to scroll a frame that contains more than just a vertical list of objects. Using the text widget is quite handy if all you need to do is create a single vertical list.

Here's a simple example:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, root, *args, **kwargs):
        tk.Frame.__init__(self, root, *args, **kwargs)
        self.root = root

        self.vsb = tk.Scrollbar(self, orient="vertical")
        self.text = tk.Text(self, width=40, height=20, 
                            yscrollcommand=self.vsb.set)
        self.vsb.config(command=self.text.yview)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        for i in range(1000):
            cb = tk.Checkbutton(self, text="checkbutton #%s" % i)
            self.text.window_create("end", window=cb)
            self.text.insert("end", "\n") # to force one checkbox per line

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

As you learn more about Tkinter you'll realize that there aren't quite as many built-in widgets as some other toolkits. Hopefully you'll also realize that Tkinter has enough fundamental building blocks to do just about anything you can imagine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文