Tkinter中的复选框的变量大小列表? [英] Variable size list of Checkboxes in Tkinter?

查看:354
本文介绍了Tkinter中的复选框的变量大小列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个编程任务。我在 Python 中工作,并使用 Tkinter 作为我们的GUI。我不能更改语言或GUI工具,也不能使用任何额外的包(例如 Tix )。

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

我需要创建一个项目列表。我想到的第一件事是一个复选框。但是,据我所知,Tkinter没有任何东西支持大量(100+)的复选框。数字不是常数,并且可能会随着程序的每次运行而不同。在自己的框架中,我没有找到一种方法使框架可滚动。我试过 Listbox ,但没有很好的方法来选择这个尺度上的多个。

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?

推荐答案

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

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

至少有三种技术用于制作可滚动容器为小部件。画布和文本小部件都支持滚动,所以一般接受的做法是使用其中的一个容器。如果你需要复杂的东西,你也可以使用place命令做聪明的技巧。

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

当你了解更多关于Tkinter的信息时,你会意识到没有其他工具包那么多内置的小部件。希望你也会意识到,Tkinter有足够的基本构建块来做任何你可以想象。

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.

这篇关于Tkinter中的复选框的变量大小列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