笔者工作业余时间也没什么爱好,社交圈子也小,主要娱乐就是背着自己带电瓶的卖唱音响到住地附近找个人多的位置唱唱KtV。

硬件上点歌就用笔记本电脑,歌曲都是网上下载的mkv格式的含有两个音轨的视频。因此点歌软件成了笔者的需求。

点歌软件需求极简单:

  • 读磁盘上的目录取全部music,双击则调用播放器播放music。
  • 自己常唱的歌曲可以选到自选歌曲列表。
  • 支持按简拼搜索music

之前已经用多种开发工具写过,这次逢学习python的机会用它再写一个python版。

软件界面如下:

双击启动播放器。

就代码量上和用笔者用C#写的相同功能的软件对比下:

C#版软件提供的功能与本例子是完全相同。工作逻辑代码约230行,设计器代码(界面代码,系统自动生成)约210行,还不算汉字转拼音类的代码。

python版界面加工作逻辑代码一起约150行。也因此,笔者在编写python版的music时,明显感觉到python的代码精练浓缩(何况笔者只是初学python几天)。

c#版的相同功能的软件

完整的代码如下(python3.6.1环境下开发):

 from tkinter import *
import tkinter.messagebox as messagebox
from xpinyin import Pinyin
import os
import win32api
import win32con class music():
__musicPlayPath='D:\\Program Files (x86)\\SPlayer\\splayer.exe'
__userMusicDiskFilePath='D:\\usermusic.txt'
__musicDefaultList=[]
musicCurList=[]
__userSelMusicList=[]
def __init__(self,musicPath):
self.__musicPath=musicPath def delUserMusicListItem(self,item):
if item in music.__userSelMusicList:
music.__userSelMusicList.remove(item)
messagebox.showinfo('提示','删除成功!') def addUserMusicListItem(self,item):
if not(item in music.__userSelMusicList):
music.__userSelMusicList.append(item) def readUserMuslicList(self):
return music.__userSelMusicList def getSerachList(self):
return music.musicCurList def setDefaultMuslic(self):
music.__musicDefaultList = self.readMusicFromDisk() def readDefaultMuslic(self):
return music.__musicDefaultList def getMusicListByUserKey(self,searchTxt):
print(searchTxt)
if str(searchTxt).__len__()<1:
music.musicCurList=music.__musicDefaultList[:]
return
music.musicCurList.clear()
p = Pinyin()
for m in music.__musicDefaultList:
py = p.get_initials(m, '')
if (py.upper()).find(searchTxt.upper())>=0:
music.musicCurList.append(m) def saveUserMusicListToDisk(self):
f=open(music.__userMusicDiskFilePath,'w')
tmpAry=music.__userSelMusicList[:]
def addReturn(x):
return x+'\n'
f.writelines(map(addReturn, tmpAry))
f.close() def loadUserMusicListFromDisk(self):
self.ifNotFileExistCreateEmptyFile()
f=open(music.__userMusicDiskFilePath,'r')
music.__userSelMusicList.clear()
tmpAry=f.readlines()
def delReturn(x):
return x[0:len(x)-1]
music.__userSelMusicList=list(map(delReturn, tmpAry))
f.close() def ifNotFileExistCreateEmptyFile(self):
if not(os.path.exists(music.__userMusicDiskFilePath)):
f = open(music.__userMusicDiskFilePath, 'a')
f.close() def readMusicFromDisk(self):
music.__musicDefaultList= [d for d in os.listdir(self.__musicPath) if d.upper().find(".MKV")>=0]
return music.__musicDefaultList def playMusic(self,event):
w = event.widget
index = int(w.curselection()[0])
value = w.get(index)
win32api.ShellExecute(0, 'open', music.__musicPlayPath,
'\"'+ self.__musicPath+value+'\"', '', 1) class gui(music): def __init__(self,winName):
self.winName=winName
self._music__musicPath='D:\\KuGou\\'
self.lbx = StringVar() def addMusicToUserList(self):
index = int(self.listbox1.curselection()[0])
value = self.listbox1.get(index)
print(value)
self.addUserMusicListItem(value)
self.saveUserMusicListToDisk()
messagebox.showinfo('成功','已经添加歌曲 \"'+value+'\"!') def UpdateUserMusicList(self):
self.lbx.set(self.readUserMuslicList()) def searchMusic(self,event):
w = event.widget
txt = w.get()
self.getMusicListByUserKey(txt)
self.lbx.set(self.getSerachList()) def delUserMusic(self):
index = int(self.listbox1.curselection()[0])
value = self.listbox1.get(index)
self.delUserMusicListItem(value)
self.lbx.set(self.readUserMuslicList())
self.saveUserMusicListToDisk() def allmusic(self):
self.lbx.set(self.readDefaultMuslic()) def initForm(self):
self.winName.title("music v1.0 by 刘小勇")
self.winName.geometry('600x480+10+10')
self.winName['bg']="pink"
self.winName.resizable(width=False,height=False) self.btn1=Button(self.winName,text='全部歌曲',bg='lightblue',command=self.allmusic)
self.btn1.grid(row=1,column=1)
self.btn2=Button(self.winName,text='选中的歌曲',bg='lightblue',command=self.UpdateUserMusicList)
self.btn2.grid(row=1,column=2)
self.btn3=Button(self.winName,text='添加到选中的歌曲',bg='lightyellow',command=self.addMusicToUserList)
self.btn3.grid(row=1,column=3)
self.btn4=Button(self.winName,text='删除歌曲',bg='lightyellow',command=self.delUserMusic)
self.btn4.grid(row=1,column=4) self.lab1=Label(self.winName,text='搜索:')
self.lab1.grid(row=1,column=5)
self.txt1=Entry(self.winName, width=20)
self.txt1.bind('<Return>',self.searchMusic)
self.txt1.grid(row=1,column=6) self.scrollbar = Scrollbar()
self.scrollbar.grid(row=2,column=13,rowspan=13,sticky='NS')
self.listbox1=Listbox(self.winName,listvariable=self.lbx,width = 82,height=25,yscrollcommand=self.scrollbar.set)
self.setDefaultMuslic();
self.lbx.set(self.readDefaultMuslic())
self.listbox1.bind('<Double-Button-1>',self.playMusic)
self.listbox1.grid(row=2,column=1,columnspan=12)
self.scrollbar.config(command=self.listbox1.yview)
self.loadUserMusicListFromDisk() tk=Tk()
form=gui(tk)
form.initForm()
tk.mainloop()

