吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4452|回复: 22
收起左侧

[Python 原创] 【原创python】桌面快捷菜单,远离满桌面的快捷方式

[复制链接]
袅袅系秋风 发表于 2019-5-19 17:54
好久没有发帖了,把自己最近捣鼓的东西发一下

因为电脑上面安装了许多的软件,所以桌面也很杂乱,特别的不美观


于是就自己写了一个快捷菜单程序,一个能顶35个快捷方式(大神勿喷)

先看下软件主界面(界面有点丑,不要介意^_^)

1.png

这是设置界面,可以在这里添加程序(添加之后需要重启程序才能在主界面找到添加的程序)

2.png

特别提醒:现在最多只能添加35个程序,多了的话也不会显示



下面就上代码了

[Python] 纯文本查看 复制代码
[/size][/font]
[font=宋体][size=6]#导入库[/size][/font]
[font=宋体][size=6]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[/size][/font]


[font=宋体][size=6]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[i],self.programname[i],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[0].config(command=lambda :self.openprogram(self.programpath[0]),text=self.programname[0])
        if (pathslength > 1):
            menubuttons[1].config(command=lambda: self.openprogram(self.programpath[1]), text=self.programname[1])
        if (pathslength > 2):
            menubuttons[2].config(command=lambda: self.openprogram(self.programpath[2]), text=self.programname[2])
        if (pathslength > 3):
            menubuttons[3].config(command=lambda: self.openprogram(self.programpath[3]), text=self.programname[3])
        if (pathslength > 4):
            menubuttons[4].config(command=lambda: self.openprogram(self.programpath[4]), text=self.programname[4])

        if (pathslength > 5):
            menubuttons[5].config(command=lambda: self.openprogram(self.programpath[5]), text=self.programname[5])
        if (pathslength > 6):
            menubuttons[6].config(command=lambda: self.openprogram(self.programpath[6]), text=self.programname[6])
        if (pathslength > 7):
            menubuttons[7].config(command=lambda: self.openprogram(self.programpath[7]), text=self.programname[7])
        if (pathslength > 8):
            menubuttons[8].config(command=lambda: self.openprogram(self.programpath[8]), text=self.programname[8])
        if (pathslength > 9):
            menubuttons[9].config(command=lambda: self.openprogram(self.programpath[9]), text=self.programname[9])
        if (pathslength > 10):
            menubuttons[10].config(command=lambda: self.openprogram(self.programpath[10]), text=self.programname[10])
        if (pathslength > 11):
            menubuttons[11].config(command=lambda: self.openprogram(self.programpath[11]), text=self.programname[11])
        if (pathslength > 12):
            menubuttons[12].config(command=lambda: self.openprogram(self.programpath[12]), text=self.programname[12])
        if (pathslength > 13):
            menubuttons[13].config(command=lambda: self.openprogram(self.programpath[13]), text=self.programname[13])
        if (pathslength > 14):
            menubuttons[14].config(command=lambda: self.openprogram(self.programpath[14]), text=self.programname[14])
        if (pathslength > 15):
            menubuttons[15].config(command=lambda: self.openprogram(self.programpath[15]), text=self.programname[15])
        if (pathslength > 16):
            menubuttons[16].config(command=lambda: self.openprogram(self.programpath[16]), text=self.programname[16])
        if (pathslength > 17):
            menubuttons[17].config(command=lambda: self.openprogram(self.programpath[17]), text=self.programname[17])
        if (pathslength > 18):
            menubuttons[18].config(command=lambda: self.openprogram(self.programpath[18]), text=self.programname[18])
        if (pathslength > 19):
            menubuttons[19].config(command=lambda: self.openprogram(self.programpath[19]), text=self.programname[19])
        if (pathslength > 20):
            menubuttons[20].config(command=lambda: self.openprogram(self.programpath[20]), text=self.programname[20])
        if (pathslength > 21):
            menubuttons[21].config(command=lambda: self.openprogram(self.programpath[21]), text=self.programname[21])
        if (pathslength > 22):
            menubuttons[22].config(command=lambda: self.openprogram(self.programpath[22]), text=self.programname[22])
        if (pathslength > 23):
            menubuttons[23].config(command=lambda: self.openprogram(self.programpath[23]), text=self.programname[23])
        if (pathslength > 24):
            menubuttons[24].config(command=lambda: self.openprogram(self.programpath[24]), text=self.programname[24])
        if (pathslength > 25):
            menubuttons[25].config(command=lambda: self.openprogram(self.programpath[25]), text=self.programname[25])
        if (pathslength > 26):
            menubuttons[26].config(command=lambda: self.openprogram(self.programpath[26]), text=self.programname[26])
        if (pathslength > 27):
            menubuttons[27].config(command=lambda: self.openprogram(self.programpath[27]), text=self.programname[27])
        if (pathslength > 28):
            menubuttons[28].config(command=lambda: self.openprogram(self.programpath[28]), text=self.programname[28])
        if (pathslength > 29):
            menubuttons[29].config(command=lambda: self.openprogram(self.programpath[29]), text=self.programname[29])
        if (pathslength > 30):
            menubuttons[30].config(command=lambda: self.openprogram(self.programpath[30]), text=self.programname[30])
        if (pathslength > 31):
            menubuttons[31].config(command=lambda: self.openprogram(self.programpath[31]), text=self.programname[31])
        if (pathslength > 32):
            menubuttons[32].config(command=lambda: self.openprogram(self.programpath[32]), text=self.programname[32])
        if (pathslength > 33):
            menubuttons[33].config(command=lambda: self.openprogram(self.programpath[33]), text=self.programname[33])
        if (pathslength > 34):
            menubuttons[34].config(command=lambda: self.openprogram(self.programpath[34]), text=self.programname[34])

    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

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

