在Python2.7中使用tkinter动态添加复选框

2024-10-01 17:36:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过一个预先制作的列表将CKBOX动态添加到我的gui中。如何用列表中的名称填充GUI?它们都有相同类型的功能,所以这不应该是一个问题。在


Tags: 功能名称类型列表动态guickbox
2条回答

使用带有复选框的treeview。在

Treeview with checkboxes

How to create a tree view with checkboxes in Python

如果您想在启动时用预先制作的列表填充GUI:

from Tkinter import *

root = Tk()

premadeList = ["foo", "bar", "baz"]

for checkBoxName in premadeList:
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

root.mainloop()

如果要在运行时用复选框动态填充GUI:

^{pr2}$

当然,你可以同时做到:

import random
import string
from Tkinter import *

root = Tk()

def addCheckBox():
    checkBoxName = "".join(random.choice(string.letters) for _ in range(10))
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

b = Button(root, text="Add a checkbox", command=addCheckBox)
b.pack()

premadeList = ["foo", "bar", "baz"]

for checkBoxName in premadeList:
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

root.mainloop()

相关问题 更多 >

    热门问题