如何在 Tkinter 中输入时间并使用它来运行代码?

问题描述

我给自己安排了一个任务,即编写一个自动运行的缩放代码,并让它在登录会议时播放一些声音。

我看了几个关于这个的教程,找到了一个适合我的需要。我复制粘贴它并做了一些调整(警告:我完全是初学者,就像我两周前开始的,对不起......)。但我注意到他们都使用自类型的时间和链接值。

我想要做的是使其具有交互性,使用 Tkinter 或 PySimpleGUI(我觉得两个更容易理解),以便用户可以通过 GUI 输入信息,然后使该代码在特定时间运行.

我需要做什么才能在 GUI 中制作时间格式的输入小部件?我只是想做两个括号,一个是时间,另一个是复制粘贴的链接,那个时候会打开。

附言这将是我使用的代码(这是我得到它的 github 链接ZoomAutoLogin,它导入了包含所有值的列表(我不知道我是否可以更改此代码以便它与 GUI 一起使用。)

import time
from datetime import datetime
from pynput.keyboard import Controller,Key
from data import lst
import webbrowser

keyboard = Controller()

isstarted = False

for i in lst:
    while True:
        if isstarted == False:
            if (datetime.Now().hour == int(i[1].split(':')[0]) and
                datetime.Now().minute == int(i[1].split(':')[1])):
                webbrowser.open(i[0])
                isstarted = True
        elif isstarted == True:
            if (datetime.Now().hour == int(i[2].split(':')[0]) and
                datetime.Now().minute == int(i[2].split(':')[1])):
                keyboard.press('w')
                time.sleep(1)
                keyboard.press(Key.enter)
                isstarted = False
                break

这将是来自数据的列表:

lst = [
    ["link1","2:00","2:30"],["link2","2:31","3:00"],["link3","3:01","3:30"]
]

解决方法

这是我在 PySimpleGUI 中通过鼠标滚轮选择时间的一个示例。

enter image description here

from datetime import datetime
import PySimpleGUI as sg

def popup_get_time(start_hour=None,start_minute=None):
    """
    Using mouse wheel to select hour and minute
    :param start_hour: Default value for hour. 0 ~ 23
    :type start_hour: int
    :param start_minute: Default value for hour. 0 ~ 59
    :type start_minute: int
    """
    option = {
        "font": ("Courier New",48,"bold"),"enable_events": True,"background_color": None,"text_color": "white","justification": "center","pad": (0,0)
    }
    now = datetime.now()
    hour,minute = now.hour,now.minute
    hour = hour if start_hour is None else max(0,min(int(start_hour),23))
    minute = minute if start_minute is None else max(0,min(int(start_minute),59))
    layout = [
        [sg.Text(f"{hour:0>2d}",**option,size=(3,1),key="Hour"),sg.Text(":",**option),sg.Text(f"{minute:0>2d}",key="Minute")],[sg.Button("OK"),sg.Button("Cancel")],]
    window = sg.Window("Select Time",layout,grab_anywhere=True,keep_on_top=True,modal=True,finalize=True)
    hour_element,minute_element = window['Hour'],window['Minute']
    hour_element.bind("<MouseWheel>","_Wheel")
    minute_element.bind("<MouseWheel>","_Wheel")

    while True:
        event,values = window.read()
        if event in (sg.WINDOW_CLOSED,"Cancel"):
            window.close()
            return (None,None)
        elif event == "OK":
            window.close()
            return (hour,minute)
        elif event == "Hour_Wheel":
            delta = -int(hour_element.user_bind_event.delta/120)
            hour = (hour+delta) % 24
            hour_element.update(f'{hour:0>2d}')
        elif event == "Minute_Wheel":
            delta = -int(minute_element.user_bind_event.delta/120)
            minute = (minute+delta) % 60
            minute_element.update(f'{minute:0>2d}')

sg.theme("DarkBlue")

layout = [
    [sg.Button('Date')],]
window = sg.Window('Title',finalize=True)

while True:
    event,values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Date':
        hour,minute = popup_get_time(5,20)
        print(f'You set time as {hour:0>2d}:{minute:0>2d}')

window.close()