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)
添加回答
舉報