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

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

问题描述

我正在处理一项编程任务.我正在使用 Python,并为我们的 GUI 使用 Tkinter.我不能更改语言或 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?

推荐答案

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.

这是一个简单的例子:

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", "
") # 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天全站免登陆