wxPyhton practical notes--create a window and display mouse coordinates
Main knowledge points involved
Trigger your own defined related functions by binding the event response of the mouse moving in the window
import wx
'''
pip install wxpython
'''
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "wxpython的入门", size=(500, 300))
panel = wx.Panel(self, -1)
panel.Bind(wx.EVT_MOTION, self.OnMove)
wx.StaticText(panel, -1, "鼠标在窗口中的坐标:", pos=(10, 12))
self.posCtrl = wx.TextCtrl(panel, -1, '', pos=(200, 10))
def OnMove(self, event):
pos = event.GetPosition()
self.posCtrl.SetValue("{} {}".format(pos.x, pos.y))
def main():
app = wx.PySimpleApp() # 创建窗口app
frame = MyFrame()
frame.Show(True)
app.MainLoop() # 运行窗口循环
if __name__ == '__main__':
main()