1 回答
TA贡献1772条经验 获得超8个赞
为了将您的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_scheduletkinter 字符串变量的内容,该内容将在用户计划或取消关机时进行修改。
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)
添加回答
举报