使用Python计划任务栏时,无法打开并且无法执行任何操作

with python schedule tray can#39;t open and can#39;t perform any action(使用Python计划任务栏时,无法打开并且无法执行任何操作)

本文介绍了使用Python计划任务栏时,无法打开并且无法执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用python语言编写了一个桌面应用程序程序。并为员工创建了一个登录表单,登录后,我创建了一个时间表,调用了一个用于检查员工是否登录的API,并打开了一个确认员工登录的弹出消息。

我的问题是计划什么时候开始,这样我才能停止代码

import PySimpleGUI as sg
import requests
from psgtray import SystemTray
import json
import schedule
from schedule import *
import gtts
import threading
import time


def make_sch(user_id: object, emp_name: object):
    schedule.every(4).seconds.do(func_msg, user_id, emp_name)
    while True:
        schedule.run_pending()
        time.sleep(4)


def func_msg(user_id: object, emp_name: object):
    response_msg = '{ "msg": "Data Found","status": 1,"Data": "hello team","id": 2}'
    response_msg_json: object = json.loads(response_msg)
    print(response_msg_json)
    if str(response_msg_json['status']) == '1' and str(response_msg_json['id']) != '':
        sg.theme('DarkTeal9')  # Add a touch of color
        layout = [[sg.Text(response_msg_json['Data'])],
                  [sg.Text('Enter your Acknowledge'), sg.Multiline(size=(50, 5), key='textbox')],
                  [sg.Button('Enter'), sg.Button('Cancel')],
                  [sg.Text(size=(40, 1), key='ACK_OUTPUT')]]

        # Create the Window
        window = sg.Window('Team Alert', layout)
        # Event Loop to process "events" and get the "values" of the inputs
        while True:
            event, values = window.read()
            if event == sg.WIN_CLOSED or event == 'Cancel':  # if user closes window or clicks cancel
                break
            elif event == 'Enter' and values['textbox'] == '':
                window['ACK_OUTPUT'].update('Please enter your Acknowledgement', text_color='red')
            else:
                window['ACK_OUTPUT'].update('', text_color='red')
                response_update = '{"msg": "Data Found","status": 1}'
                response_update_json: object = json.loads(response_update)
                sg.Popup('Thanks For Your Acknowledgement!!')
                window.close()

        window.close()


def main():
    menu = ['',
            ['Show Window', 'Hide Window', '---', '!Disabled Item', 'Exit']]
    tooltip = 'Team Alert'
    sg.theme('DarkTeal9')
    layout = [[sg.Text("* Enter Your Id")],
              [sg.Input(key='emp_id')],
              [sg.Text(size=(40, 1))],
              [sg.Text("* Enter Your Password")],
              [sg.Input(key='emp_pass')],
              [sg.Text(size=(40, 1), key='-OUTPUT-')],
              [sg.Button('Login')]]

    window = sg.Window('Team Alert', layout, finalize=True, enable_close_attempted_event=True)
    tray = SystemTray(menu, single_click_events=True, window=window, tooltip=tooltip)
    tray.show_message('Team Alert', 'Team Alert Started!')
    sg.cprint(sg.get_versions())
    while True:
        event, values = window.read()
        if values['emp_id'] and values['emp_pass']:
            response = '{ "msg": "Sucessfully login", "status": 1}'
            login_data_json: object = json.loads(response)
            if str(login_data_json['status']) == '1':
                window.hide()
                tray.show_icon()
                window['-OUTPUT-'].update(login_data_json['msg'], text_color='yellow')
                emp_id = values['emp_id']
                emp_name = 'emp'
                make_sch(emp_id, emp_name)
            else:
                window['-OUTPUT-'].update(login_data_json['msg'], text_color='red')
            if event == 'Login':
                sg.Popup(login_data_json['msg'])
        else:
            window['-OUTPUT-'].update('** Username and Password is required', text_color='red')
        if event == tray.key:
            sg.cprint(f'Team Alert Event = ', values[event], c='white on red')
            event = values[event]
        if event in (sg.WIN_CLOSED, 'Exit'):
            sg.Popup("You are Logout!!")
            break

        sg.cprint(event, values)
        tray.show_message(title=event, message=values)

        if event in ('Show Window', sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED):
            window.un_hide()
            window.bring_to_front()
        elif event in ('Hide Window', sg.WIN_CLOSE_ATTEMPTED_EVENT):
            window.hide()
            tray.show_icon()
        elif event == 'Hide Icon':
            tray.hide_icon()
        elif event == 'Show Icon':
            tray.show_icon()
        elif event == 'Change Tooltip':
            tray.set_tooltip(values['-IN-'])

    tray.close()
    window.close()

    print('this is event', event)


if __name__ == '__main__':
    main()

推荐答案

以下代码适用于不同情况,但仅供您参考如何使用多线程。

from time import sleep
import threading
import math

import PySimpleGUI as sg

def capture_data(window):

    global duration, running
    theta = 0
    while running:
        x = [(i+theta)/180*math.pi for i in range(w)]
        y = [sum([math.sin(i*j)/i/math.pi for i in range(1, 8, 2)])*h+h//2 for j in x]
        x = [i for i in range(w)]
        window.write_event_value('Plot', [(i, j) for i, j in zip(x, y)])
        theta = (theta+1) % 360
        sleep(duration)

font = ("Courier New", 11)
sg.theme("DarkBlue3")
sg.set_options(font=font)
w, h = size = (360, 240)

layout = [
    [sg.Graph(size, (0, 0), size, background_color='green', key='Graph')],
    [sg.Push(), sg.Button('Exit')]
]
window = sg.Window('Matplotlib', layout, finalize=True)
graph = window['Graph']

theta, duration, running, curve = 0, 0.01, True, None
threading.Thread(target=capture_data, args=(window, ), daemon=True).start()

while True:

    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, 'Exit'):
        running = False
        sleep(0.1)
        break
    elif event == 'Plot':
        points = values['Plot']
        if curve:
            graph.delete_figure(curve)
        curve = graph.draw_lines(points, color='white', width=1)

window.close()

这篇关于使用Python计划任务栏时,无法打开并且无法执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用Python计划任务栏时,无法打开并且无法执行任何操作

基础教程推荐