liangyun 发表于 2019-5-20 09:31

辛苦了兄dei

[Python] 纯文本查看 复制代码
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[i],self.programname[i],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[i].config(command=lambda: self.openprogram(self.programpath[i]), text=self.programname[i])
        # 添加35个按钮的监听事件
        # if pathslength > 0:
        #     menubuttons[0].config(command=lambda: self.openprogram(self.programpath[0]), text=self.programname[0])
        # if pathslength > 1:
        #     menubuttons[1].config(command=lambda: self.openprogram(self.programpath[1]), text=self.programname[1])
        # if pathslength > 2:
        #     menubuttons[2].config(command=lambda: self.openprogram(self.programpath[2]), text=self.programname[2])
        # if pathslength > 3:
        #     menubuttons[3].config(command=lambda: self.openprogram(self.programpath[3]), text=self.programname[3])
        # if pathslength > 4:
        #     menubuttons[4].config(command=lambda: self.openprogram(self.programpath[4]), text=self.programname[4])
        # if pathslength > 5:
        #     menubuttons[5].config(command=lambda: self.openprogram(self.programpath[5]), text=self.programname[5])
        # if pathslength > 6:
        #     menubuttons[6].config(command=lambda: self.openprogram(self.programpath[6]), text=self.programname[6])
        # if pathslength > 7:
        #     menubuttons[7].config(command=lambda: self.openprogram(self.programpath[7]), text=self.programname[7])
        # if pathslength > 8:
        #     menubuttons[8].config(command=lambda: self.openprogram(self.programpath[8]), text=self.programname[8])
        # if pathslength > 9:
        #     menubuttons[9].config(command=lambda: self.openprogram(self.programpath[9]), text=self.programname[9])
        # if pathslength > 10:
        #     menubuttons[10].config(command=lambda: self.openprogram(self.programpath[10]), text=self.programname[10])
        # if pathslength > 11:
        #     menubuttons[11].config(command=lambda: self.openprogram(self.programpath[11]), text=self.programname[11])
        # if pathslength > 12:
        #     menubuttons[12].config(command=lambda: self.openprogram(self.programpath[12]), text=self.programname[12])
        # if pathslength > 13:
        #     menubuttons[13].config(command=lambda: self.openprogram(self.programpath[13]), text=self.programname[13])
        # if pathslength > 14:
        #     menubuttons[14].config(command=lambda: self.openprogram(self.programpath[14]), text=self.programname[14])
        # if pathslength > 15:
        #     menubuttons[15].config(command=lambda: self.openprogram(self.programpath[15]), text=self.programname[15])
        # if pathslength > 16:
        #     menubuttons[16].config(command=lambda: self.openprogram(self.programpath[16]), text=self.programname[16])
        # if pathslength > 17:
        #     menubuttons[17].config(command=lambda: self.openprogram(self.programpath[17]), text=self.programname[17])
        # if pathslength > 18:
        #     menubuttons[18].config(command=lambda: self.openprogram(self.programpath[18]), text=self.programname[18])
        # if pathslength > 19:
        #     menubuttons[19].config(command=lambda: self.openprogram(self.programpath[19]), text=self.programname[19])
        # if pathslength > 20:
        #     menubuttons[20].config(command=lambda: self.openprogram(self.programpath[20]), text=self.programname[20])
        # if pathslength > 21:
        #     menubuttons[21].config(command=lambda: self.openprogram(self.programpath[21]), text=self.programname[21])
        # if pathslength > 22:
        #     menubuttons[22].config(command=lambda: self.openprogram(self.programpath[22]), text=self.programname[22])
        # if pathslength > 23:
        #     menubuttons[23].config(command=lambda: self.openprogram(self.programpath[23]), text=self.programname[23])
        # if pathslength > 24:
        #     menubuttons[24].config(command=lambda: self.openprogram(self.programpath[24]), text=self.programname[24])
        # if pathslength > 25:
        #     menubuttons[25].config(command=lambda: self.openprogram(self.programpath[25]), text=self.programname[25])
        # if pathslength > 26:
        #     menubuttons[26].config(command=lambda: self.openprogram(self.programpath[26]), text=self.programname[26])
        # if pathslength > 27:
        #     menubuttons[27].config(command=lambda: self.openprogram(self.programpath[27]), text=self.programname[27])
        # if pathslength > 28:
        #     menubuttons[28].config(command=lambda: self.openprogram(self.programpath[28]), text=self.programname[28])
        # if pathslength > 29:
        #     menubuttons[29].config(command=lambda: self.openprogram(self.programpath[29]), text=self.programname[29])
        # if pathslength > 30:
        #     menubuttons[30].config(command=lambda: self.openprogram(self.programpath[30]), text=self.programname[30])
        # if pathslength > 31:
        #     menubuttons[31].config(command=lambda: self.openprogram(self.programpath[31]), text=self.programname[31])
        # if pathslength > 32:
        #     menubuttons[32].config(command=lambda: self.openprogram(self.programpath[32]), text=self.programname[32])
        # if pathslength > 33:
        #     menubuttons[33].config(command=lambda: self.openprogram(self.programpath[33]), text=self.programname[33])
        # if pathslength > 34:
        #     menubuttons[34].config(command=lambda: self.openprogram(self.programpath[34]), text=self.programname[34])

    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:54
