首先,我是编程的新手,并且现在仅学习基础知识。 我正在使用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秒然后键入 我想做的是在下次点击类型上
任何帮助将不胜感激,不仅仅是使它起作用,还可以帮助我学习。
答案 0 :(得分: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()