【原创python】桌面快捷菜单,远离满桌面的快捷方式
好久没有发帖了,把自己最近捣鼓的东西发一下因为电脑上面安装了许多的软件,所以桌面也很杂乱,特别的不美观
于是就自己写了一个快捷菜单程序,一个能顶35个快捷方式(大神勿喷)
先看下软件主界面(界面有点丑,不要介意^_^)
这是设置界面,可以在这里添加程序(添加之后需要重启程序才能在主界面找到添加的程序)
特别提醒:现在最多只能添加35个程序,多了的话也不会显示
下面就上代码了
#导入库
from tkinter import *
import tkinter.filedialog
import tkinter.messagebox
from tkinter import ttk
import win32api
from PIL import ImageTk as itk
from PIL import Image as Img
import tkinter as tk
class MyMenu():
def __init__(self):
self.programname=[]
self.programpath = []
self.root = Tk()
self.root.geometry('1100x700+200+180')
self.root.resizable(False, False)
windowname = '\u5feb\u6377\u83dc\u5355'
self.root.title(windowname)
tabControl = ttk.Notebook(self.root)
menutab = ttk.Frame(tabControl)
tabControl.add(menutab, text='菜单')
settingtab = ttk.Frame(tabControl)
tabControl.add(settingtab, text='设置')
menupage = ttk.LabelFrame(menutab, text='菜单区', width=1100, height=700)
menupage.place(x=0, y=0)
settingpage = ttk.LabelFrame(settingtab, text='设置区', width=1100, height=700)
settingpage.place(x=0, y=0)
tabControl.pack(expand=1, fill='both')
#设置页面布局
# 设置背景图
bg1 = tk.Canvas(settingpage, width=1100, height=700, bg='white')
self.img1 = itk.PhotoImage(file="settingbg.jpg")
bg1.place(x=0, y=0)
bg1.create_image(0, 0, anchor=NW, image=self.img1)
# 文件路径标签
self.filepathlabel = Label(settingpage, width=90, text='尚未选择文件',font='仿宋', bg='white')
self.filepathlabel.place(x=50, y=30)
self.choicepath=''
# 选择存档按钮按钮
self.choicefilebutton = Button(settingpage, text='选择程序位置', font='仿宋', bg='yellow',command=self.ChoiceFileEvent)
self.choicefilebutton.place(x=300, y=150)
# 读取已选择存档内容按钮
self.readfilebutton = Button(settingpage, text='添加此程序到菜单',command=self.saveselectedpath)
self.readfilebutton.place(x=600, y=150)
#self.readfilebutton.config(state='disable')# 设置按钮不可点击
#菜单页面布局
#菜单背景图片
bg2 = tk.Canvas(menupage, width=1100, height=700, bg='white')
self.img2 = itk.PhotoImage(file="menubg.jpg")
bg2.place(x=0, y=0)
bg2.create_image(0, 0, anchor=NW, image=self.img2)
pathslength = 0
with open('配置文件.ini','rb')as f:
self.paths=f.readlines()
for path in self.paths:
#print('$'+path.strip().decode('gbk')+'$')
if path.strip().decode('gbk')!='':
self.programname.append(path.decode('gbk').split('/')[-1].replace('.exe', '').strip())
self.programpath.append(path.decode('gbk').strip())
pathslength +=1
#print(self.programpath,self.programname)
#创建菜单按钮
menubuttons=[]
x=50
y=50
for i in range(0,35):
#print(self.programpath,self.programname,i)
menubutton=Button(menutab,text='<NULL>',width=15,height=5)
if i%7==0 and i!=0:
y=y+120
x=50
menubutton.config(bg='#FAEBD7')
menubutton.place(x=x,y=y)
x = x+150
menubuttons.append(menubutton)
#print(123,pathslength)
#添加35个按钮的监听事件
if(pathslength>0):
menubuttons.config(command=lambda :self.openprogram(self.programpath),text=self.programname)
if (pathslength > 1):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 2):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 3):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 4):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 5):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 6):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 7):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 8):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 9):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 10):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 11):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 12):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 13):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 14):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 15):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 16):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 17):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 18):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 19):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 20):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 21):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 22):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 23):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 24):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 25):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 26):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 27):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 28):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 29):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 30):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 31):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 32):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 33):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
if (pathslength > 34):
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
def openprogram(self,programpath):
#print('='*40)
#print(programpath)
win32api.ShellExecute(0, 'open', programpath, '', '', 1)
def saveselectedpath(self):
with open('配置文件.ini','a+')as f:
print('#'+self.choicepath.encode('gbk').decode('gbk')+'$')
if self.choicepath.encode('gbk').decode('gbk')!='':
f.write(self.choicepath.encode('utf-8').decode('utf-8')+'\n')
tkinter.messagebox.showinfo('提示', '添加程序成功\n(重启生效)')
else:
tkinter.messagebox.showinfo('提示', '未选择程序\n')
#获取exe文件路径及文件名
def ChoiceFileEvent(self):
filepath=tkinter.filedialog.askopenfilename(filetypes=[('EXE','*.exe')])
if(filepath!=''):
self.filepathlabel.config(text='您选择的文件是:'+filepath,bg='green')
self.choicepath=filepath
self.choicefilebutton.config(bg='green')
self.readfilebutton.config(state='active')
else:
self.filepathlabel.config(text='您没有选择任何文件,请重新选择')
self.choicepath=''
self.filepathlabel.config(bg='red')
self.choicefilebutton.config(bg='red')
self.readfilebutton.config(state='disable',bg='red')
if __name__ == '__main__':
menu=MyMenu()
menu.root.mainloop()
大家有什么建议或者不明白的可以留言告诉我,我看到就会回复的。
对了,软件添加的程序可以在"配置文件.ini"里直接添加修改删除,格式一定要与程序添加的格式一样,而且文件夹下的文件一定不要删,否则程序就打不开了
源码及成品软件下载链接在这里: https://pan.baidu.com/s/1TefUuKip3j3RWn75TeCISA 提取码: aeaa
辛苦了兄dei
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import win32api
from tkinter import *
from tkinter import ttk
from PIL import ImageTk as itk
class MyMenu():
def __init__(self):
self.programname = []
self.programpath = []
self.root = Tk()
self.root.geometry('1100x700+200+180')
self.root.resizable(False, False)
windowname = '\u5feb\u6377\u83dc\u5355'
self.root.title(windowname)
tabControl = ttk.Notebook(self.root)
menutab = ttk.Frame(tabControl)
tabControl.add(menutab, text='菜单')
settingtab = ttk.Frame(tabControl)
tabControl.add(settingtab, text='设置')
menupage = ttk.LabelFrame(menutab, text='菜单区', width=1100, height=700)
menupage.place(x=0, y=0)
settingpage = ttk.LabelFrame(settingtab, text='设置区', width=1100, height=700)
settingpage.place(x=0, y=0)
tabControl.pack(expand=1, fill='both')
# 设置页面布局
# 设置背景图
bg1 = tk.Canvas(settingpage, width=1100, height=700, bg='white')
self.img1 = itk.PhotoImage(file="settingbg.jpg")
bg1.place(x=0, y=0)
bg1.create_image(0, 0, anchor=NW, image=self.img1)
# 文件路径标签
self.filepathlabel = Label(settingpage, width=90, text='尚未选择文件', font='仿宋', bg='white')
self.filepathlabel.place(x=50, y=30)
self.choicepath = ''
# 选择存档按钮按钮
self.choicefilebutton = Button(settingpage, text='选择程序位置', font='仿宋', bg='yellow', command=self.ChoiceFileEvent)
self.choicefilebutton.place(x=300, y=150)
# 读取已选择存档内容按钮
self.readfilebutton = Button(settingpage, text='添加此程序到菜单', command=self.saveselectedpath)
self.readfilebutton.place(x=600, y=150)
# self.readfilebutton.config(state='disable')# 设置按钮不可点击
# 菜单页面布局
# 菜单背景图片
bg2 = tk.Canvas(menupage, width=1100, height=700, bg='white')
self.img2 = itk.PhotoImage(file="menubg.jpg")
bg2.place(x=0, y=0)
bg2.create_image(0, 0, anchor=NW, image=self.img2)
pathslength = 0
with open('配置文件.ini', 'rb')as f:
self.paths = f.readlines()
for path in self.paths:
# print('$'+path.strip().decode('gbk')+'$')
if path.strip().decode('gbk') != '':
self.programname.append(path.decode('gbk').split('/')[-1].replace('.exe', '').strip())
self.programpath.append(path.decode('gbk').strip())
pathslength += 1
# print(self.programpath,self.programname)
# 创建菜单按钮
menubuttons = []
x = 50
y = 50
for i in range(0, 35):
# print(self.programpath,self.programname,i)
menubutton = Button(menutab, text='<NULL>', width=15, height=5)
if i % 7 == 0 and i != 0:
y = y + 120
x = 50
menubutton.config(bg='#FAEBD7')
menubutton.place(x=x, y=y)
x = x + 150
menubuttons.append(menubutton)
for i in range(36):
if pathslength > i:
menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# 添加35个按钮的监听事件
# if pathslength > 0:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 1:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 2:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 3:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 4:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 5:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 6:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 7:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 8:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 9:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 10:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 11:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 12:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 13:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 14:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 15:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 16:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 17:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 18:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 19:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 20:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 21:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 22:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 23:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 24:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 25:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 26:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 27:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 28:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 29:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 30:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 31:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 32:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 33:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
# if pathslength > 34:
# menubuttons.config(command=lambda: self.openprogram(self.programpath), text=self.programname)
def openprogram(self, programpath):
# print('='*40)
# print(programpath)
win32api.ShellExecute(0, 'open', programpath, '', '', 1)
def saveselectedpath(self):
with open('配置文件.ini', 'a+')as f:
print('#' + self.choicepath.encode('gbk').decode('gbk') + '$')
if self.choicepath.encode('gbk').decode('gbk') != '':
f.write(self.choicepath.encode('utf-8').decode('utf-8') + '\n')
tkinter.messagebox.showinfo('提示', '添加程序成功\n(重启生效)')
else:
tkinter.messagebox.showinfo('提示', '未选择程序\n')
# 获取exe文件路径及文件名
def ChoiceFileEvent(self):
filepath = tkinter.filedialog.askopenfilename(filetypes=[('EXE', '*.exe')])
if (filepath != ''):
self.filepathlabel.config(text='您选择的文件是:' + filepath, bg='green')
self.choicepath = filepath
self.choicefilebutton.config(bg='green')
self.readfilebutton.config(state='active')
else:
self.filepathlabel.config(text='您没有选择任何文件,请重新选择')
self.choicepath = ''
self.filepathlabel.config(bg='red')
self.choicefilebutton.config(bg='red')
self.readfilebutton.config(state='disable', bg='red')
if __name__ == '__main__':
menu = MyMenu()
menu.root.mainloop()
本帖最后由 pk8900 于 2019-5-19 18:58 编辑
中间的那一大段监视事件放一个循环里,代码就精简了一半。
#添加35个按钮的监听事件
for x in range(36):
if(pathslength>x):
menubuttons.config(command=lambda :self.openprogram(self.programpath),text=self.programname) 跟着楼主学习! 路过学习了,可视界面弄的漂亮点就更好了。 跟着楼主学习 本帖最后由 Time丨Brand 于 2019-5-19 19:00 编辑
这不就是快捷方式放到软件里了吗。。。
这个工具做工具箱超级方便!
https://www.52pojie.cn/thread-823685-1-1.html
https://www.52pojie.cn/thread-869538-1-1.html Lily3.9.1 快捷启动工具 专为程序员、美工设计、Hacker、办公达人设计【2018.8.14】
https://www.52pojie.cn/thread-724933-1-1.html
(出处: 吾爱破解论坛)
这个也不错 emmm这个界面。。。也太“简约”了吧……