单击wx.TextCtrl时启用按钮

问题描述 投票:0回答:1

我有一个对话框(带有多个可编辑的文本字段和按钮),并希望在单击任何一个文本字段框时激活其中一个按钮。如下所示,默认值为0.56,一旦用户单击该框,则单独显示应该启用按钮。有什么建议?

wx.StaticText(panel, -1, "Reach Slope", (40, 170))
self.reachslope = wx.TextCtrl(panel, -1, value=str(0.56), pos=(150, 165), size=(75,25))
python wxpython wxtextctrl
1个回答
1
投票

绑定到wx.TextCtrl()事件EVT_SET_FOCUS,当textctrl获得焦点并使用该事件启用按钮时触发。 这是一个构造不良的例子:

import wx

def on0Focus(event):
    button0.Enable()
    button1.Disable()
    print "text0 widget received focus!"

def on1Focus(event):
    button1.Enable()
    button0.Disable()
    print "text1 widget received focus!"

app = wx.App()

frame = wx.Frame(None, -1, 'Set Focus Test', size=(500,100))

dummy = wx.TextCtrl(frame, wx.ID_ANY, size=(1,1), pos=(10,1))#Prevents text0 getting focus on Show()
text0 = wx.TextCtrl(frame, wx.ID_ANY, size=(345,25), pos=(10,10))
text0.SetValue("123456")
button0 = wx.Button(frame,-1, "Zero",pos=(400,10))

text1 = wx.TextCtrl(frame, wx.ID_ANY, size=(345,25), pos=(10,40))
text1.SetValue("abcdef")
button1 = wx.Button(frame,-1, "One", pos=(400,40))

text0.Bind(wx.EVT_SET_FOCUS, on0Focus)
text1.Bind(wx.EVT_SET_FOCUS, on1Focus)
button0.Disable()
button1.Disable()

frame.Show()

app.MainLoop()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.