亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 tkinter 制作計時器?

如何使用 tkinter 制作計時器?

大話西游666 2021-10-19 17:19:29
我正在嘗試制作一個計時器來關閉我運行 linux 的計算機。選擇我使用 spinbox 的時間。所以想法是選擇旋轉框中的時間量,然后添加到命令sudo shutdown -P+ 15 分鐘(例如示例)。直到現在它才關閉,我無法用簡單的方法來做到這一點。from tkinter import *import osimport timedef 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 = 2win = 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()
查看完整描述

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)


查看完整回答
反對 回復 2021-10-19
  • 1 回答
  • 0 關注
  • 323 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號