Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

python - Variable size list of Checkboxes in Tkinter?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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", "
") # 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...