最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python編寫電腦定時(shí)關(guān)機(jī)小程序

 更新時(shí)間:2024年01月14日 17:00:18   作者:wokaoyan1981  
這篇文章主要為大家詳細(xì)介紹了如何使用Python編寫電腦定時(shí)關(guān)機(jī)小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

這是一個(gè)Python應(yīng)用。家里臥室裝了新電視,HDMI連接筆記本追劇還是很愉快的??墒墙?jīng)常睡著,自然忘了關(guān)機(jī)。搜了一大圈,都是用命令行或者bat解決。商店里的應(yīng)用也不好用,有些還收費(fèi)。于是萌生了自己寫一個(gè)定時(shí)關(guān)機(jī)應(yīng)用的想法。利用Notebook實(shí)現(xiàn)“默認(rèn)模式”和“自定義模式”選項(xiàng)卡,如圖所示。最后一張圖是素材。

示例代碼

import datetime
import tkinter as tk
from tkinter import ttk
from threading import Thread
import time
import os
 
 
class ShutdownApp:
    def __init__(self, root):
        self.time_left = None
        self.root = root
        self.root.title("定時(shí)關(guān)機(jī)應(yīng)用")
        self.root.resizable(0, 0)
        screenwidth = self.root.winfo_screenwidth()
        screenheight = self.root.winfo_screenheight()
        width = 600
        height = 200
        size_geo = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.root.geometry(size_geo)
        self.root.iconphoto(False, tk.PhotoImage(file='C:/Users/wokao/Desktop/icon.png'))
        self.root["background"] = "#8DB6CD"
 
        self.notebook = tk.ttk.Notebook(self.root)
        self.framework1 = tk.Frame()
        self.framework2 = tk.Frame()
        self.notebook.add(self.framework1, text='默認(rèn)模式')
        self.notebook.add(self.framework2, text='自定義模式')
        self.notebook.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
 
        # 選項(xiàng)卡1的界面
        tk.Label(self.framework1, text="選擇關(guān)機(jī)時(shí)長(zhǎng):").pack()
        cbox = ttk.Combobox(self.framework1)
        cbox['value'] = ('0.5小時(shí)', '1小時(shí)', '1.5小時(shí)', '2小時(shí)')
        cbox.current(1)
        self.selected_value = cbox.get()
        cbox.pack()
        self.start_button = tk.Button(self.framework1, text="開始", command=self.start_timer)
        self.start_button.pack()
        self.cancel_button = tk.Button(self.framework1, text="取消關(guān)機(jī)", state='disabled', command=self.cancel_timer)
        self.cancel_button.pack()
        self.timer_label = tk.Label(self.framework1, text="", bg="#8DB6CD")
        self.timer_label.pack()
 
        # 選項(xiàng)卡2的界面
        tk.Label(self.framework2, text="輸入關(guān)機(jī)時(shí)長(zhǎng)(秒):").pack()
        self.time_entry2 = tk.Entry(self.framework2)
        self.time_left2 = self.time_entry2.get()
        self.time_entry2.pack()
        self.start_button2 = tk.Button(self.framework2, text="開始", command=self.start_timer2)
        self.start_button2.pack()
        self.cancel_button2 = tk.Button(self.framework2, text="取消關(guān)機(jī)", state='disabled', command=self.cancel_timer2)
        self.cancel_button2.pack()
        self.timer_label2 = tk.Label(self.framework2, text="", bg="#8DB6CD")
        self.timer_label2.pack()
 
        self.timer_thread = None
        self.running = False
 
    # 選項(xiàng)卡1的功能實(shí)現(xiàn)
    def selected_time(self, selected_value):
        match selected_value:
            case '0.5小時(shí)':
                self.time_left = 1800
            case '1小時(shí)':
                self.time_left = 3600
            case '1.5小時(shí)':
                self.time_left = 5400
            case '2小時(shí)':
                self.time_left = 7200
 
    def start_timer(self):
        try:
            self.selected_time(self.selected_value)
        except ValueError:
            self.timer_label.config(text="請(qǐng)選擇關(guān)機(jī)倒計(jì)時(shí)時(shí)長(zhǎng)!")
            return
 
        self.notebook.tab(1, state='disabled')
        self.running = True
        self.start_button.config(state='disabled')
        self.cancel_button.config(state='normal')
        self.timer_thread = Thread(target=self.run_timer)
        self.timer_thread.start()
 
    def run_timer(self):
        while self.time_left > 0 and self.running:
            timer = str(datetime.timedelta(seconds=int(self.time_left)))
            self.timer_label.config(text=f"關(guān)機(jī)倒計(jì)時(shí): {timer} ", font=("黑體", 45), fg="white", bg="#8DB6CD")
            time.sleep(1)
            self.time_left -= 1
 
        self.timer_label.config(text="")
        if self.running:
            os.system("shutdown /s /t 1")  # 在Windows上執(zhí)行關(guān)機(jī)命令
 
    def cancel_timer(self):
        self.running = False
        self.start_button.config(state='normal')
        self.cancel_button.config(state='disabled')
        self.timer_label.config(text="已取消關(guān)機(jī)")
        self.notebook.tab(1, state='normal')
 
    # 選項(xiàng)卡2的功能實(shí)現(xiàn)
    def start_timer2(self):
        try:
            self.time_left2 = int(self.time_entry2.get())
        except ValueError:
            self.timer_label2.config(text="請(qǐng)輸入有效的數(shù)字!")
            return
 
        self.notebook.tab(0, state='disabled')
        self.running = True
        self.start_button2.config(state='disabled')
        self.cancel_button2.config(state='normal')
        self.timer_thread = Thread(target=self.run_timer2)
        self.timer_thread.start()
 
    def run_timer2(self):
        while self.time_left2 > 0 and self.running:
            self.timer_label2.config(text=f"關(guān)機(jī)倒計(jì)時(shí): {self.time_left2} 秒", font=("黑體", 45),fg="white", bg="#8DB6CD")
            time.sleep(1)
            self.time_left2 -= 1
 
        self.timer_label2.config(text="")
        if self.running:
            os.system("shutdown /s /t 1")  # 在Windows上執(zhí)行關(guān)機(jī)命令
 
    def cancel_timer2(self):
        self.running = False
        self.start_button2.config(state='normal')
        self.cancel_button2.config(state='disabled')
        self.timer_label2.config(text="已取消關(guān)機(jī)")
        self.notebook.tab(0, state='normal')
 
 
