我正在试着做一个计时器,关闭我运行linux的电脑。选择使用spinbox的时间。因此,我们的想法是在spinbox中选择时间量,然后添加到命令sudo shutdown -P
+15min(例如)。
到目前为止,它只是关闭,我不能一个简单的方法来做到这一点。你知道吗
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"
。你知道吗如果您想添加关于关机计划的消息,您需要添加另一个标签,这里
shutdown_schedule
,它将显示var_schedule
的内容,这是tkinter字符串变量,当用户计划或取消关机时,它将被修改。你知道吗相关问题 更多 >
编程相关推荐