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

如何解决如何在 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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?