Variable size list of flags in Tkinter?

I am working on a programming task. I work in Python and using Tkinter for our GUI. I cannot change the language or the GUI tool, and I cannot use any additional packages (e.g. Tix ).

I need to make a list of items that need to be pulled out. The first thing I thought was a checkbox. However, as far as I know, Tkinter does not have anything that supports a large number of (100+) flags. The number is not constant and is likely to be different each time the program starts. In my own frame, I did not find a way to make a scrollable frame. I tried Listbox , but there is no good way to select multipliers on this scale.

Do any of you know how to do this?

+8
python checkbox tkinter
source share
1 answer

Tkinter supports a relatively unlimited number of flags, limited mainly by practical issues, such as system memory and usability restrictions.

There are at least three methods for creating a scrollable container for widgets. Both canvases and text widgets support scrolling, so it’s common practice to use one of them for a container. You can also perform clever stunts with the team place, if you need something difficult.

Using a canvas is good if you want to scroll a frame containing more than just a vertical list of objects. Using a text widget is very convenient if you only need to create one vertical list.

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

When you learn more about Tkinter, you will realize that there are not as many built-in widgets as some other tools. I hope you also understand that Tkinter has enough fundamental blocks to do everything you can imagine.

+14
source share

All Articles