【问题标题】:How to get value of checkbox in tix如何在tix中获取复选框的值
【发布时间】:2017-12-07 08:33:40
【问题描述】:

我可以获取复选框的状态,但如何在Tix 中获取复选框的值。

from tkinter import tix

class View(object):
    def __init__(self, root):
        self.root = root
        self.makeCheckList()

    def makeCheckList(self):
        self.cl = tix.CheckList(self.root, browsecmd=self.selectItem)
        self.cl.pack()
        self.cl.hlist.add("CL1", text="C:/")
        self.cl.hlist.add("CL1.Item1", text="subitem1")
        self.cl.hlist.add("CL2", text="some folder")
        self.cl.hlist.add("CL2.Item1", text="test")
        self.cl.setstatus("CL2", "on")
        self.cl.setstatus("CL2.Item1", "on")
        self.cl.setstatus("CL1", "off")
        self.cl.setstatus("CL1.Item1", "off")
        self.cl.autosetmode()

    def selectItem(self, item):
        print (item, self.cl.getstatus(item))

def main():
    root = tix.Tk()
    view = View(root)
    root.update()
    root.mainloop()

if __name__ == '__main__':
    main()     

预期输出

如果我点击复选框c:/,那么它应该打印c:/。我想要复选框的值。

输出

【问题讨论】:

    标签: python user-interface tkinter tix


    【解决方案1】:

    全部保存在hlist

    Official documentation更多关于hlist的信息发送到tcl/tk documentation然后你可以找到

    pathName item_cget entryPath col option
    

    但它并没有像我预期的那样工作

    value = self.cl.hlist.item_cget('CL1.Item1', 0, 'text')
    
    # _tkinter.TclError: unknown option "text"
    

    你必须使用"-text" 而不是"text"

    value = self.cl.hlist.item_cget('CL1.Item1', 0, '-text')
    

    【讨论】:

    • 如何找到父路径? (路径名信息父 entryPath )。这是在文档中给出的,但我没有找到任何有用的解释。你能告诉我如何找到父路径吗?
    • doc 以 tcl/tk 语言显示代码。在您的代码中pathName 表示self.cl.histentryPath 表示CLI1CLI1.Item1 等。parent 可能表示self.root
    • self.cl.hlist.info(self.root,'CL1') 是否正确?
    • 我检查了文档,我看到 parent 是粗体 - 这意味着它将是 python 中的函数 info_parent()。 `parent_id = self.cl.hlist.info_parent('CL1')
    • 顺便说一句:在代码中尝试help(self.cl.hlist),它将显示所有功能 - 有时带有描述。
    猜你喜欢
    • 2012-03-06
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2011-05-27
    • 2016-02-28
    • 2023-03-13
    • 2012-09-23
    • 1970-01-01
    相关资源
    最近更新 更多