在编码过程中研究过的一些知识点汇集如下:

1.  如何在搜索框触发搜索后更新listbox, 这个是利用tkinter的 StringVar()

  参考笔者贴子:Python tkinter 控件更新信息

2.  控件listbox的滚动条

  参考笔者贴子:python tkinter Listbox用法

3. 控件的事件绑定

  参考笔者贴子:python tkinter教程-事件绑定

4. 控件的布局

  参考笔者贴子:python tkinter学习——布局

5. 高阶函数

  参考笔者贴子:python 函数式编程:高阶函数,map/reduce

6. 汉字转拼音

  参考笔者贴子:Python汉字转换成拼音

7. win32模块

  参考笔者贴子:Python中四种运行其他程序的方式

笔者写python程序时的开发ide集成环境是pycharm,在写tkinter代码时,关于控件的属性没有什么有意义的提示。因为去记住控件属性的完整拼写是不容易也是没有意义的事。

这也是写界面代码比较麻烦的地方。

长期依赖visual studio c#的IDE强大的语法提示、拼写补全功能, 已经让笔者手工编码的能力高度退化,对于没有属性提示的编程环境已经无法想像。

原创文章,出处 : http://www.cnblogs.com/hackpig/

Python Tkinter 学习成果:点歌软件music的更多相关文章

  1. 利用python+tkinter开发一个点名软件

    最近上课学生多名字记不住,名册忘记了带,要点名怎么办,好久没有写代码了,于是自己写了个点名软件,记录下吧,第一次接触TK也不是太熟悉,写的不太好,记录下源代码 以后遇到要写桌面软件还是可以耍耍的. t ...

  2. Python Tkinter学习笔记

    介绍 入门实例 显示一个窗口,窗口里面有一个标签,显示文字 import tkinter as tk # 一个顶层窗口的实例(Top Level),也称为根窗口 app = tk.Tk() # 设置窗 ...

  3. Python Tkinter学习(1)——第一个Tkinter程序

    注:本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6885066.html.格式修改未完成. Tkinter资料 Python Wiki, T ...

  4. Python tkinter 学习记录(一) --label 与 button

    最简的形式 from tkinter import * root = Tk() # 创建一个Tk实例 root.wm_title("标题") # 修改标题 root.mainloo ...

  5. Python Tkinter 学习历程 一

    一丶一个简单的程序 from tkinter import * #引入所有类#查看tk版本#tkinter._test() root = Tk(); #对这个类进行实例化 w1 = Label(roo ...

  6. python tkinter学习——tkinter部件1

    tkinter部件 一.Tk() & Label() & Button() 1,Tk() 窗口 用Tk()创建窗口对象: #文件名:test1.py import tkinter as ...

  7. python tkinter学习——布局

    目录 一.pack() 二.grid() 三.place() 四.Frame() 正文 布局 一.pack() pack()有以下几个常用属性: side padx pady ipadx ipady ...

  8. python爬虫学习记录——各种软件/库的安装

    Ubuntu18.04安装python3-pip 1.apt-get update更新源 2,ubuntu18.04默认安装了python3,但是pip没有安装,安装命令:apt install py ...

  9. python GUI学习——Tkinter

    支持python的常见GUI工具包: Tkinter 使用Tk平台 很容易得到 半标准 wxpython 基于wxWindows.跨平台越来越流行 Python Win 只能在Windows上使用 使 ...

