admin管理员组

文章数量:1640615

我如何简单地禁用TkInter列表框?这似乎是一件很简单的事情,可能是这样。在下面的简单示例中,我有一个按钮,它应该将列表框的状态从完全可选状态切换为灰色和不可选。

#!/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()我尝试了一些东西,包括改变状态:

self.listBox_.config( state = DISABLED )我发现的参考文档建议你可以用state属性来设置它:

state By default, a listbox is in the NORMAL state. To make the listbox unresponsive to mouse events, set this option to DISABLED.

但是,如果我尝试这个,我所得到的是:

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"正如你所看到的,我使用的是Python的一个很老的版本(2.4),probbaly不会帮助,但我无法控制。有任何想法吗?

本文标签: 如何在列表StatePythonconfig