wxPython – 鼠标悬停在按钮上时更改光标
在本文中,我们将学习如何在光标悬停在框架中的按钮上时更改光标。我们需要按照以下步骤进行操作。
Step 1- Create a wx.Image object with image you want to use as cursor image.
Step 2- Create a wx.Cursor object and pass wx.Image object above created.
Step 3- Set the cursor using SetCursor() function.
Syntax: wx.Button.SetCursor(cursor)
Parameters:
Parameter | Input Type | Description |
---|---|---|
cursor | wx.Cursor | cursor to be set. |
代码示例:
Python3
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
self.pnl = wx.Panel(self)
# CREATE BUTTON AT POINT (20, 20)
self.st = wx.Button(self.pnl, id = 1, label ="Button", pos =(20, 20),
size = wx.DefaultSize, name ="button")
# CREATE CURSOR OBJECT
c = wx.Cursor(wx.Image('pointer.png'))
# SET c AS CURSOR
self.st.SetCursor(c)
self.SetSize((350, 250))
self.SetTitle('wx.Button')
self.Centre()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
输出窗口: