1 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
為了將您的hrs(由用戶選擇的分鐘數(shù))添加到您的命令中,您可以簡單地將其添加到現(xiàn)有的字符串命令中,使用+或使用format()我在我的解決方案中顯示的方法。此外,在運(yùn)行sudo需要輸入密碼的命令時(shí),要自動(dòng)執(zhí)行此操作,您可以使用 -S 參數(shù)使 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)
如果要添加有關(guān)關(guān)機(jī)計(jì)劃的消息,則需要添加另一個(gè)標(biāo)簽,此處shutdown_schedule將顯示var_scheduletkinter 字符串變量的內(nèi)容,該內(nèi)容將在用戶計(jì)劃或取消關(guān)機(jī)時(shí)進(jìn)行修改。
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)
添加回答
舉報(bào)