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 秒然后输入我想要它做的是在下一次点击类型上

任何帮助将不胜感激,不仅仅是让它工作,而且帮助我学习。

4

1 回答 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() 
于 2019-02-02T14:40:37.660 回答