我想要找出在GUI上找到鼠標光標位置的小解決方案。Python - 尋找獲取鼠標位置的指導
以下是我的代碼。我確實得到了鼠標的位置,但只有當鼠標移動而沒有點擊時。事實上,我想知道只有當鼠標點擊時,我纔可以將此解決方案獲得X,Y座標嗎? 我在這裏使用這段代碼遇到的另一個問題是,當我點擊屏幕上的任何地方時,我的GUI就會關閉,之後我無法做任何事情。請讓我知道有沒有可能的解決方案?原諒我如果我的編碼風格不符合標準。
# Globals
# ------------------
x_pad = 0
y_pad = 0
import win32api, win32con
import wx
import time
import sys
def mousePos(cord):
win32api.SetCursorPos((x_pad + cord[0], y_pad + cord[1]))
class URL(wx.Frame):
def __init__(self, *args, **kw):
super(URL, self).__init__(*args, **kw)
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
sbtn = wx.Button(pnl, label='Start', pos=(10, 30))
cbtn = wx.Button(pnl, label='Close', pos=(10, 80))
stdot = wx.TextCtrl(pnl, pos =(100,30),size=(150,100), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
sys.stdout = stdot
sys.stderr = stdot
sbtn.Bind(wx.EVT_BUTTON, self.OnStart)
cbtn.Bind(wx.EVT_BUTTON, self.OnClose)
w = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
h = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
APPWIDTH = 300
APPHEIGHT = 200
posx = w - APPWIDTH
posy = h - APPHEIGHT
self.SetSize((300, 150))
self.SetTitle('Get XY Coordinates')
self.SetPosition((posx, posy))
self.Show(True)
def OnStart(self,e):
while 1 == 1:
x,y = win32api.GetCursorPos()
x = x - x_pad
y = y - y_pad
print x,y
time.sleep(2)
def OnClose(self, e):
self.Close(True)
def main():
ex = wx.App()
URL(None)
ex.MainLoop()
if __name__ == '__main__':
main()
我用x,y = wx.GetMousePoistion替換了x,y = win32api.GetCursorPos(),但它只是作爲替換。當我點擊屏幕上的任何地方時,我的應用程序就會卡住你覺得我還應該做點別的嗎? – just10minutes