Tkinter的蟒蛇:如何删除从ttk.Entry输入光标?
题
我有ttk.Entry的一个实例。 用户点击它。 我有事件的约束。 根据一些条件,我要么需要将输入光标出现,让打字的或的本质我想忽略点击,而不必输入光标出现在ttk.Entry。我的不的想拥有使用只读或禁用状态。
操纵焦点也没有效果。
解决方案 2
拖网TTK文档后,此是卓有成效的:
ttk.Style().map("TEntry",
foreground=[('disabled', 'black')],
fieldbackground=[('disabled','white')]
)
widget['state'] = 'disabled'
其他提示
下面是一个类,做什么你问。
class MyEntry(Entry):
def disable(self):
self.__old_insertontime = self.cget('insertontime')
self.config(insertontime=0)
self.bind('<Key>', lambda e: 'break')
def enable(self):
self.unbind('<Key>')
if self.cget('insertontime') == 0:
self.config(insertontime=self.__old_insertontime)
但是,因为你真正关心的是,你不希望禁用的条目照看残疾,只需设置disabledbackground
和disabledforground
的颜色相配background
和forground
的颜色。如果您需要此卷成类,像这样做:
class MyEntry(Entry):
def __init__(self, *args, **kwds):
Entry.__init__(self, *args, **kwds)
self.config(disabledbackground=self.cget('background'))
self.config(disabledforeground=self.cget('foreground'))
和使用它是这样的:
e = MyEntry(root)
e.config(state=DISABLED) # or state=NORMAL
注意。强>重塑GUI约定时要小心。有一些看起来启用禁用行为可能会造成混淆用户。所以,除非你有很好的理由不改变这一点。
不隶属于 StackOverflow