简体   繁体   English

如何在Python中完成启用/禁用TkInter列表框

[英]How to complete enable / disable a TkInter listbox in Python

How can I simply disable a TkInter Listbox? 如何简单地禁用TkInter列表框? It seems such a straightforward thing to do and possibly it is. 这似乎是一件很简单的事情,而且可能是。 In the simple example below, I've got a button which should toggle the state of the listbox from fully selectable to greyed and unselectable. 在下面的简单示例中,我有一个按钮,该按钮应将列表框的状态从完全可选择状态切换为灰色和不可选择状态。

#!/usr/bin/python

from Tkinter import *

class MyDialog:
    def __init__(self, rootWin):
        self.rootWin_ = rootWin
        self.frame_ = Frame( self.rootWin_, borderwidth=10 )
        self.frame_.grid(row=0, column=0)
        self.listBox_ = Listbox( self.frame_, height=4, width=30, selectbackground='#000000' )
        self.listBox_.grid(row=0, column=0)
        self.lbEnabled_ = 1
        for item in [ 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet' ]:
            self.listBox_.insert(END, item)
        self.button_ = Button( self.frame_, text='Disable', command=self.onEnableDisable)
        self.button_.grid(row=1, column=0)

    def go(self):
        self.rootWin_.mainloop()

    def onEnableDisable(self):
        if self.lbEnabled_ == 1:
            self.button_.config( text='Enable' )
            # TODO enable the list box
            self.lbEnabled_ = 0
        else:
            self.button_.config( text='Disable' )
            # TODO disable the list box
            self.lbEnabled_ = 1

def main():
    myDlg = MyDialog(Tk())
    myDlg.go()

if __name__ == '__main__':
    main()

I've tried a few things including changing state: 我已经尝试了一些方法,包括更改状态:

self.listBox_.config( state = DISABLED )

The reference documentation I've found suggests you can set this with the state attribute: 我发现的参考文档建议您可以使用state属性进行设置:

state By default, a listbox is in the NORMAL state. state默认情况下,列表框处于NORMAL状态。 To make the listbox unresponsive to mouse events, set this option to DISABLED. 要使列表框对鼠标事件不响应,请将此选项设置为DISABLED。

However, if I try this, all I get is: 但是,如果我尝试这样做,我得到的只是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "./example.py", line 24, in onEnableDisable
    self.listBox_.config( state = DISABLED )
  File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1139, in configure
    return self._configure('configure', cnf, kw)
  File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1130, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: unknown option "-state"

As you can see, I'm using a pretty old version (2.4) of python which probbaly won't help, but I have no control over. 如您所见,我使用的是python的旧版本(2.4),可能不会帮助您,但我无法控制。 Any ideas? 有任何想法吗?

Your proposed code works for me (python 2.6, OS-X). 您建议的代码对我有用(python 2.6,OS-X)。 It actually looks like Tkinter is doing the right thing from the traceback. 实际上, Tkinter看起来从追溯中做对了。 Perhaps you're also using an old version of Tcl/Tk which could also cause the problem you're seeing. 也许您还使用了旧版本的Tcl/Tk ,这也可能会导致您遇到的问题。 If you don't have control over the python version, do you have control over the Tk version? 如果您无法控制python版本,那么您可以控制Tk版本吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM