[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()