能不能告诉我,当我点击鼠标第二次(而不是当前窗口)的功能

问题描述 投票:1回答:1

首先我是新来的编程和刚学此刻的基础。我创建一个“产品选择器”使用Python 3.7(在pycharm),并希望鼠标点击该产品的一个按钮,然后在我的下一个鼠标点击粘贴/打印/插入产品名称为我的发票系统。

目前我的代码中使用“时间”模块设置的延迟,该类型的让我选择在那里要键入它(代码行49)前工作正常。这里是我的代码:

我已经尝试过的事情像onMouseUp或onDoubleClick(我知道这些都是不正确的代码,但你的想法)。

# import statements import modules
from tkinter import *  # tkinter helps create GUI's
from pynput.keyboard import Key, Controller as keyboardController  # helps 
control keyboard functions
from pynput.mouse import Listener, Controller as mouseController  # helps 
control mouse functions
import time
import pyautogui


# since pynput.mouse and .keyboard have 'Controller' we have to rename them 
so 
both will work
keyboarad = keyboardController()
mouseclick = mouseController()

# defining some variables
root = Tk()
root.grid()
btn = {}
raw_col = 1


# product lists set into categories

product = [
            'product1',
            'product 2',
            'product 3',
            'product 4',
            'product 5'
]


def picker(product):

    time.sleep(3)
    keyboarad.type(product)
    time.sleep(.5)
    keyboarad.press(Key.tab)
    keyboarad.release(Key.tab)
    keyboarad.press(Key.tab)
    keyboarad.release(Key.tab)


# for each 'item in 'raw' list we want you to create a button
for item in product:

    btn[item] = Button(root, text=item, command=lambda x=item: picker(x), 
height=1, width=25)

    btn[item].grid(column=1, row=raw_col, pady=5, padx=5)
     raw_col += 1


mainloop() 

什么是实际发生的那一刻就等待3秒钟然后种什么我想它做的是对未来的点击类型

任何帮助将不胜感激,不只是为了得到它的工作,但帮助我学习。

python-3.x tkinter buttonclick pyautogui pynput
1个回答
1
投票

命令time.sleep(3)暂停三秒钟,在此期间它是无响应的主循环。尝试使用after()而不是因为这将调度后的作用,但不能暂停主循环。

更新我的事情我不太明白你是什么之后。要在点击打印之前的选择,你必须保存在某些方面的状态。在我的例子,我简单地使用全局变量:

from tkinter import *

root = Tk()
button_dict = {}

# product lists set into categories
product_list = ['product 1', 'product 2', 'product 3', 'product 4', 'product 5']

def paste_to_invoice(p):
    # Just to have a function as placeholder for invoicing system
    print('Previous choice was:', p)

last_pick = None    # Global variable to hold previous selection
def picker(product):
    global last_pick
    if last_pick != None:   # Is there a previous choice?
        paste_to_invoice(last_pick)     # Then print it
    last_pick = product     # Save this choice for next click

# for each 'item in 'raw' list we want you to create a button
for row, item in enumerate(product_list):
    b = Button(root, text=item, command=lambda x=item: picker(x))
    b.grid(column=1, row=row+1, pady=5, padx=5)
    button_dict[item] = b  # Save reference to button

mainloop() 

推荐问答