python - 如何使用 tkinter 制作计时器?

标签 python linux tkinter timer

我正在尝试制作一个计时器来关闭我运行 Linux 的计算机。选择我使用spinbox的时间。所以我们的想法是选择旋转框中的时间量,然后添加到命令 sudo shutdown -P + 15 min (例如示例)。

直到现在它才刚刚关闭,我无法通过简单的方法来做到这一点。

timer program

from tkinter import *
import os
import time

def shutdown():
    hrs = spin1.get()
    command1 = ('sudo shutdown -P')
    #sum1 = command1 + hrs
    os.system('sudo shutdown -P') + ('hrs')
    print(os.system)

def cancel():
    command = ('sudo shutdown -c')
    os.system('sudo shutdown -c')
    print(command)



'''def hrs():
    spn1 = spin1.get()
    dsp1 = spn1
    lbltime ['text'] = dsp1'''

entry_width = 2
win = Tk()

spin1 = IntVar()
spn = IntVar()

win.title('SHUTDOWN')
win.geometry('300x250+300+150')
lbl = Label(win, text='SET YOUR SHUTDOWN')
lbl.place(x=80, y=30)


spin1 = Spinbox(win, from_=00, to=23, font=('arial',26,'bold'), width= entry_width, textvariable=spin1)
spin1.insert(0, '00')
spin1.place(x=130, y=60)

setup = Button(win, text='SET TIMER', font=('arial',16,'bold'), command=lambda :shutdown())
setup.place(x=90, y=130)

cnc = Button(win, text='CANCEL', font=('verdana',10, 'bold'),command= lambda :cancel())
cnc.place(x=120, y=180)

win.mainloop()

最佳答案

为了将您的 hrs(用户选择的分钟)添加到您的命令中,您可以使用 + 或使用 将它简单地添加到您现有的字符串命令中format() 正如我在我的解决方案中所展示的那样。此外,当使用 sudo 运行命令时,您需要输入密码,要自动执行此操作,您可以使用 -S 参数,使 sudo 从 STDIN 读取密码,此处为 “mypassword”

def shutdown():
    hrs = spin1.get()
    sd_command = 'echo mypassword | sudo -S shutdown -P +' + hrs # both will work
    sd_command = 'echo mypassword | sudo -S shutdown -P +{}'.format(hrs)
    os.system(command)
    print(command)

def cancel():
    cancel_command = 'echo mypassword | sudo -S shutdown -c'
    os.system(cancel_command)
    print(cancel_command)

如果你想添加关于关机计划的消息,你需要添加另一个标签,这里 shutdown_schedule 将显示 var_schedule 的内容,这是 tkinter 字符串变量,这将是修改用户计划或取消关机的时间。

def shutdown():
    hrs = spin1.get()

    sd_time = time.strftime("%H:%M", time.localtime(time.time() + 60*int(hrs)))
    var_schedule.set('Shutdown scheduled for {}'.format(sd_time))

    sd_command = 'echo mypassword | sudo -S shutdown -P +' + hrs # both will work
    sd_command = 'echo mypassword | sudo -S shutdown -P +{}'.format(hrs)
    os.system(command)
    print(command)

def cancel():
    var_schedule.set('Shutdown not scheduled yet')

    cancel_command = 'echo mypassword | sudo -S shutdown -c'
       os.system(cancel_command)
    print(cancel_command)

var_schedule = StringVar()
var_schedule.set('Shutdown not scheduled yet')

shutdown_schedule = Label(win, textvariable=var_schedule)
shutdown_schedule.place(x=130, y=30)

关于python - 如何使用 tkinter 制作计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54353532/

相关文章:

python - 用自定义激活替换 sigmoid 激活

python - 寻找附近 friend 的算法?

用于替换标签的 Python Tkinter 按钮

python - 将列表乘以列表的所有元素,除了它自己的索引

python - 如何使用 pandas 计算四分位数前滚?

linux - 在 shell 脚本中使用 for 循环遍历参数列表?

linux - 由于访问被拒绝,无法将 CSV 复制到数据库表

linux - assembly - 指定节的方法

python - 从 tkinter 小部件中启动线程

python-3.x - Python 3 Tkinter Canvas