用Python中的tablelist包装器配置表格列表中的复选框的值

2024-06-16 12:52:41 发布

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

基本上,我的问题是在用python编写的tablelist中配置组合框(而不是直接使用tcl)。我准备了一个可以演示问题的示例,但在此之前,让我们看看运行它所需的步骤:

  1. here复制tablelist包装并将其另存为'表格列表.py,然后将其放在示例代码目录中。在
  2. here下载“tklib-0.5”,并将“tablelist”目录从“modules”目录复制到示例代码的目录中。在

这里是代码:

from tkinter import *
from tkinter import ttk
from tablelist import TableList

class Window (Frame):
    def __init__(self):
#       frame
        Frame.__init__(self)
        self.grid()
#       tablelist
        self.tableList = TableList(self,
                                    columns = (0, "Parity"),
                                    editstartcommand=self.editStartCmd
                                    )
        self.tableList.grid()
#       configure column #0 
        self.tableList.columnconfigure(0, editable="yes", editwindow="ttk::combobox")
#       insert an item 
        self.tableList.insert(END,('Even'))

    def editStartCmd(self, table, row, col, text):
#
#       must configure "values" option of Combobox here! 
#
        return

def main():
    Window().mainloop()

if __name__ == '__main__':
    main()

正如您将看到的那样,结果是一个具有初始值(偶数)的单列/单元格窗口。通过单击单元格,组合框将出现(因为使用了“editstartcommand”),它没有任何值(无)。我知道在编辑cell的widgets时,必须使用“editwinpath”命令来获取临时小部件的路径名,但是该方法只返回一个指向combobox小部件的不可调用的地址字符串。在

如果有任何帮助或可能的解决方案,我将不胜感激。在


Tags: 代码fromimportself目录示例heremain
1条回答
网友
1楼 · 发布于 2024-06-16 12:52:41

我知道我回答自己的问题有点奇怪,但我希望它将来能对其他人有用。在阅读了TCL中的tablelist脚本和示例之后,我找到了我的答案。答案有两个不同的部分,一个与包装器有关(python中的tablelist包装器,您可以找到它here),另一个则位于我的示例代码中。首先,需要修改包装器,使其在“TableList”类的“configure”方法中包含小部件的路径名。这里是修改后的版本:

def configure(self, pathname, cnf={}, **kw):
    """Queries or modifies the configuration options of the 
    widget.  

    If no option is specified, the command returns a list 
    describing all of the available options for def (see 
    Tk_ConfigureInfo for information on the format of this list).  
    If option is specified with no value, then the command 
    returns a list describing the one named option (this list 
    will be identical to the corresponding sublist of the value 
    returned if no option is specified).  If one or more 
    option-value pairs are specified, then the command modifies
    the given widget option(s) to have the given value(s); in 
    this case the return value is an empty string.  option may 
    have any of the values accepted by the tablelist::tablelist 
    command.
    """
    return self.tk.call((pathname, "configure") +
                 self._options(cnf, kw))

第二部分也是最后一部分是“editstartcommand”(在我的示例代码“editStartCmd”方法的情况下)必须在末尾返回其“text”变量的义务。这个技巧可以防止条目在你点击时变成“无”。示例代码的正确形式是:

^{pr2}$

就这样,我希望它足够清楚。在

相关问题 更多 >