如何制作时间。

发布于 2025-01-23 23:44:14 字数 1093 浏览 0 评论 0原文

我如何制作一个输入选项,其中输入的值用于睡眠,而不是代码“ Time.sleep(10)”中的时间,例如,i将30输入到PSG中。等待30秒,然后休息

import PySimpleGUI as Psg
import time
import pause
from pynput.keyboard import Key, Controller
loop = 0
cancel_loop = 0
punch_wait = 0
punch_time = 0
keyboard = Controller()
layout = [[Psg.Button("punch")],
          [Psg.Button("Close")]]
window = Psg.Window("App", size=(300, 270)).Layout(layout)
def cancel():
    if event == "Close":
        pass
def d_press():
    keyboard.press(Key.right)
    pause.milliseconds(punch_time)
    keyboard.release(Key.right)
    pause.milliseconds(punch_wait)
    keyboard.release(Key.space)
    return event
def space_press():
    keyboard.press(Key.space)
    pause.milliseconds(1)
def movement():
    d_press()
    space_press()
while True:
    event, values = window.read()
    if event == Psg.WIN_CLOSED:
        break
    if event == "Close":
        break
    if event == "punch":
        punch_wait = 300
        punch_time = 85
        loop = 1
    while loop == 1:
        movement()
        cancel()
        time.sleep(10)
        break

How can I make an input option where the value inputted is used to sleep and not the time in the code 'time.sleep(10)', for example I inputted 30 into the Psg.Input after i press on punch i want it to wait 30 seconds then break

import PySimpleGUI as Psg
import time
import pause
from pynput.keyboard import Key, Controller
loop = 0
cancel_loop = 0
punch_wait = 0
punch_time = 0
keyboard = Controller()
layout = [[Psg.Button("punch")],
          [Psg.Button("Close")]]
window = Psg.Window("App", size=(300, 270)).Layout(layout)
def cancel():
    if event == "Close":
        pass
def d_press():
    keyboard.press(Key.right)
    pause.milliseconds(punch_time)
    keyboard.release(Key.right)
    pause.milliseconds(punch_wait)
    keyboard.release(Key.space)
    return event
def space_press():
    keyboard.press(Key.space)
    pause.milliseconds(1)
def movement():
    d_press()
    space_press()
while True:
    event, values = window.read()
    if event == Psg.WIN_CLOSED:
        break
    if event == "Close":
        break
    if event == "punch":
        punch_wait = 300
        punch_time = 85
        loop = 1
    while loop == 1:
        movement()
        cancel()
        time.sleep(10)
        break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2025-01-30 23:44:14

调用time.sleep将在主线程中阻止您的代码,并且您的GUI可能没有响应。

更喜欢在其他线程中进行。以下代码显示要走的路。

from time import sleep
import threading
import PySimpleGUI as sg

def action(window, punch_time, wait_time):
    print("Key Pressed - Space")
    print("Key Pressed - Right")
    print(f"Wait {punch_time} seconds")
    sleep(punch_time)
    print("Key Released - Right")
    print(f"Wait {wait_time} seconds")
    sleep(wait_time)
    print("Key Released - Space")
    print("Thread finished\n")
    # Call window.write("EVENT", "VALUE") in thread to update GUI if required

layout = [
    [sg.Text("Punch time", size=10), sg.Input("", size=10, key='Punch Time')],
    [sg.Text("Wait time",  size=10), sg.Input("", size=10, key='Wait Time')],
    [sg.Button('Punch'), sg.Button('Exit')],
]

window = sg.Window('App', layout)

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event == 'Punch':
        try:
            punch_time, wait_time = float(values['Punch Time']), float(values['Wait Time'])
        except ValueError:
            print('Wrong value for time')
            continue
        threading.Thread(target=action, args=(window, punch_time, wait_time), daemon=True).start()
    elif event == 'EVENT':
        value = values[event]
        """ Update GUI here """

window.close()

Call time.sleep will block your code in the main thread and there maybe no response for your GUI.

Prefer to do it in other thread. Following code show the way to go.

from time import sleep
import threading
import PySimpleGUI as sg

def action(window, punch_time, wait_time):
    print("Key Pressed - Space")
    print("Key Pressed - Right")
    print(f"Wait {punch_time} seconds")
    sleep(punch_time)
    print("Key Released - Right")
    print(f"Wait {wait_time} seconds")
    sleep(wait_time)
    print("Key Released - Space")
    print("Thread finished\n")
    # Call window.write("EVENT", "VALUE") in thread to update GUI if required

layout = [
    [sg.Text("Punch time", size=10), sg.Input("", size=10, key='Punch Time')],
    [sg.Text("Wait time",  size=10), sg.Input("", size=10, key='Wait Time')],
    [sg.Button('Punch'), sg.Button('Exit')],
]

window = sg.Window('App', layout)

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event == 'Punch':
        try:
            punch_time, wait_time = float(values['Punch Time']), float(values['Wait Time'])
        except ValueError:
            print('Wrong value for time')
            continue
        threading.Thread(target=action, args=(window, punch_time, wait_time), daemon=True).start()
    elif event == 'EVENT':
        value = values[event]
        """ Update GUI here """

window.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文