python 如何实现运行后单击屏幕的任意位置返回鼠标点击坐标的位置?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 如何实现运行后单击屏幕的任意位置返回鼠标点击坐标的位置?相关的知识,希望对你有一定的参考价值。

pyHook的方法不可以了,我试过了,请问还有什么方法嘛???求解答

你好,下面是一个对应的代码,不过你需要安装pythoncom和pyHook。
import pythoncom, pyHook
def onMouseEvent(event):
print "Position:", event.Position
return True
def main():
hm = pyHook.HookManager()
hm.HookKeyboard()
hm.MouseAllButtonsDown = onMouseEvent
hm.MouseAllButtonsUp = onMouseEvent
hm.HookMouse()
pythoncom.PumpMessages()
if __name__ == "__main__":
main()追问

说了Hook不可以了,python3已经用不了了,会报错

参考技术A 需要调用操作系统API。你用的什么操作系统追问

windows

追答

程序超出字数限制了,这里放图片

运行效果:

代码上传github了,有兴趣可自行查看

mousehook.py

追问

感谢。

本回答被提问者采纳

单击后更新按钮的位置? (Tkinter Python GUI)

启动游戏会使按钮按随机顺序(由功能引起)但当我继续按下“正确”按钮时,按钮的位置在加载屏幕后不会更新;即使他们的文字更新。即使在randomPosition函数改变每个按钮的位置后,按钮的位置也不会更新。有没有办法更新按钮的位置,以便加载屏幕后“正确”的位置不在同一个位置?

import tkinter as tk
from tkinter import StringVar
from tkinter import *
from random import randint

f1 = ("MS Serif", 29)
lives = 3
multiplier = 0
levelCounter = 1
answer2Counter = 0
answer3Counter = 1
answer4Counter = 2

def randomPosition(): #PLACES THE BUTTONS IN RANDOM VARIETY EACH TIME GAME IS OPEN
  global xpos1, xpos2, ypos3, ypos4, random
  random = randint(1,4)
  print("Random Integer Position: " + str(random))

  #CO-ORDINATES FOR BUTTON PLACEMENT
  if random == 1:
    xpos1 = 50
    xpos2 = 730
    ypos3 = 610
    ypos4 = 720
  elif random == 2:
    xpos1 = 730
    xpos2 = 50
    ypos3 = 610
    ypos4 = 720
  elif random == 3:
    xpos1 = 50
    xpos2 = 730
    ypos3 = 720
    ypos4 = 610
  elif random == 4:
    xpos1 = 730
    xpos2 = 50
    ypos3 = 720
    ypos4 = 610

randomPosition()

BtnWidth = 20 

def wrong(self, controller): 
  global lives 
  lives -= 1 
  print("Lives: " + str(lives))
  if lives == 0:
    app.destroy()

def correct(self, controller):
  global multiplier, levelCounter
  multiplier += 1
  levelCounter += 1

  answer1.set(ftvArray[levelCounter])
  answer2.set(wrongFtvArray[multiplier][answer2Counter]) #READS 1ST VALUE OF ARRAY
  answer3.set(wrongFtvArray[multiplier][answer3Counter]) #READS 2ND VALUE OF ARRAY
  answer4.set(wrongFtvArray[multiplier][answer4Counter]) #READS 3RD VALUE OF ARRAY

ftvArray = ['CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT']

wrongFtvArray = [['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG']] #'WRONG' IS A PLACEHOLDER VALUE

class Game(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side = "top", fill = "both", expand = True)

        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frame = {}

        for F in (FilmTV, LoadingScreen):

            frame = F(container, self)
            self.frame[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(FilmTV)

    def show_frame(self, cont):
        frame = self.frame[cont]
        frame.tkraise()

class FilmTV(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        global answer1, answer2, answer3, answer4
        answer1 = StringVar()
        answer1.set(ftvArray[levelCounter])
        answer2 = StringVar()
        answer2.set(wrongFtvArray[multiplier][answer2Counter])
        answer3 = StringVar()
        answer3.set(wrongFtvArray[multiplier][answer3Counter])
        answer4 = StringVar()
        answer4.set(wrongFtvArray[multiplier][answer4Counter])

        #ANSWERS
        Btn = tk.Button(self, textvariable = answer1, command = lambda:[correct(self, controller), loading(self, controller), randomPosition()], font = f1, width = BtnWidth).place(x = xpos1, y = ypos3)
        Btn2 = tk.Button(self, textvariable = answer2, command =  lambda:[wrong(self, controller)], font = f1, width = BtnWidth).place(x = xpos1, y = ypos4)
        Btn3 = tk.Button(self, textvariable = answer3, command =  lambda:[wrong(self, controller)], font = f1, width = BtnWidth).place(x = xpos2, y = ypos3)
        Btn4 = tk.Button(self, textvariable = answer4, command =  lambda:[wrong(self, controller)], font = f1, width = BtnWidth).place(x = xpos2, y = ypos4)

        def loading(self, controller):
          self.controller.show_frame(LoadingScreen)
          app.after(1500, nextLevel)

        def nextLevel():
          self.controller.show_frame(FilmTV)

class LoadingScreen(tk.Frame):
    def __init__(self, parent, controller):
      tk.Frame.__init__(self, parent)
      LoadingLbl = tk.Label(self, text = 'CORRECT ANSWER! LOADING...', font = f1).place(x = 50, y = 50)

app = Game()
app.geometry("1260x830+150+5")
app.mainloop()
答案

“点击后更新按钮的位置?(Tkinter Python GUI)”

这是一个最小的例子:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def on_button_press(widget):
    import random
    widget_size = (widget.winfo_width(), widget.winfo_height())
    window_size = (widget.winfo_toplevel().winfo_width(),
                                    widget.winfo_toplevel().winfo_height())
    random_x = random.randint(0, window_size[0] - widget_size[0])
    random_y = random.randint(0, window_size[1] - widget_size[1])
    widget.place(x=random_x, y=random_y)


def main():
    root = tk.Tk()
    button = tk.Button(root, text="Place Randomly!")
    button['command'] = lambda w=button: on_button_press(w)
    button.place(x=0, y=0)
    tk.mainloop()

if __name__ == '__main__':
    main()

以上是关于python 如何实现运行后单击屏幕的任意位置返回鼠标点击坐标的位置?的主要内容,如果未能解决你的问题,请参考以下文章

单击TableView部分中的任意位置后如何执行segue?

单击后更新按钮的位置? (Tkinter Python GUI)

单击屏幕上的任意位置关闭所有 Angular JS Bootstrap 弹出窗口?

如何在按下按钮后单击屏幕上的图像

如何通过单击菜单外部来关闭移动菜单

单击 HTML 按钮在后端运行 Python 程序