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

如何简单地禁用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()

我尝试了一些方法,包括改变状态:

^{pr2}$

我找到的参考文档建议您使用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不会帮上忙,但我无法控制它。有什么想法吗?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值