wxPython 更改鼠标光标以通知长时间运行的操作

6 投票
1 回答
6820 浏览
提问于 2025-04-17 05:06

我正在写一个Python程序,用来在一个远程网站上搜索东西。有时候,这个操作会花费很多秒钟,我觉得用户可能不会注意到状态栏上显示的“搜索操作进行中”的信息。因此,我想把鼠标光标换成其他样式,以便让用户知道程序还在等待结果。

这是我正在使用的方法:

def OnButtonSearchClick( self, event ):
        """
        If there is text in the search text, launch a SearchOperation.
        """
        searched_value = self.m_search_text.GetValue()

        if not searched_value:
            return

        # clean eventual previous results
        self.EnableButtons(False)
        self.CleanSearchResults()

        operations.SearchOperation(self.m_frame, searched_value)

在最后一行之前,我尝试了两种不同的方法:

  • wx.BeginBusyCursor()
  • self.m_frame.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))

但是这两种方法都没有奏效。

我在GNU/Linux的KDE环境下使用这个程序。这在Gnome环境下也不行。

有没有什么建议?谢谢!

1 个回答

6

我问了wxPython的开发者Robin Dunn关于这个问题,他说这个应该是可以工作的,但实际上却不行。不过,如果你使用面板的SetCursor()方法,那就可以正常工作,听说是这样的。下面是一个你可以尝试的例子:

import wx

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a self.panel so it looks the correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        btn = wx.Button(self.panel, label="Change Cursor")
        btn.Bind(wx.EVT_BUTTON, self.changeCursor)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(btn)
        self.panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def changeCursor(self, event):
        """"""
        myCursor= wx.StockCursor(wx.CURSOR_WAIT)
        self.panel.SetCursor(myCursor)


#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

撰写回答