本帖最后由 pk8900 于 2019-5-19 18:58 编辑

中间的那一大段监视事件放一个循环里,代码就精简了一半。
[Python] 纯文本查看 复制代码
#添加35个按钮的监听事件
for x in range(36):
     if(pathslength>x):
          menubuttons[x].config(command=lambda :self.openprogram(self.programpath[x]),text=self.programname[x])
一叶知夏 发表于 2019-5-19 17:58
头像被屏蔽
imtaizi888 发表于 2019-5-19 18:19
提示: 作者被禁止或删除 内容自动屏蔽
cao_jf 发表于 2019-5-19 18:30
路过学习了,可视界面弄的漂亮点就更好了。
hui00000 发表于 2019-5-19 18:30
跟着楼主学习
Time丨Brand 发表于 2019-5-19 18:45
本帖最后由 Time丨Brand 于 2019-5-19 19:00 编辑

这不就是快捷方式放到软件里了吗。。。
Time丨Brand 发表于 2019-5-19 19:00
Time丨Brand 发表于 2019-5-19 19:02
Lily3.9.1 快捷启动工具 专为程序员、美工设计、Hacker、办公达人设计【2018.8.14】
https://www.52pojie.cn/thread-724933-1-1.html
(出处: 吾爱破解论坛)
这个也不错
愿他们彼此珍惜 发表于 2019-5-19 19:17
emmm这个界面。。。也太“简约”了吧……
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-16 10:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表