首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建文本框以输入特定事件在Tkinter和Python中发生的时间

创建文本框以输入特定事件在Tkinter和Python中发生的时间
EN

Stack Overflow用户
提问于 2017-08-30 21:15:14
回答 1查看 109关注 0票数 0

我不确定如何更好地表达标题问题,但我真正想做的是能够在Tkinter中的小部件中输入时间,以便在该时间运行Python脚本。想象一个可编程的咖啡机,这正是我想要创建的功能。

下面是简单地打开和关闭某些东西的基本代码(而不是程序本身)。如何将其编码为在可在GUI中输入的设定时间打开?

我试过在谷歌上搜索,但不确定我的问题是否用对了,对不起,我是个菜鸟。

代码语言:javascript
复制
from tkinter import *
import tkinter.font
from gpiozero import LED
import RPi.GPIO
import time

RPi.GPIO.setmode(RPi.GPIO.BCM)

led=LED(4)

win = Tk()
win.title("LED Toggler")
myFont = tkinter.font.Font(family = 'Helvetica', size = 12, weight = 
"bold")

def ledToggle():
    if led.is_lit:
        led.off()

    else:       
        led.on()        
        time.sleep(3)
        led.off() 

def close():
    RPi.GPIO.cleanup()
    win.destroy()

ledButton = Button(win, text='GPIO ON', font=myFont,  
command=ledToggle, fg='black', bg='green', height=1, width=10)
ledButton.grid(row=1,column=1)

exitButton = Button(win, text='Exit', font=myFont, command=close, 
bg='red', height=1, width=6)
exitButton.grid(row=2, column=1)

win.protocol("WM_DELETE_WINDOW", close)

win.mainloop() 
EN

回答 1

Stack Overflow用户

发布于 2017-08-30 21:57:47

代码语言:javascript
复制
import time
# you can input a string like this in the gui 
gui_time = 'Wed Aug 30 18:20:01 2017'

# this gives you the number of seconds from now until the time you specified
ETA = time.mktime(time.strptime(gui_time))-time.time()
# convert to milliseconds 
mETA = int(ETA*1000)
win.after(mETA,ledToggle)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45961498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档