自助式自动重启测试
import os,sys,shutil,webbrowser import tkinter as tk from tkinter import scrolledtext,END,Y,LEFT,BOTH,messagebox sys.path.append("..") class Video(object): def __init__(self,master): self.master = master self.adict={} self.bdict={} self.initWidgets() def initWidgets(self): #菜单点击搜索结果复制到剪切板到浏览器 menubar = tk.Menu(self.master) filemenu = tk.Menu(menubar,tearoff=0) filemenu_1 = tk.Menu(menubar,tearoff=0) menubar.add_cascade(label='关于',menu=filemenu) filemenu.add_command(label='Xcsg ver1.0') menubar.add_cascade(label='帮助',menu=filemenu_1) filemenu_1.add_command(label='操作',command=self.runing) self.master.config(menu = menubar) tk.Label(self.master,text='请输入重启次数:',font=('Arial,12'),width=15,height=2).place(x=0,y=10) #结果窗口#布局窗口 frm=tk.Frame(self.master) frm.place(x=5,y=80) frm_1 = tk.Frame(frm) frm_2 = tk.Frame(frm) frm_1.pack(side='left') frm_2.pack(side='right') t_x = tk.Scrollbar(frm) self.rest = tk.Listbox(frm,height=15,width=88,yscrollcommand=t_x.set) # rest.place(x=10,y=80) t_x.config(command=self.rest.yview) ret = tk.StringVar() self.e = tk.Entry(self.master,show=None,textvariable=ret,width=35) self.e.place(x=120,y=20) lable = tk.Label(self.master,text='人生苦短,及时行乐').place(x=480,y=75) tk.Label(self.master,text='技术博客 https://www.cnblogs.com/xcsg').place(x=3,y=75) tn = tk.Button(self.master,text='技术博客 https://www.cnblogs.com/xcsg',width=35,height=0,command=self.info_url_).place(x=3,y=75) tn = tk.Button(self.master,text='开始',width=20,height=1,command=self.crawl).place(x=450,y=14) def runing(self): messagebox.showinfo(title='操作', message='输入次数即可自动重启,此软件仅限重启测试,如有纠纷概不负责') #浏览器跳转 def info_url_(self,event='<Double-Button-1>') -> str: webbrowser.open('https://www.cnblogs.com/xcsg') def crawl(self): name = self.e.get() if os.path.exists('C:/ProgramData/config.txt'): self.fun() else: with open('C:/ProgramData/config.txt', 'w') as f: f.write('1,%s'%name) self.fun() os.system("shutdown /r /t 0") def fun(self): ff = open('C:/ProgramData/config.txt',encoding='utf-8') m = ff.read() ff.close() num = m.split(',') if num[0] == num[1]: sys.exit() s = int(num[0])+1 hh =str(s)+","+num[1] with open('C:/ProgramData/config.txt', 'w') as f: f.write(hh) os.system("shutdown /r /t 0") if __name__ == '__main__': traget = 'C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Startup' a = "shutd.exe" if os.path.exists('C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Startup/shutd.exe'): ff = open('C:/ProgramData/config.txt',encoding='utf-8') m = ff.read() ff.close() num = m.split(',') if num[0] == num[1]: sys.exit() s = int(num[0])+1 hh =str(s)+","+num[1] with open('C:/ProgramData/config.txt', 'w') as f: f.write(hh) os.system("shutdown /r /t 0") else: shutil.copy(a,os.path.join(traget, a)) window = tk.Tk() window.title('自助式自动重启测试') # window.geometry("650x400") Video(window) window.resizable(width=False, height=False) window.update_idletasks() # width, height = (window.winfo_width(), window.winfo_height()) width, height = (650,100) screenwidth, screenheight = (window.winfo_screenwidth(), window.winfo_screenheight()) size_loc = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2-30) window.geometry(size_loc) window.mainloop()