|
| 1 | +import os |
| 2 | +from tkinter import * |
| 3 | +from tkinter import ttk |
| 4 | +import subprocess |
| 5 | +from subprocess import Popen, PIPE, STDOUT |
| 6 | + |
| 7 | +# reference https://stackoverflow.com/a/34466743 |
| 8 | +class CustomButton(Label): |
| 9 | + |
| 10 | + def __init__(self, *args, **kwargs): |
| 11 | + # TODO: Create a Frame for border https://www.geeksforgeeks.org/how-to-change-border-color-in-tkinter-widget/ |
| 12 | + Label.__init__(self) |
| 13 | + self.bind("<Enter>", self.changeBGEnter) |
| 14 | + self.bind("<Leave>", self.changeBGLeave) |
| 15 | + self.bind("<Button-1>",kwargs.get('handleClick',self.handleClickDefault) ) |
| 16 | + |
| 17 | + self.background = kwargs.get('background',"#1890ff") |
| 18 | + self.hoverBackground = kwargs.get('hoverBackground',"#40a9ff") |
| 19 | + self.foreground = kwargs.get('foreground',"#fff") |
| 20 | + self.fontsize = kwargs.get('fontsize',14) |
| 21 | + self.fontfamily = kwargs.get('fontfamily',"微软雅黑") |
| 22 | + self.text = kwargs.get('text',"Button") |
| 23 | + self.width = kwargs.get('width',30) |
| 24 | + self.padx = kwargs.get('padx',10) |
| 25 | + self.pady = kwargs.get('pady',10) |
| 26 | + self.relief = kwargs.get('relief','solid') |
| 27 | + self.borderwidth = kwargs.get('borderwidth',1) |
| 28 | + self.anchor = kwargs.get('anchor','center') |
| 29 | + self.justify = kwargs.get('justify','center') |
| 30 | + |
| 31 | + self.config( |
| 32 | + anchor=self.anchor, # Where to anchor the text in the widget |
| 33 | + justify=self.justify, |
| 34 | + background=self.background, |
| 35 | + foreground=self.foreground, |
| 36 | + font=(self.fontfamily, self.fontsize), |
| 37 | + text=self.text, |
| 38 | + relief=self.relief, |
| 39 | + width=self.width, |
| 40 | + padx=self.padx, |
| 41 | + pady=self.pady, |
| 42 | + borderwidth=self.borderwidth |
| 43 | + ) |
| 44 | + |
| 45 | + def changeBGEnter(self,event): |
| 46 | + self.config(background=self.hoverBackground,foreground=self.foreground) |
| 47 | + |
| 48 | + def changeBGLeave(self, event): |
| 49 | + self.config(background=self.background,foreground=self.foreground) |
| 50 | + |
| 51 | + def handleClickDefault(self, event): |
| 52 | + print('click') |
| 53 | + |
| 54 | +class Win: |
| 55 | + def __init__(self,root): |
| 56 | + # path |
| 57 | + dirname = os.path.dirname(__file__) |
| 58 | + self.classic = os.path.join(dirname, 'bat/classic.bat') |
| 59 | + self.default = os.path.join(dirname, 'bat/default.bat') |
| 60 | + |
| 61 | + |
| 62 | + # set title |
| 63 | + root.title("Windows 11 Menu Editor") |
| 64 | + |
| 65 | + # screen width |
| 66 | + sw = root.winfo_screenwidth() |
| 67 | + # screen height |
| 68 | + sh = root.winfo_screenheight() |
| 69 | + # frame width |
| 70 | + ww = 380 |
| 71 | + # frame height |
| 72 | + wh = 180 |
| 73 | + x = (sw-ww) / 2 |
| 74 | + y = (sh-wh) / 2 - 20 |
| 75 | + |
| 76 | + root.geometry("%dx%d+%d+%d" %(ww,wh,x,y)) |
| 77 | + root.resizable(False, False) |
| 78 | + root.config(background="white") |
| 79 | + |
| 80 | + # create content frame |
| 81 | + mainFrame = ttk.Frame(root,padding='20 20 20 20') |
| 82 | + mainFrame.grid(column=0,row=0,sticky=(N,W,E,S)) |
| 83 | + # The columnconfigure/rowconfigure bits tell Tk that the frame should expand to fill any extra space if the window is resized. |
| 84 | + root.columnconfigure(0,weight=1) |
| 85 | + root.rowconfigure(0,weight=1) |
| 86 | + |
| 87 | + # classic button |
| 88 | + classicButton = CustomButton(root, text="Change to Win10 Classic Menu",handleClick=self.useClassic) |
| 89 | + classicButton.grid(column=0, row=0, columnspan=2, sticky=(N, W), padx=5,pady=20) |
| 90 | + |
| 91 | + # default button |
| 92 | + defaultButton = CustomButton(root, text="Change to Win11 Default Menu",foreground='#1a1a1a',hoverBackground="#ede9e5",background='#fcfbfa',handleClick=self.useDefault) |
| 93 | + defaultButton.grid(column=0, row=1, columnspan=2, sticky=(N, W), padx=5,pady=20) |
| 94 | + # exec bat |
| 95 | + def useClassic(self,*args): |
| 96 | + |
| 97 | + # os.system(self.classic) |
| 98 | + subprocess.Popen(self.classic) |
| 99 | + |
| 100 | + # not working |
| 101 | + # process = Popen(["cmd"], shell=False, stdout=PIPE, stdin=PIPE, stderr=STDOUT) |
| 102 | + |
| 103 | + # commands = ("reg.exe delete 'HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}' /f\n" |
| 104 | + # "taskkill /IM explorer.exe /F\n" |
| 105 | + # "explorer\n" |
| 106 | + # ) |
| 107 | + # outs, errs = process.communicate(commands.encode("gbk")) |
| 108 | + # content = [z.strip() for z in outs.decode("gbk").split("\n") if z] |
| 109 | + # print(*content,sep="\n") |
| 110 | + |
| 111 | + def useDefault(self,*args): |
| 112 | + |
| 113 | + # os.system(self.default) |
| 114 | + subprocess.Popen(self.default) |
| 115 | + |
| 116 | +# sets up the main application window |
| 117 | +root = Tk() |
| 118 | +# init function |
| 119 | +Win(root) |
| 120 | +# necessary for everything to appear onscreen and allow users to interact with it. |
| 121 | +root.mainloop() |
0 commit comments