wxPython SetFocus()不起作用

时间:2015-09-23 03:10:11

标签: python wxpython

我试图在wxPython的窗口上使用SetFocus()。我不确定它在Mac上应该是什么样子,但据我所知,当我调用window.SetFocus()时,根本没有视觉上的变化,而window.HasFocus()返回False。这是一些简化的示例代码:

 while there are still edges in S
   Let V = leftmost vertical edge taken from S
   Determine Vv, the intersection of V with the visible parts of C
   if V is of the form (L, i) // a left edge
     Update P with Vv (polylines may be added or joined)
     add (R, i) to S
     add (T, i) and (B, i) to C, incrementing depths as needed
   else // V is of the form (R, i) // a right edge
     Update P with Vv (polylines may be removed or joined)
     remove (T, i) and (B, i) from C, decrementing depths as needed

wxPython版本' 2.9.2.4'存在此问题。和' 3.0.2.0'。有任何想法吗?

编辑:

看起来静态文本小部件无法接受焦点(window.AcceptFocus())返回False)。在这种情况下,是否有一种简单,可接受的方式突出显示这样的窗口?或者,是否可以更改窗口是否可以接受焦点?

3 个答案:

答案 0 :(得分:1)

因为它是静态文本,所以没有什么可以获得焦点,因为你无法点击它 尝试稍微修改一下代码,看看如何设置焦点并使用event.focus来查看焦点的变化。

import wx
def onFocus(event):
    print "widget received focus!"

def onKillFocus(event):
    print "widget lost focus!"

app = wx.App()

frame = wx.Frame(None, -1, '')
box = wx.StaticBox(frame, -1, "")
sizer = wx.StaticBoxSizer(box, orient=wx.VERTICAL)
text0 = wx.StaticText(frame,label="1st Item")
text0_input = wx.TextCtrl(frame, wx.ID_ANY, size=(345,25))
text = wx.StaticText(frame, label="Some Text")
text_input = wx.TextCtrl(frame, wx.ID_ANY, size=(345,25))
sizer.Add(text0, wx.ALIGN_LEFT|wx.ALL, border=10)
sizer.Add(text0_input, wx.ALIGN_LEFT|wx.ALL, border=10)
sizer.Add(text, wx.ALIGN_LEFT|wx.ALL, border=10)
sizer.Add(text_input, wx.ALIGN_LEFT|wx.ALL, border=10)
text0_input.Bind(wx.EVT_SET_FOCUS, onFocus)
text0_input.Bind(wx.EVT_KILL_FOCUS, onKillFocus)

main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(sizer)
frame.SetSizer(main_sizer)

frame.Centre()
frame.Show()
text_input.SetFocus()
#print 'text_input.HasFocus?', text_input.HasFocus()

app.MainLoop()

答案 1 :(得分:0)

wx.StaticText窗口小部件不接受焦点。如果您只是想将窗口置于前面,则可以使用框架的Raise方法。大多数其他小部件允许您将焦点设置在它们上,以便您可以编辑它们。

如果要突出显示wx.StaticText窗口小部件中的某些文本,可以尝试更改其背景颜色。这应该适用于大多数情况。

答案 2 :(得分:0)

回答你评论中提出的第二个问题 当你说设置背景颜色会产生一个非常薄的高光时,我不确定你指的是什么 您可以通过启用或禁用它们来关注在任何给定时刻相关或不相关的按钮。您还可以根据它们是启用,禁用还是一时兴起来更改颜色。

例如:

        self.button.Disable()
        self.button.SetBackgroundColour(wx.NullColour)

        self.button.Enable()
        self.button.SetBackgroundColour('gold')    

设置背景颜色应改变整个按钮的颜色 您还可以更改前景色,这将改变任何给定按钮上文本的颜色。