if __name__ == "__main__":
    ui = tk.Tk()
    app = ShutdownApp(ui)
    ui.mainloop()

到此這篇關(guān)于使用Python編寫電腦定時(shí)關(guān)機(jī)小程序的文章就介紹到這了,更多相關(guān)Python定時(shí)關(guān)機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python批量清洗Excel數(shù)據(jù)的操作指南(去重+補(bǔ)缺失值+可視化)

    Python批量清洗Excel數(shù)據(jù)的操作指南(去重+補(bǔ)缺失值+可視化)

    日常辦公或入門數(shù)據(jù)分析時(shí),常遇到Excel數(shù)據(jù)雜亂(重復(fù)值、缺失值、格式混亂),手動(dòng)處理耗時(shí);本文用Python批量搞定清洗+可視化,10行代碼解決重復(fù)工作,0基礎(chǔ)也能會(huì),需要的朋友可以參考下
    2025-12-12
  • Jupyter Notebook 基本操作快捷鍵方式

    Jupyter Notebook 基本操作快捷鍵方式

    這篇文章主要介紹了Jupyter Notebook 基本操作快捷鍵方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 解決pycharm中的run和debug失效無法點(diǎn)擊運(yùn)行

    解決pycharm中的run和debug失效無法點(diǎn)擊運(yùn)行

    這篇文章主要介紹了解決pycharm中的run和debug失效無法點(diǎn)擊運(yùn)行方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 最新評(píng)論

    邛崃市| 濉溪县| 原平市| 长乐市| 星子县| 八宿县| 望城县| 阿合奇县| 临夏县| 石渠县| 阜新| 长沙市| 池州市| 东源县| 朔州市| 娱乐| 柳河县| 万全县| 台中市| 金秀| 庄浪县| 长武县| 六枝特区| 永德县| 安丘市| 唐海县| 兴业县| 大连市| 台北县| 哈尔滨市| 和平区| 云安县| 静乐县| 韶关市| 鸡泽县| 分宜县| 绵阳市| 陕西省| 铁岭县| 乌拉特中旗| 洞口县|