随机推荐

  1. MySql:charset和collation的设置

    From: http://www.2cto.com/database/201302/189920.html MySql:charset和collation的设置   charset 和 collati ...

  2. php base64_encode,serialize对于存入数据表中字段的数据处理方案

    A better way to save to Database $toDatabse = base64_encode(serialize($data)); // Save to database $ ...

  3. iOS:PSTCollectionView

    https://github.com/steipete/PSTCollectionView Open Source, 100% API compatible replacement of UIColl ...

  4. GCT之数学公式(代数部分)

    一.代数部分: 1.复数 2.一元二次方程   3.数列 4.排列组合

  5. nodejs包管理工具npm

    用Node.js安装模块 在某个项目中单独安装的时候,npm会下载所有的文件到你项目中的一个叫做node_modules的文件夹内 全局模块会被安装到{prefix}/lib/node_modules ...

  6. 小企业是否能用得上"ITIL"?

    在小型IT部门中,明显存在着迫切的IT管理需求.但目前主流ITSM解决方案的价格.实施周期.复杂程度.对人力资源的占用等使他们难以承受.     浦发机械公司的计算机部经理老张带着十几个员工,经过数年 ...

  7. 如何让form表单在enter键入时不提交

    今天在做我的一个小玩意 在线聊天工具的时候 form表单只有一个text和一个button每当我键入enter的时候就刷新.很是郁闷,直接在form上onsumbit=false.才行. 下面是我查询 ...

  8. 如何在Java 环境下使用 HTTP 协议收发 MQ 消息

    1. 准备环境在工程 POM 文件添加 HTTP Java 客户端的依赖. <dependency> <groupId>org.eclipse.jetty</groupI ...

  9. Java终止循环体

    编写程序,是先创建一个字符串数组,在使用foreach语句遍历时,如果发现数组中包含字符串“老鹰”则立刻中断循环.再创建一个整数类型的二维数组,使用双层foreach语句循环遍历,当发现第一个小于60 ...

  10. 03-Linux各目录及每个目录的详细介绍

    Linux各目录及每个目录的详细介绍 [常见目录说明] 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所 ...