精华内容
下载资源
问答
  • (最近事情比较,今天就到这里,得忙别的事情了) import tkinter as tk import tkinter.messagebox as messagebox #初始化窗口 window = tk.Tk() #窗口名称 window.title("我的第一个窗口") #窗口大小,是 x ...

    今天简单接触学习了一下,很简单,不过代码不是很优,以后有需要再补充修正吧。(最近事情比较多,今天就到这里,得忙别的事情了)

    import tkinter as tk
    import tkinter.messagebox as messagebox
    #初始化窗口
    window = tk.Tk()
    #窗口名称
    window.title("我写的第一个窗口")
    #窗口大小,是 x 不是 *
    window.geometry("400x400")
    #不能改变窗口的大小
    window.resizable(width=False,height=False)
    #相当于动画显示
    var = tk.StringVar()
    #传值
    var.set("Hello,欢迎登录!")
    #创建title标签,以便更清楚观察
    title = tk.Label(window,textvariable=var,height=2,width=30,bg="yellow",font=("宋体",15))
    title.pack()
    #创建容器
    f_1 = tk.Frame(window)
    f_1.place(x=100,y=50)
    #标签1
    l_1 = tk.Label(f_1,text="账号:")
    l_1.pack()
    #标签2
    l_2 = tk.Label(f_1,text="密码:")
    l_2.pack()
    #创建容器
    f_2 = tk.Frame(window)
    f_2.place(x=170,y=50)
    #编辑框1
    e_1 = tk.Entry(f_2,width=20)
    e_1.pack()
    f_5 = tk.Frame(window)
    f_5.place(x=180,y=180)
    #编辑框2,隐藏输入的内容
    e_2 = tk.Entry(f_2,width=20,show="*")
    e_2.pack()
    #图片标签
    label_image = tk.Label(f_5)
    def log():
        #
        #判断编辑框的内容
        if e_1.get() == "123" and e_2.get() == "123":
            #
            #image = tk.PhotoImage(file="3.png")
            #image = tk.PhotoImage(file="1.png")
            image = tk.PhotoImage(file="2.gif")
            #添加图片
            label_image.config(image=image)
            label_image.pack()
            #传值
            var.set("登陆成功")
            #使用消息框,弹出对应的信息
            #messagebox.showinfo(title="提示",message="欢迎使用本系统")
            if messagebox.askokcancel("确定", "欢迎使用本系统"):
                window.destroy()
        else:
            #传值
            var.set("账号或密码输入错误!")
            # 使用消息框,弹出对应的信息
            messagebox.showerror(title="错误",message="你的输入有误!")
            if messagebox.askokcancel("确定", "你的输入有误"):
                window.destroy()
    
    def log2():
        #
        #判断编辑框的内容
        #messagebox.showinfo(title="提示",message="取消登录!")
        if messagebox.askokcancel("确定", "取消登录!!"):
            window.destroy()
    
    f_3 = tk.Frame(window)
    f_3.place(x=100,y=100)
    
    #创建登陆按钮
    button = tk.Button(f_3,text="confirm",command=log)
    button.pack()
    f_4 = tk.Frame(window)
    f_4.place(x=250,y=100)
    #创建取消按钮
    button = tk.Button(f_4,text="cancel",command=log2)
    #button = tk.Button(f_4,text="cancel",command=window.quit) #或者这个
    button.pack()
    #循环窗口
    window.mainloop()#运行

    运行结果:

     

    展开全文
  • 因为调用的后台程序执行时间过长,tkinter是通过不停的刷新页面来进行显示的,这个程序一调用,tkinter就不刷新了,界面就卡死了。 欸,这时候就想起了线程,一个线程去后台执行程序,一个线程用来在前面刷新...

    原文地址

    分类目录——多线程

    用tkinter写了个窗口界面来 Python+FFmpeg音视频格式转换 ,点击执行之后,完蛋,界面卡死了。

    因为调用的后台程序执行时间过长,tkinter是通过不停的刷新页面来进行显示的,这个程序一调用,tkinter就不能刷新了,界面就卡死了。

    欸,这时候就想起了多线程,一个线程去后台执行程序,一个线程用来在前面刷新显示。

    就去搜了一下,果然跟我想的一样~

    这里是关键代码

    # 点击提交执行的功能
    def executeit():
        hint.set('converting . . .')
        convert(pathin.get(), pathout.get(), aimf.get())
        hint.set('convert finished')
    
    # 创建线程执行程序
    def thread_it(func, *args):		# 传入函数名和参数
        # 创建线程
        t = threading.Thread(target=func, args=args)
        # 守护线程
        t.setDaemon(True)
        # 启动
        t.start()
        
    # 提交按钮
    # tk.Button(window, text='  提 交  ', command=executeit).place(x=200, y=190)	# 普通执行
    tk.Button(window, text='  提 交  ', command=lambda:thread_it(executeit)).place(x=200, y=190)	# 通过另外线程执行
    

    完整代码如下

    import tkinter as tk
    from tkinter.filedialog import *
    from convert import convert		# 导入我自己写的方法
    # 在pycharm的编译器中这种方式无法通过,在用pyinstaller封装是是能识别的,并且pyinstaller无法识别从工程根目录开始一层层import的方式
    # 注:上述写法是从conver.py中导入conver方法
    import threading
    
    # 点击“选择文件”按钮调用该功能
    def selectFilePath():
        path_ = askopenfilename(title='选择文件')
        pathin.set(path_)
        print(pathin.get())
    
    # 点击“选择文件夹”调用该功能
    def selectDirecPath():
        path_ = askdirectory(title='选择文件夹')
        # path_ = askopenfilename(title='选择文件')
        pathout.set(path_)
        print(path_)
    
    # 点击提交执行的功能
    def executeit():
        hint.set('converting . . .')
        convert(pathin.get(), pathout.get(), aimf.get())
        hint.set('convert finished')
    
    def thread_it(func, *args):
        # 创建线程
        t = threading.Thread(target=func, args=args)
        # 守护线程
        t.setDaemon(True)
        # 启动
        t.start()
    
    # 生成窗口
    window = tk.Tk()
    window.title('格式转换')
    window.geometry('450x300')
    
    # 格式转换label
    tk.Label(window, text='格式转换').place(x=200, y=10)  # x是从左向右的偏移,y是从上向下的偏移
    
    # 输入文件一行
    pathin = tk.StringVar()     # 定义变量
    tk.Label(window, text='输入文件:').place(x=50, y=50)
    entry_pathin = tk.Entry(window, textvariable=pathin).place(x=160, y=50)     # 输入框
    btn_pathin = tk.Button(window, text='选择文件', command=selectFilePath).place(x=340, y=45)  # 按钮
    
    # 输出文件的一行
    pathout = tk.StringVar()
    tk.Label(window, text='输出位置:').place(x=50, y=100)  # 从左偏,从上偏
    tk.Entry(window, textvariable=pathout).place(x=160, y=100)
    tk.Button(window, text='选择文件夹', command=selectDirecPath).place(x=335, y=95)
    
    # 目标格式的一行
    aimf = tk.StringVar()
    tk.Label(window, text='目标格式:').place(x=50, y=150)  # 从左偏,从上偏
    tk.Entry(window, textvariable=aimf).place(x=160, y=150)
    
    # 提交按钮
    # tk.Button(window, text='  提 交  ', command=executeit).place(x=200, y=190)
    tk.Button(window, text='  提 交  ', command=lambda:thread_it(executeit)).place(x=200, y=190)
    
    # 提示区域
    hint = tk.StringVar()
    hint.set('')
    tk.Label(window, textvariable=hint).place(x=190, y=240)
    
    # 不停的刷新显示
    window.mainloop()
    

    上面的代码从我自己写的convert.py导入了convert这个函数

    import os
    
    def convert(pin, pout, aim):
        fname = pin.split('/')[-1].split('.')[0]
        print(fname)
        cmd = 'ffmpeg -i ' + pin + ' ' + pout+'/'+fname+'.'+aim
        print('converting. . .')
        # os.popen()命令会转为后台运行,先完成后面的命令的执行
        # os.popen(cmd)
        os.system(cmd)
        print('convert finished')
    

    参考文献

    tkinter界面卡死的解决办法

    展开全文
  • 再用tkinter编辑个ui界面发给同学用,岂不是救众生于水火,功德无量吗?就这样,他幻想着同学们投来崇拜的目光,眼(zui)角流着幸福的泪(kou)水,错过了交作业的最后一点时间…… 一.编写计算文件 工欲善其事,必...
  • 用python的tkinter写一个音乐播放器

    千次阅读 2019-12-29 14:53:59
    就学了一下tkinter,主要参考的《疯狂python》这本书,期间碰到一些比较i麻烦的问题,但最终都解决了,然后也解决了很bug,做出来的播放器虽然不好看(完全没怎么在意美观的问题,界面美化的话花的时间总做好的...

    花了几天时间用python写里一个音乐播放器

    最近对用python做gui很感兴趣,就学了一下tkinter,主要参考的《疯狂python》这本书,期间碰到一些比较i麻烦的问题,但最终都解决了,然后也解决了很多bug,做出来的播放器虽然不好看(完全没怎么在意美观的问题,界面美化的话花的时间总能做好的,作为实用主义就不花时间做美化了),但是自己用的时候感觉还是满意的,作为播放器该有的功能也有,当然不能和网易云音乐比,也只能播放本地音乐,不能在线。下面是代码,差不多1000行,菜鸟水平所以代码编得啰嗦才这么多行。

    from tkinter import *
    from tkinter import ttk
    from tkinter import filedialog
    from tkinter import simpledialog
    import re
    import psutil as ps
    import os
    import pathlib as pa
    # from tkinter import messagebox
    from pygame import mixer
    # import eyed3
    # import time
    from tinytag import TinyTag as ti
    from threading import Timer
    from random import randint
    
    
    ##music list choose
    class mlf(Toplevel):
        def __init__(self, parent, stems, lb_test, musiclist, playlist):
            Toplevel.__init__(self, parent)
            self.transient(parent)
            self.title('Files you find')
            self.parent = parent
            self.grab_set()
            self.lb_test = lb_test
            self.fr = ttk.Labelframe(self, text='Choose desks to scan')
            self.fr.pack(side=TOP, fill=BOTH, expand=YES)
            self.stems = stems
            self.select_stem = []
            self.musiclsit = musiclist
            self.playlist = playlist
            self.lb = Listbox(self.fr, font=('微软雅黑', 10, 'bold'), selectmode='extended')
            self.lb.pack(side=LEFT, fill=BOTH, expand=YES)
            for stem in stems:
                self.lb.insert(END, pa.Path(stem).stem)
            scroll = ttk.Scrollbar(self.fr, command=self.lb.yview)
            scroll.pack(side=RIGHT, fill=BOTH)
            self.lb.configure(yscrollcommand=scroll.set)
            self.select_all = ttk.Button(self, text='Select all', command=self.select_allf)
            self.select_all.pack(side=LEFT, fill=Y, expand=YES)
            self.ok = ttk.Button(self, text='Ok', command=self.okf)
            self.ok.pack(side=LEFT, fill=Y, expand=YES)
            self.quit_all = ttk.Button(self, text='Quit all', command=self.quit_allf)
            self.quit_all.pack(side=RIGHT, fill=Y, expand=YES)
    
        def select_allf(self):
            self.lb.selection_set(0, END)
    
        def okf(self):
            print(self.lb.curselection())
            for x in self.lb.curselection():
                if x not in self.select_stem:
                    self.select_stem.append(self.stems[x])
            print(self.select_stem)
            for x in self.select_stem:  # should create a txt to save musiclist !!
                if x not in self.musiclsit:
                    self.musiclsit.append(x)
                    self.lb_test.insert(END, pa.Path(x).stem)
                    self.playlist.append(x)
            self.destroy()
    
        def quit_allf(self):
            self.lb.selection_clear(0, END)
    
    
    # Custom Dialog
    class sfs(Toplevel):
        def __init__(self, parent, lb, sf, musiclist, save, playlist):
            Toplevel.__init__(self, parent)
            self.transient(parent)
            self.title('Choose desks and Scan files')
            self.parent = parent
            self.grab_set()
            self.lb_test = lb
            self.musiclist = musiclist
            self.playlist = playlist
            self.sf = sf
            self.save = save
            fr = ttk.Labelframe(self, text='Choose desks to scan')
            fr.pack()
            d = ps.disk_partitions()
            # limit files size
            self.test = FALSE
            self.maxs = IntVar()
            self.mins = IntVar()
            self.maxs.set(50)
            self.mins.set(1)
            self.ax = []
            self.bool = BooleanVar()
            self.bool.set(FALSE)
    
            for xx in d:
                if not xx[3] == 'cdrom':
                    print(xx[0])
                    self.ax.append(xx[0])
            i = 0
            self.desks = []
            for desk in self.ax:
                strVar = StringVar()
                self.desks.append(strVar)
                cb = ttk.Checkbutton(self, text=desk, variable=strVar, onvalue=str(i), offvalue='none',
                                     command=self.choose);
                i += 1
                cb.pack(anchor=W)
            self.ok = ttk.Button(self, text='Ok', command=self.okf);
            self.ok.pack(side=LEFT, fill=Y, expand=YES)
            self.cancel = ttk.Button(self, text='Return', command=self.cancelf)
            self.cancel.pack(side=LEFT, fill=Y, expand=YES)
            self.sizel = ttk.Button(self, text='Limit the file size', command=self.sizelf)
            self.sizel.pack(side=RIGHT, fill=Y, expand=YES)
    
        def choose(self):
            st = [e.get() for e in self.desks]
            print(st)
    
        def okf(self):
            st = [e.get() for e in self.desks]
            # print(st)
            sc = []
            for xx in range(len(st)):
                if str(xx) in st:
                    sc.append(self.ax[xx])
            print(sc)
    
            stems = []
            # for format in ('.mp3','.flac'):\for x in p.glob('**/*'):
            for fx in sc:
                px = pa.Path(fx)
                for x in px.glob('**/*'):
                    if ti.is_supported(str(x.name)):
                        # print(x)
                        if (os.path.getsize(x)) > self.mins.get() * 1024 * 1024 and (
                        os.path.getsize(x)) < self.maxs.get() * 1024 * 1024:
                            stems.append(x)
            test = mlf(self, stems, self.lb_test, self.musiclist, self.playlist)
    
        def cancelf(self):
            self.parent.focus_set()
            self.destroy()
            print('sss')
            self.bool.set(TRUE)
            self.test = TRUE
            self.sf['style'] = 'SM.TLabel'
            self.save()
    
        def sizelf(self):
            maxs = simpledialog.askinteger('Minimum szie', 'the minimum size of files (\M)', initialvalue=1, )
            mins = simpledialog.askinteger('Maximum szie', 'the maximum size of files (\M)', initialvalue=50)
            if maxs:
                self.mins.set(maxs)
            if mins:
                self.maxs.set(mins)
    
            print(self.maxs.get())
            print(self.mins.get())
    
    
    class App():
        def __init__(self, master):
            self.master = master
            self.initt()
    
        def initt(self):
            titlefont = ('simkai', 15, 'bold')
            self.musiclist = []
            self.playlist = []#播放列表
            self.lines = []
            self.pbl = []
            self.pl = []  # playlist  歌单列表
            self.like=[]#like 歌单第一个
            self.filepathlist=[]  #file path list
            self.play_if = BooleanVar()
            self.play_if.set(FALSE)
            self.is_all = BooleanVar()
            self.is_all.set(TRUE)
            self.playinglist = StringVar()
    
            self.filename = 'save_music.txt'
            mixer.init()
            self.playing = StringVar()
            self.endsave=[]#save_end_
    
            try:
                with open(self.filename, 'r+', True, 'UTF-8') as ff:
                    self.lines = ff.readlines()
            except:
                f = open(self.filename, 'w+', True, 'UTF-8')
                f.write('musiclist--> \n')
                f.write('playlist--> \n')
                f.write('Like (protected playlist)\n')
                f.write('end--> \n')
                f.write("mode 3\n")
            with open(self.filename, 'r+', True, 'UTF-8') as ff:
                self.lines = ff.readlines()
                if self.lines[1] == 'playlist--> \n':
                    print('musiclist is empty')
                else:
                    print('tt')
                    for line in self.lines[1:self.lines.index(('playlist--> \n'))]:
                        if not line == '\n':
                            self.musiclist.append(line[0:-1])
                if self.lines.index('playlist--> \n') == self.lines.index('end--> \n') - 1:
                    print('playlist is empty')
                else:
                    for line in self.lines[self.lines.index('playlist--> \n') + 1:self.lines.index('end--> \n')]:
                        if not line == '\n':
                            self.pbl.append(line[0:-1])
                    for line in self.lines[self.lines.index('end--> \n')+1: ]:
                        self.endsave.append(line)
                    #print(self.pbl)
                #print(self.lines)
            for pb in self.pbl:
                if not ti.is_supported(pb):
                    self.pl.append(pb)
            if len(self.musiclist)!=0:
                self.playing.set(self.musiclist[0])
            for x in self.pbl[1:]:
                if pa.Path(x).is_file():
                    self.like.append(x)
                    #print(x)
                else:
                    break
            #print(self.like)
            style = ttk.Style()
            style.configure("BW.TLabel", foreground="blue", background="white", font=titlefont, anchor=W)
            style.configure("CL.TLabel", foreground="blue", background='gray', font=titlefont, anchor=W)
            style.configure("SM.TLabel", foreground="blue", background='white', font=('微软雅黑', 10, 'bold'), anchor=W)
            style.configure("SMCL.TLabel", foreground="blue", background='gray', font=('微软雅黑', 10, 'bold'), anchor=W)
            fmlt = ttk.Frame(self.master, height=1000, width=800)
            fmlt.pack(side=TOP, fill=BOTH, expand=YES, padx=10, pady=10, anchor=NW)
            fml = ttk.Frame(fmlt, height=100, width=100)
            fml.pack(side=LEFT, fill=NONE, expand=NO, padx=10, pady=10, anchor=NW)
            fmb = ttk.Frame(self.master, width=800)
            fmb.pack(side=BOTTOM, fill=X, expand=NO, anchor=S, padx=10, pady=10)
            fmr = ttk.Frame(fmlt, width=500)
            fmr.pack(side=RIGHT, fill=BOTH, expand=YES, anchor=W, padx=5, pady=5)
    
            fm1 = ttk.Labelframe(fml, text='Playlist setting')
            fm1.pack(side=TOP, fill=NONE, expand=YES, anchor=NW)
            fm1.bind('<Leave>', self.leave_lefttop)
            self.ap = ttk.Button(fm1, text='Add playlist', style='BW.TLabel', command=self.add_pl)
            self.ap.pack(side=TOP, fill=X, expand=YES)
            self.am = ttk.Button(fm1, text='All added mousic', style='BW.TLabel', command=self.all_music)
            self.am.pack(side=TOP, fill=X, expand=YES)
            self.fm2 = ttk.Labelframe(fml, text='Playlist list')
            self.fm2.pack(side=TOP, fill=BOTH, expand=YES, anchor=N, pady=10)
            self.fm3 = ttk.Labelframe(fmr, text='Music list')
            self.fm3.pack(side=BOTTOM, fill=BOTH, expand=YES)
            self.lb = Listbox(self.fm3, selectmode='browse', font=titlefont, foreground="green", background="white")#four mode
            self.lb.pack(side=LEFT, fill=BOTH, expand=YES)
            self.lb.bind('<Button-3>', self.pop)
            self.lb.bind('<Motion>', self.lb_focus)
            self.pb = Listbox(self.fm2, selectmode='browse', font=titlefont)
            self.pb.pack(side=LEFT, fill=BOTH, expand=YES)
            self.pb.bind('<Button-1>', self.artist_play)
            self.pb.bind('<Button-3>', self.pbpop)
            self.pb.bind('<Motion>', self.pb_focus)
            self.pb.bind('<Control-1>',self.ctrlpb)
            self.pb.bind('<ButtonRelease-3>',self.pbrel)
            self.ctrl=BooleanVar()
            self.ctrl.set(FALSE)
            self.popmenu = Menu(self.lb, tearoff=0)
            self.popmenu.add_cascade(label='play', command=self.play_it)
            self.popmenu.add_cascade(label='pause or play', command=self.pause)
            self.m = Menu(self.popmenu, tearoff=0)
            self.popmenu.add_cascade(label='add to playlist', menu=self.m)
            self.popmenu.add_cascade(label='delate', command=self.del_music)
    
            self.pbmenu = Menu(self.pb, tearoff=0)
            self.pbmenu.add_cascade(label='delate', command=self.del_list)
            self.pbmenu.add_cascade(label='rename', command=self.rename)
            self.pbmenu.add_cascade(label='add', command=self.add_pl)
    
            fm2 = ttk.Frame(fml)
            fm2.pack(side=BOTTOM, fill=BOTH, expand=YES, anchor=N, pady=10)
            ttk.Button(fm2, text='delate playlist', style='SM.TLabel', command=self.del_list).pack(side=LEFT, expand=NO,
                                                                                                   padx=5,
                                                                                                   anchor=SW)  # ddddeeettt
            ttk.Button(fm2, text='rename', style='SM.TLabel', command=self.rename).pack(side=LEFT, expand=NO,
                                                                                        padx=5, anchor=SW)
            self.sort=ttk.Button(fm2, text='sort by artist', style='SM.TLabel', command=self.artist)
            self.sort.pack(side=LEFT, expand=NO,padx=5, anchor=SW)
            self.artistnow = BooleanVar()
            self.artistnow.set(FALSE)
            scrollp = ttk.Scrollbar(self.fm2, command=self.pb.yview)
            scrollp.pack(side=RIGHT, fill=Y)
            self.pb.configure(yscrollcommand=scrollp.set)
            for x in self.pl:  # should create a txt to save musiclist !!
                self.pb.insert(END, x)
                self.m.add_command(label=x, command=self.testt(self.add_topl, x=x))
            # ttk.Button(fm2, text='All added mousic', style='BW.TLabel').pack(side=BOTTOM, fill=X, expand=YES)
            fm4 = ttk.Frame(fmr);
            fm4.pack(side=TOP, fill=NONE, expand=NO, anchor=W)
            self.sf = ttk.Button(fm4, text='Scan files', style='SM.TLabel', command=self.sff)
            self.sf.pack(side=LEFT, expand=YES, padx=5, anchor=W)
            self.af = ttk.Button(fm4, text='Add file path', style='SM.TLabel',command=self.aff)
            self.af.pack(side=LEFT, expand=YES, padx=5, anchor=W)
            self.ms = ttk.Button(fm4, text='Browse', style='SM.TLabel',command=self.modechange)
            self.ms.pack(side=LEFT, expand=YES, padx=5, anchor=W)
            self.strVar = StringVar()
    
            self.cb = ttk.Button(fm4, text=self.endsave[0][0:-1] , style='SM.TLabel', command=self.change_module)
            self.cb.pack(side=LEFT, expand=YES, fill=BOTH, padx=5, anchor=W)
            # Combobox(fm4,textvariable=self.strVar,values=['play in order','random play','loop play in list','loop play single song']), four play model to select
            if len(self.endsave)>1:
                for x in self.endsave[1:]:
                    if 'mode' not in x and x!='\n':
                        self.playlist.append(x[0:-1])
                        self.lb.insert(END, pa.Path(x).stem)
                        #print(x)
            else:
                for x in self.musiclist:  # should create a txt to save musiclist !!
                    self.lb.insert(END, pa.Path(x).stem)
                    self.playlist.append(x)
    
    
            scroll = ttk.Scrollbar(self.fm3, command=self.lb.yview)
            scroll.pack(side=LEFT, fill=Y)
            self.lb.configure(yscrollcommand=scroll.set)
            self.lb.bind('<Double-1>', self.choose_play)
            self.cv = Canvas(fmb, background='white', height=40)
            self.cv.pack(fill=BOTH, expand=NO)
            self.cv.bind('<Button-1>', self.click)
            self.cv.bind('<ButtonRelease-1>', self.release)
            self.cv.create_line(97.5 - 5, 22.5 - 10, 97.5 - 5, 22.5 + 10, fill='red', width=4, tag='g1')  # playing
            self.cv.create_line(97.5 + 5, 22.5 - 10, 97.5 + 5, 22.5 + 10, fill='red', width=4, tag='g1')
            self.play_name = self.cv.create_text(220, 5, text=' ', font='simkai', fill='blue', anchor=W, justify=LEFT,
                                                 tag='g3')  # play_name
            self.spand = self.cv.create_rectangle(260, 35, 260, 40, fill='green', tag='g3')
            self.cv.tag_lower('g3')
    
            self.cv.create_line(220, 5 + 2, 220, 35, arrow=FIRST, width=2, fill='red', tag='m2', arrowshap=(5, 10, 5))
            self.cv.create_line(220, 5, 250 - 2, 5, arrow=LAST, width=2, fill='red', tag='m2', arrowshap=(5, 10, 5))
            self.cv.create_line(250, 5, 250, 35 - 2, arrow=LAST, width=2, fill='red', tag='m2', arrowshap=(5, 10, 5))
            self.cv.create_line(250, 35, 220 + 2, 35, arrow=LAST, width=2, fill='red', tag='m2', arrowshap=(5, 10, 5))
    
            self.cv.create_line(220, 5, 220, 35, arrow=FIRST, width=2, fill='violet', tag='m3', arrowshap=(5, 10, 5))
            self.cv.create_line(220, 5, 250, 5, arrow=None, width=2, fill='violet', tag='m3', arrowshap=(5, 10, 5))
            self.cv.create_line(250, 5, 250, 35, arrow=None, width=2, fill='violet', tag='m3', arrowshap=(5, 10, 5))
            self.cv.create_line(250, 35, 220, 35, arrow=None, width=2, fill='violet', tag='m3', arrowshap=(5, 10, 5))
            self.cv.create_oval(235 - 10, 20 - 10, 235 + 10, 20 + 10, fill='yellow', outline='green', tag='m3')
            self.name=self.cv.create_text(235, 20, text=' ', width=2, fill='blue', tag='m3')
    
            self.cv.create_line(235 - 5, 20 + 5, 220, 35, arrow=LAST, width=2, fill='violet', tag='m4',
                                arrowshap=(5, 10, 5))
            self.cv.create_line(235 - 5, 20 - 5, 220, 5, arrow=LAST, width=2, fill='violet', tag='m4', arrowshap=(5, 10, 5))
            self.cv.create_line(235 + 5, 20 + 5, 250, 35, arrow=LAST, width=2, fill='violet', tag='m4',
                                arrowshap=(5, 10, 5))
            self.cv.create_line(235 + 5, 20 - 5, 250, 5, arrow=LAST, width=2, fill='violet', tag='m4', arrowshap=(5, 10, 5))
    
            self.cv.create_rectangle(0, 1, 200 - 5, 42, fill='yellow', width=0)
            self.cv.create_rectangle(200, 1, 260, 42, fill='white', width=0)
            self.cv.create_line(225, 5, 225, 35, arrow=LAST, width=2, fill='red', tag='m1', arrowshap=(5, 10, 5))
            self.cv.create_line(235, 5, 235, 35, arrow=LAST, width=2, fill='red', tag='m1', arrowshap=(5, 10, 5))
            self.cv.create_line(245, 5, 245, 35, arrow=LAST, width=2, fill='red', tag='m1', arrowshap=(5, 10, 5))
    
            self.cv.create_oval(20, 5, 60 - 5, 40, fill='pink', outline='green')
            self.cv.create_oval(80, 5, 120 - 5, 40, fill='pink', outline='green')
            self.cv.create_oval(140, 5, 180 - 5, 40, fill='pink', outline='green')
            self.cv.create_polygon(22.5, 22.5, 45, 22.5 - 7.5 * 3 ** 0.5, 45, 22.5 + 7.5 * 3 ** 0.5, fill='red')
            self.cv.create_polygon(172.5 - 60, 22.5, 150 - 60, 22.5 - 7.5 * 3 ** 0.5, 150 - 60, 22.5 + 7.5 * 3 ** 0.5,
                                   fill='red', tag='g2')  # stop playing
            self.cv.create_polygon(172.5, 22.5, 150, 22.5 - 7.5 * 3 ** 0.5, 150, 22.5 + 7.5 * 3 ** 0.5,
                                   fill='red')  # 190-22.5
            self.cv.create_rectangle(500,0,600,10,fill='orange')
            self.cv.create_text(470,6,text='Volume',fill='blue')
            self.vol=self.cv.create_rectangle(500,0,600,10,fill='green')
            self.to_like = self.cv.create_polygon(190, 30, 210, 30, 200, 0, fill='gray')
            self.to_like=self.cv.create_polygon(190,15,210,15,200,30,fill='gray')
            # 定时器需要
            self.pause = BooleanVar()
            self.pause.set(FALSE)
            self.press_3=BooleanVar()
            self.press_3.set(FALSE)#youjian
            self.save()
            self.func()  # check playing_if
            self.speed = 1
            self.long = DoubleVar()
            self.long.set(260.)
            self.control_play = BooleanVar()
            self.control_play.set(FALSE)
            self.change_module();self.change_module();self.change_module();self.change_module();self.change_module();
        def func(self):#实时更新
            if self.play_if.get() and mixer.music.get_busy():
                test = ti.get(self.playing.get())
                self.speed = 24 / float(test.duration)
                self.long.set(self.long.get() + self.speed)
                #self.name['text']=self.playing.get()
                self.cv.itemconfigure(self.name,text=self.playing.get())
                # print(str(self.long.get()))
                self.cv.coords(self.spand, 260, 33, float(self.long.get()), 42)
            elif self.play_if.get() and not mixer.music.get_busy() and not len(self.playlist) == 0:
                self.to_next()
                #print('to_next')
            elif not self.play_if.get() and not mixer.music.get_busy() and not len(self.playlist) == 0:
                self.playing.set(self.playlist[0])
                # print('start')
                # self.t.cancel()
            if len(self.playlist) == 0 and not mixer.music.get_busy():
                self.play_if.set(FALSE)
                self.cv.tag_raise('g2')
                self.cv.tag_lower('g1')
            if self.playing.get() in self.like:
                self.cv.itemconfigure(self.to_like,fill='red')
            else:
                self.cv.itemconfigure(self.to_like,fill='gray')
            t = Timer(0.1, self.func)
            t.setDaemon(TRUE)
            t.start()
    
        def to_next(self):
            if self.cb['text'] == 'mode 1':
                if self.playing.get() not in self.playlist:
                    self.playing.set(self.playlist[0])
                elif self.playlist.index(self.playing.get()) == len(self.playlist) - 1:
                    if not mixer.music.get_busy():
                        mixer.music.stop()
                        self.play_if.set(FALSE)  # notplaying
                        print('busy')
                        self.cv.tag_raise('g2')
                        self.cv.tag_lower('g1')
                    else:
                        self.playing.set(self.playlist[0])
                        self.play_music(self.playing.get())
                elif self.play_if.get():
                    self.playing.set(self.playlist[self.playlist.index(self.playing.get()) + 1])
                    self.play_music(self.playing.get())
                else:
                    self.play_music(self.playing.get())
                   # print('??')
            elif self.cb['text'] == 'mode 2':
                if self.playlist.index(self.playing.get()) == len(self.playlist) - 1:
                    self.playing.set(self.playlist[0])
                    self.play_music(self.playing.get())
                else:
                    self.playing.set(self.playlist[self.playlist.index(self.playing.get()) + 1])
                    self.play_music(self.playing.get())
            elif self.cb['text'] == 'mode 3':
                if self.play_if.get() and not mixer.music.get_busy():
                    self.play_music(self.playing.get())
                elif self.playlist.index(self.playing.get()) == len(self.playlist):
                    self.playing.set(self.playlist[0])
                    self.play_music(self.playing.get())
                else:
                    self.playing.set(self.playlist[self.playlist.index(self.playing.get()) + 1])
                    self.play_music(self.playing.get())
            elif self.cb['text'] == 'mode 4':
                self.playing.set(self.playlist[randint(0, len(self.playlist)) - 1])
                self.play_music(self.playing.get())
              #  print(self.playing.get())
           # print('ss')
    
        def to_before(self):
            if self.cb['text'] == 'mode 1':
                self.playing.set(self.playlist[self.playlist.index(self.playing.get()) - 1])
                self.play_music(self.playing.get())
            elif self.cb['text'] == 'mode 2':
                self.playing.set(self.playlist[self.playlist.index(self.playing.get()) - 1])
                self.play_music(self.playing.get())
            elif self.cb['text'] == 'mode 3':
                self.playing.set(self.playlist[self.playlist.index(self.playing.get()) - 1])
                self.play_music(self.playing.get())
            elif self.cb['text'] == 'mode 4':
                self.playing.set(self.playlist[randint(0, len(self.playlist)) - 1])
                self.play_music(self.playing.get())
            print('ss')
    
        def to_play(self):
            if self.cb['text'] == 'mode 4':
                self.playing.set(self.playlist[randint(0, len(self.playlist)) - 1])
                self.play_music(self.playing.get())
            else:
                self.play_music(self.playing.get())
            print('ss')
    
        def add_pl(self):
            if not self.artistnow.get():
                self.ap['style'] = 'CL.TLabel'
                self.am['style'] = 'BW.TLabel'
                print('no')
                name = simpledialog.askstring('Create a playlist', 'input the name of playlist:',
                                              initialvalue='New playlist')
                print(name)
                if name:
                    i = 0
                    strr = ''
                    while name in self.pl:
                        for xx in range(len(name)):
                            if re.match(r'\d', name[len(name) - 1 - xx]):
                                strr += name[len(name) - 1 - xx]
                                name = name[0:len(name) - 1 - xx]
                        i += 1
                        name = name + str(i)
                    self.pb.insert(END, name)
                    self.m.add_command(label=name)
                    self.pl.append(name)
                    self.pbl.append(name)
                    print(name)
                self.save()
    
        def all_music(self):
            self.am['style'] = 'CL.TLabel'
            self.ap['style'] = 'BW.TLabel'
            self.is_all.set(TRUE)
            if self.artistnow.get():
                self.artistnow.set(FALSE)
            self.pb.delete(0, END)
            for x in self.pl:  # should create a txt to save musiclist !!
                self.pb.insert(END, pa.Path(x).stem)
            self.lb.delete(0, END)
            self.playlist=[]
            for x in self.musiclist:  # should create a txt to save musiclist !!
                self.lb.insert(END, pa.Path(x).stem)
                self.playlist.append(x)
    
        def sff(self):
            self.sf['style'] = 'SMCL.TLabel'
            self.af['style'] = 'SM.TLabel'
            self.ms['style'] = 'SM.TLabel'
            test = sfs(self.master, self.lb, self.sf, self.musiclist, self.save, self.playlist)
        def aff(self):
            filepath=filedialog.askdirectory(title='choose file path',initialdir='F:/')
            stems = []
            # for format in ('.mp3','.flac'):\for x in p.glob('**/*'):
            for x in pa.Path(filepath).glob('**/*'):
                if ti.is_supported(str(x.name)):
                    if (os.path.getsize(x)) > 1 * 1024 * 1024 and (
                            os.path.getsize(x)) < 50 * 1024 * 1024:
                        stems.append(x)
    
            test = mlf(self.master, stems, self.lb, self.musiclist, self.playlist)
        def save(self):
            self.endsave=['mode 3']
            self.endsave.extend(self.playlist)
            #print(self.endsave)
            self.lines = ['musiclist--> \n'] + self.musiclist + ['playlist--> \n'] + self.pbl + ['end--> \n']+self.endsave
            # print('save_test')
            # print(self.lines)
            with open(self.filename, 'w', True, 'UTF-8') as ff:
                for x in self.lines:
                    if not '\n' in x.__str__():
                        ff.write(x.__str__() + '\n')
                        #print(x.__str__() + '\n')
                    elif x !='\n':
                        ff.write(x.__str__())
    
        def choose_play(self, event):
            print(u"播放音乐1")
           # track = mixer.music.load(str(self.playlist[self.lb.curselection()[0]]))
            # print(track)
            self.play_music(self.playlist[self.lb.curselection()[0]])
            self.play_if.set(TRUE)
    
        #  'play in order', 'random play', 'loop play in list', 'loop play single song'
        def play_music(self, file):
            print(file)
            mixer.init()
            try:
                track = mixer.music.load(str(file))
            except:
                self.lb.delete(self.playlist.index((file)))
                self.musiclist.remove(file)
                track = mixer.music.load(str(self.playlist[(self.playlist.index(file)+1)%len(self.playlist)]))
                print(str(self.playlist[(self.playlist.index(file)+1)%len(self.playlist)]))
                self.playlist.remove(file)
    
            self.playing.set(str(file))
            self.play_if.set(TRUE)
            self.long.set(260.)
            mixer.music.play()
            self.lb.selection_clear(0, END)
            self.lb.selection_set(self.playlist.index(file))
            self.lb.see(self.playlist.index(file))
            test = ti.get(file)
            self.cv.tag_raise('g1')
            self.cv.tag_lower('g2')
            self.cv.delete('g3')
            self.play_name = self.cv.create_text(260, 20, text=pa.Path(file).name, font='simkai', fill='green',
                                                 anchor=W, justify=LEFT, tag='g3')
            self.cv.create_text(510, 35, text=str(int(test.duration)) + ' s', font=('simkai', 10, 'bold'), fill='green',
                                anchor=W, justify=LEFT, tag='g3')
            self.cv.create_rectangle(260, 33, 500, 42, fill='blue', width=0, tag='g3')
            self.spand = self.cv.create_rectangle(260, 35, 260, 40, fill='yellow', tag='g3')
    
            self.cv.tag_raise('g3')
    
        def change_module(self):
            if self.cb['text'] == 'mode 1':
                self.cb['text'] = 'mode 2'
                self.cv.tag_raise('m2')
                self.cv.tag_lower('m1')
            elif self.cb['text'] == 'mode 2':
                self.cb['text'] = 'mode 3'
                self.cv.tag_raise('m3')
                self.cv.tag_lower('m2')
            elif self.cb['text'] == 'mode 3':
                self.cb['text'] = 'mode 4'
                self.cv.tag_raise('m4')
                self.cv.tag_lower('m3')
            elif self.cb['text'] == 'mode 4':
                self.cb['text'] = 'mode 1'
                self.cv.tag_raise('m1')
                self.cv.tag_lower('m4')
    
        def click(self, event):
            if event.x > 80 and event.x < 115 and mixer.music.get_busy() and not len(self.playlist) == 0:
                if self.pause.get():
                    print('??')
                    self.pause.set(FALSE)
                    mixer.music.unpause()
                    self.play_if.set(TRUE)
                    self.cv.tag_raise('g1')
                    self.cv.tag_lower('g2')
                else:
                    mixer.music.pause()
                    self.play_if.set(FALSE)
                    print('busy')
                    self.cv.tag_raise('g2')
                    self.cv.tag_lower('g1')
                    self.pause.set(TRUE)
            if event.x > 80 and event.x < 115 and not mixer.music.get_busy() and not len(self.playlist) == 0:
                print('stt')
                self.to_play()
            elif event.x > 140 and event.x < 175 and not len(self.playlist) == 0:
                self.to_next()
            elif event.x > 20 and event.x < 55 and not len(self.playlist) == 0:
                self.to_before()
            elif event.x > 260 and event.x < 500 and event.y > 30 and self.play_if.get():
                self.control_play.set(TRUE)
            elif event.x > 220 and event.x < 250:
                self.change_module()
            elif event.x>500 and event.x<600 and event.y<11:
                mixer.music.set_volume((event.x-500)/100)
                self.cv.coords(self.vol,500,0,event.x,10)
            elif event.x>190 and event.x<210 and event.y>15 and event.y<30:
    
                if self.playing.get() not in self.like:
                    self.like.append(self.playing.get())
                    self.pbl.insert(1,self.playing.get())
                    self.cv.itemconfigure(self.to_like, fill='red')
                else:
                    self.like.remove(self.playing.get())
                    self.pbl.remove(self.playing.get())
                    self.cv.itemconfigure(self.to_like, fill='gray')
        def release(self, event):
            print('release')
            if event.x > 260 and event.x < 500 and event.y > 30 and self.control_play.get() and mixer.music.get_busy():
                test = ti.get(self.playing.get())
                self.long.set(event.x)
                mixer.music.rewind()
                mixer.music.play(start=(event.x - 260.) / 240. * float(test.duration))
                print('??')
                self.pause.set(FALSE)
                # mixer.music.unpause()
                self.play_if.set(TRUE)
                self.cv.tag_raise('g1')
                self.cv.tag_lower('g2')
            elif event.x>500 and event.x<600 and event.y<11:
                mixer.music.set_volume((event.x-500)/100)
                self.cv.coords(self.vol,500,0,event.x,10)
                print(mixer.music.get_volume())
            # pygame.mixer.music.pause() #暂停
            # pygame.mixer.music.unpause()#取消暂停
    
        def del_list(self):
            if not len(self.pb.curselection()) == 0 and (not self.pb.curselection()[0] == 0) and not self.artistnow.get():
                #print(str(self.pb.curselection()))
                self.m.delete(self.pb.curselection()[0]), (self.pb.curselection()[0])
    
                print(self.pl)
                for xxx in self.pb.curselection():
                    i = 0
                    for xx in self.pbl[self.pbl.index(self.pl[xxx]):]:
                        if ti.is_supported(xx) or i == 0:
                            self.pbl.remove(xx)
                            i= 1
                        else:
                            break
                    self.pl.pop(xxx)
                    self.pb.delete(xxx)
                self.save()
            elif not len(self.pb.curselection()) == 0 and self.sort['text']=='sort by filepath':
                ss=self.pb.curselection()
                sel,pl,plist=[],[],[]
                for x in ss:
                    print(len(self.filepathlist))
                    sel.append(self.filepathlist[x])
                self.pb.delete(0,END)
                self.lb.delete(0,END)
                for xxx in sel:
                    self.filepathlist.remove(xxx)
                print(self.filepathlist)
                for xx in self.musiclist:
                        if pa.Path(xx).parent in self.filepathlist:
                            pl.append(xx)
                            print(xx)
                            if xx in self.playlist:
                                self.lb.insert(END,pa.Path(xx).stem)
                                plist.append(xx)
                self.musiclist=pl.copy()
                self.playlist=plist.copy()
               # print(self.musiclist)
                for x in self.filepathlist:
                    self.pb.insert(END,x)
            for x in self.pbl[1:]:
                if pa.Path(x).is_file():
                    self.like.append(x)
                else:
                    break
            self.save()
        def rename(self):
            if not len(self.pb.curselection()) == 0 and (not self.pb.curselection()[0] == 0) and not self.artistnow.get():
                num = int(str(self.pb.curselection())[1:-2])
                test = simpledialog.askstring('Rename the playlist', 'input a name:',
                                              initialvalue=str(self.pl[int(str(self.pb.curselection())[1:-2])]))
                if test:
                    print(num)
                    self.pb.delete(num)
                    self.pb.insert(num, test)
                    self.m.delete(num, num)
                    self.m.insert_command(num, label=test, command=self.testt(self.add_topl, x=test))
                    self.pbl[self.pbl.index(self.pl[num])] = test
                    self.pl[num] = test
    
        def artist(self):
            if self.artistnow.get() and self.sort['text']=='sort by filepath':
                self.pb['selectmode']='browse'
                self.ctrl.set(FALSE)
                print('sss')
                self.press_3.set(FALSE)
                self.sort['text']='sort by artist'
                self.artistnow.set(FALSE)
                self.pb.delete(0, END)
                for x in self.pl:  # should create a txt to save musiclist !!
                    self.pb.insert(END, pa.Path(x).stem)
            elif self.artistnow.get() and self.sort['text']=='sort by artist':
                self.sort['text']='sort by filepath'
                self.pb.delete(0, END)
                self.filepathlist=[]
                for x in self.musiclist:
                    if pa.Path(x).parent not in self.filepathlist:
                        self.filepathlist.append(pa.Path(x).parent)
                self.all_path=[pa.Path(x).parent for x in self.musiclist]
                print(self.all_path)
                print(self.filepathlist)
                ind=[self.all_path.count(x) for x in self.filepathlist]
                inds=ind.copy()
                self.pathcopy=self.filepathlist.copy()
                inds.sort(reverse=TRUE)
                nosame=[]
                #print(ind)
                for x in inds:
                    if x not in nosame:
                        nosame.append(x)
                i=0
                for x in nosame:
                    ax=ind.index(x)
                    for a in range(inds.count(x)):
                        self.filepathlist[i]=self.pathcopy[ind.index(x,ax)]
                        if not a ==inds.count(x)-1:
                            ax=ind.index(x,ax+1)
                        i+=1
                for x in self.filepathlist:
                    self.pb.insert(END, str(x)+ '  (' + str(inds[self.filepathlist.index(x)]) + ')')
    
            else :
                self.artistnow.set(TRUE)
                self.pb.delete(0, END)
                self.artistlist, allartistlist = [], []
                for x in self.musiclist:
                    #print(x)
                    try:
                        tag = ti.get(x)
                        allartistlist.append(tag.artist)
                        if tag.artist not in self.artistlist:
                            self.artistlist.append(tag.artist)
                    except:
                        if x in self.playlist:
                            self.lb.delete(self.playlist.index((x)))
                            self.playlist.remove(x)
                        self.musiclist.remove(x)
                        #print(x)
                        self.save()
                ind = [allartistlist.count(xx) for xx in self.artistlist]
                inds = ind.copy()  # 这里有点绕,利用指标辅助帮助艺术家根据歌曲数目排序,不简单
                inds.sort(reverse=True)
                artistlistsort = self.artistlist.copy()
                for x in artistlistsort:
                    self.artistlist[artistlistsort.index(x)] = '0'
                for art in artistlistsort:
                    i = inds.index(ind[artistlistsort.index(art)])
                    while True:
                        if self.artistlist[i] == '0':
                            self.artistlist[i] = art
                            break
                        else:
                            i += 1
                for x in self.artistlist:  # should create a txt to save musiclist !!
                    # print(x)
                    if not x:
                        self.pb.insert(END, 'Unkonw' + ' (' + str(inds[self.artistlist.index(x)]) + ') ')
                    else:
                        self.pb.insert(END, x + '  (' + str(inds[self.artistlist.index(x)]) + ')')
    
        def artist_play(self, event):  # 事件响应一定要记得加event!!!!!
            if self.artistnow.get() and not str(self.pb.curselection()) == '()' and self.sort['text']=='sort by artist':
                # print(str(self.pb.curselection()))
                x = self.pb.curselection()[0]
                self.lb.delete(0, END)
                self.playlist.clear()
                # print(self.artistlist[x])
                for xx in self.musiclist:
                    try:
                        tag = ti.get(xx)
                        if tag.artist:
                            # print(tag.artist)
                            if tag.artist == self.artistlist[x]:
                                self.lb.insert(END, pa.Path(xx).stem)
                                self.playlist.append(xx)
                        elif not self.artistlist[x]:
                            self.lb.insert(END, pa.Path(xx).stem)
                            self.playlist.append(xx)
                    except:
                        self.lb.delete(self.playlist.index((xx)))
                        self.musiclist.remove(xx)
                        self.playlist.remove(xx)
                        #print(xx)
                        self.save()
            elif self.artistnow.get() and not str(self.pb.curselection()) == '()' and self.sort['text']=='sort by filepath':
                x = self.pb.curselection()[0]
                self.is_all.set(TRUE)
                self.lb.delete(0, END)
                self.playlist.clear()
                for xx in self.musiclist:
                    if pa.Path(xx).parent==self.filepathlist[x]:
                        self.lb.insert(END, pa.Path(xx).stem)
                        self.playlist.append(xx)
    
    
            elif not str(self.pb.curselection()) == '()':
                x = self.pb.curselection()[0]
                self.lb.delete(0, END)
                self.playlist.clear()
                self.playlist = []
                self.is_all.set(FALSE)
                self.playinglist.set(self.pl[x])
                for xx in self.pbl[self.pbl.index(self.pl[x]) + 1:]:
                    if ti.is_supported(pa.Path(xx).name):
                        self.lb.insert(END, pa.Path(xx).stem)
                        self.playlist.append(xx)
                    else:
                        break
    
        def pop(self, event):
            self.popmenu.post(event.x_root, event.y_root)
    
        def pbpop(self, event):
            self.pbmenu.post(event.x_root, event.y_root)
            self.press_3.set(TRUE)
        def pbrel(self,event):
            self.press_3.set(FALSE)
            print('xxxx')
        def play_it(self):
            print(self.lb.curselection())
            if str(self.lb.curselection()) == '()':
                print('ddd')
                self.to_play()
            else:
                self.play_music(self.playlist[self.lb.curselection()[0]])
                self.play_if.set(TRUE)
    
        def pause(self):
            if self.pause.get():
                print('??')
                self.pause.set(FALSE)
                mixer.music.unpause()
                self.play_if.set(TRUE)
                self.cv.tag_raise('g1')
                self.cv.tag_lower('g2')
            else:
                mixer.music.pause()
                self.play_if.set(FALSE)
                print('busy')
                self.cv.tag_raise('g2')
                self.cv.tag_lower('g1')
                self.pause.set(TRUE)
    
        def add_topl(self, x):
            # print(str(self.lb.curselection())[1:-2])
            # print(self.lb.curselection()[0])
            #print(self.playlist)
            if not str(self.lb.curselection()) == '()':
                for ax in self.lb.curselection():
                    # print(self.pbl.index(x))
                    if x == self.pl[-1] and self.playlist[ax] not in self.pbl[int(self.pbl.index(x)):]:
                        self.pbl.insert(int(self.pbl.index(x)) + 1, self.playlist[ax])
                        #print(self.pbl)
                        self.save()
                    elif x == self.pl[-1]:
                        pass
                    elif self.playlist[ax] not in self.pbl[int(self.pbl.index(x)):int(self.pbl.index(self.pl[self.pl.index(x) + 1]))]:
                        self.pbl.insert(int(self.pbl.index(x)) + 1, self.playlist[ax])
                        #print(self.pbl)
                        self.save()
            for x in self.pbl[1:]:
                if pa.Path(x).is_absolute():
                    self.like.append(x)
                else:
                    break
    
    
    
        def testt(self, fun, **kwds):
            # print('xx')
            return lambda fun=fun, kwds=kwds: fun(**kwds)
    
        def leave_lefttop(self, event):
            self.am['style'] = 'BW.TLabel'
            self.ap['style'] = 'BW.TLabel'
    
        def del_music(self):
            print('de l')
            if not len(self.lb.curselection()) == 0 and self.is_all.get():
                self.musiclist.remove(self.playlist[self.lb.curselection()[0]])
                self.playlist.pop(self.lb.curselection()[0])
                self.lb.delete(self.lb.curselection()[0])
                print('del')
            elif not len(self.lb.curselection()) == 0:
                print(self.playinglist.get())
                for ax in self.lb.curselection():
                    if ax<len(self.playlist):
                        print(len(self.playlist))
                        print(ax)
                        m = self.playlist.pop(ax)
                        print(m)
                        self.lb.delete(ax)
                        x = self.pbl.index(self.playinglist.get())
                        print(x)
                        if x == len(self.pl) - 1:
                            if m in self.pbl[x:]:
                                print(self.pbl.pop(self.pbl.index(m, x)))
                        else:
                            y = self.pbl.index(self.pl[self.pl.index(self.playinglist.get()) + 1])
                            print(self.pbl.pop(self.pbl.index(m, x, y)))
    
    
    
        def lb_focus(self, event):
            if self.ms['text']=='Browse' or self.ms['text']=='Single':
                self.lb.selection_clear(0,END)
                self.lb.selection_set(self.lb.nearest(event.y))
        def pb_focus(self, event):
            if not self.press_3.get():
                if not self.ctrl.get():
                    self.pb.selection_clear(0, END)
                self.pb.selection_set(self.pb.nearest(event.y))
                #print(self.pb.curselection())
    
    
        def modechange(self):
            if self.ms['text']=='Browse':
                self.ms['text']='Single'
                self.lb['selectmode']='single'
            elif self.ms['text']=='Single':
                self.ms['text']='Multiple'
                self.lb['selectmode'] = 'multiple'
            elif self.ms['text']=='Multiple':
                self.ms['text']='Extended'
                self.lb['selectmode'] = 'extended'
            elif self.ms['text']=='Extended':
                self.ms['text']='Browse'
                self.lb['selectmode'] = 'browse'
        def ctrlpb(self,event):
            if self.ctrl.get() and self.sort['text']=='sort by filepath':
                self.ctrl.set(FALSE)
            elif self.sort['text']=='sort by filepath' :
                self.ctrl.set(TRUE)
                #print('ttt')
            print(self.ctrl.get())
    root = Tk()
    root.title('Music')
    test = App(root)
    if not root.mainloop():
        mixer.music.stop()
        test.save()
        # root.quit()
    
    
    展开全文
  • 前言 这个笔记只是为了方便我自己,我不想把这些杂的知识点在...但是我们不要在意太细节,因为Tkinter还是有许多被吐槽的地方,只需坚持一个原则:跑就行 主窗口 import tkinter as tk window = t...

    前言

    不会有很多注释
    python在界面开发这一块是没有什么优势的,但是当我们一些小项目需要可视化或者给别人使用的时候用python自带的模块会比较合适
    但是我们不要在意太多细节,因为Tkinter还是有许多被吐槽的地方,只需坚持一个原则:能跑就行

    主窗口

    import tkinter as tk
    window = tk.Tk()
    window.title('window')
    window.geometry('200x100+0+0') ##宽x高+x+y
    """
    这里塞部件
    """
    window.mainloop()

    Button按钮

    def callback():
    	pass
    Button = tk.Button(window, text='static text', command=callback)
    Button.place(x=100, y=100, width=40, height=25)

    Label标签

    Label部件只用于显示文本,文本不可复制
    如果文本固定不变,用不打注释的
    如果文本会变,用打注释的

    Label = tk.Label(window, text='static text')
    Label.place(x=100, y=100, width=40, height=25)
    #content = tk.StringVar() 
    #Label = tk.Label(window, textvariable=content)
    #Label.place(x=100, y=100, width=40, height=25)

    知识点一:文本可变用textvariable = tk.StringVar()
    赋值用下面语句

    content.set("new content")
    #content.get()

    Text编辑框

    Text不可用textvariable=content,也不可用Text.get()直接获取值

    Text = tk.Text(window)
    Text.place(x=100, y=100, width=40, height=25)

    知识点二:Text不可用textvariable=content来改变文本
    改变文本的方法如下

    #Text.delete(0.0, tk.END)
    Text.insert("insert", "追加的文本")
    #Text.get(0.0, tk.END)

    Entry输入框

    Entry部件和Text有一些区别:文本要换行用Text,输入密码用Entry
    获取文本有两种方法

    Entry = tk.Entry(window)
    Entry.place(x=100, y=100, width=40, height=25)

    知识点三:有换行、复制需求要用Text
    获取值、赋值用下面语句

    content = tk.StringVar()
    Entry = tk.Entry(window,textvariable=content)
    Entry.place(x=100, y=100, width=40, height=25)
    content.get()
    
    #Entry.get()

    ListBox列表

    如果要显示很多列数据而且要便于复制可以用Text、也可以用Listbox
    区别是Listbox你点击会有选中一行,Text会选中一个单词,而且Text可编辑
    Text的这些特性不代表它功能弱于Listbox,如果你要一行显示两列数据,那么使用Text会更方便复制

    contents = tk.StringVar()
    contents.set(("第一行","第二行","第三行"))
    ListBox = tk.Listbox(window, listvariable=contents)  #将var2的值赋给Listbox
    ListBox.place(x=100, y=100, width=40, height=25)

    ListBox可以很方便地进行增删
    知识点四:对ListBox项目进行修改

    ListBox.insert('end',"最后一行")
    ListBox.delete(2)

    RadioButton单选框

    所有地单选框只能同时选中一个

    content = tk.StringVar()
    Radiobutton1 = tk.Radiobutton(window, text='单选框1', variable=content, value='1')
    Radiobutton1.place(x=100, y=100, width=40, height=25)
    
    Radiobutton2 = tk.Radiobutton(window, text='单选框2', variable=content, value='2')
    Radiobutton2.place(x=100, y=100, width=40, height=25)

    CheckButton多项框

    content = tk.IntVar()
    CheckButton = tk.Checkbutton(window, text='复选框1', variable=content, onvalue=1, offvalue=0)
    CheckButton.place(x=100, y=100, width=40, height=25)

    知识点五: tk变量不全是StringVar,还有IntVar(),根据需要使用

    MessageBox

    如果要弹窗提示用户信息,使用MessageBox,唯一缺点就是弹窗的内容不可以复制

    tkinter.messagebox.showinfo('提示',"30 error 25 warning ")

    总结

    Tkinter基本组件就这几个,多的不要学了,高级功能也不要碰(比如快捷键,不太好使)
    下面总结一下上面组件的用处和缺点

    组件 用处 获取组件内容 缺陷
    MessageBox 提示信息,特别是错误信息 ---------- 信息内文字不可复制
    Button 触发事件 ---------- --------
    Label 显示不可复制的文字 ---------- 不可复制
    Text 编辑文字、显示文字 Text.get(0.0, tk.END) 不可用变量替代文本
    Entry 编辑文字、显示文字 Entry.get() 或 content.get()
    ListBox 显示列表信息 contents.get() ----
    RadioBottun 获取用户选择 content.get() ----
    CheckButton 获取用户选择 content.get() ----

    小提示

    很多组件建立的时候都可以加command=callback
    但是基本上只有Button和BadioButton需要,而且回调函数没有()

    展开全文
  • Python和VB都是让人快乐的编程语言,我使用了Python之后,很自己使用的工具都使用Python开发或改写了,因为最终实现的Python代码实在太短了(相比VB),有时候Python一行代码就可以实现VB一个函数的功能。...
  • 以代码为例,教你如何利用tkinter快速开发一个简易可用的...先贴出简单项目图给你提提兴趣(这些东西在学完后并查阅网上其他资料后是很简单的出来的): 既然你选择了tkinter用于开发程序界面,想必是看中了tkin...
  • 本项目是tkinter写界面,基于scrapy爬虫,爬取指定贴吧/某个帖子,通过treeview显 示爬取进度,并且可以搜索关键字、发帖人等 爬取指定贴吧思路: 1.进入该贴吧第N页-第M页,获取所有帖子的初始信息 2.分别...
  • 以前我的处理各种油田开发地质方面使用的数据,都是自己一些小程序,没有界面,不算是软件,甚至连程序都算不上,只能说是有点小用途的代码而已。这一次,我将全面介绍“带有界面的”、“全Windows式风格的”...
  • 小弟是小白因大作业,在此博文的基础上,我增加了歌曲目录的选择...自动播放的关键在于用线程编程来跳出自动播放在GUI界面设计中死循环! 废话不说,代码如下: main_test.py import tkinter import os from mus.
  • 用python一个程序,然后在命令行上执行,看不到界面(UI),这种程序很常见了,叫命令行程序。然而很多人,特别是不懂程序的人,更需要看到的是一个有界面的,通过鼠标操作的程序,毕竟已经迈进“窗口”的时代,...
  • 可视化界面程序,本来不想,只在console台运行就好,但是后来很小伙伴都有这样的需求:需要从redis中删除某个key的value,然后需要跟key去查,有些小伙伴不会用redis,就产生如下的产物。可以看出main后面的tk...
  • Python和VB都是让人快乐的编程语言,我使用了Python之后,很自己使用的工具都使用Python开发或改写了,因为最终实现的Python代码实在太短了(相比VB),有时候Python一行代码就可以实现VB一个函数的功能。...
  • 帮同学给他的闹钟加了一个GUI界面到后面,几乎是推倒重来,还是学到一些东西,after循环怎么关闭始终是一个难题,当时我想的是用一个while循环,但是while循环一直都会崩溃,我也不是很清楚其原理,我个人觉得...
  • tkinter+sqlite3学生信息管理系统

    千次阅读 多人点赞 2020-06-19 14:58:54
    完趁热打铁个博客记录一下,初学很东西不懂,也可能有错误的地方,仅是做个记录。 1 数据库 1.1 用户信息(pickle) pickle的使用 用户信息存储用到了pickle模板,在登录时输入用户名和密码(均不为空)...
  • 一方面,想一个较简单、内含功能较tkinter窗口,以便将来给其他程序作模板用。另一方面,用命令行打包exe太麻烦了。 界面预览 若文件地址是手动输入,则点击‘确认’后点击‘打包’。 若文件地址是点击‘选择...
  • 现在打开文件夹之后 很地方显示乱码,而且打开某一个文件并不是马上显示出文件夹中所有文件,需要点进去再退回来才能显示。 现在程序应该有不少错误,一点点改吧。 首先请问大神 怎么解决中文乱码问题? 在...
  • you-get 库是命令行下非常常用的下载视频的命令,但是由于很时候很多人总是会来问你,B 站视频怎么下载,啥啥啥视频怎么下载,你帮我下载一下… 苦于这么频繁的被问所以索性个简单的 UI 界面打包一下 you-...
  • 听说Qt designer和PyQt5结合起来代码会非常方便,做出来的界面tkinter和wxPython好看许多,于是决定试一试。我在文件加载部分参照了《Python Qt GUI快速编程》的代码,为了保存文件加粗、颜色等.txt不保存的...
  • 用python从头开始学做游戏介绍思路开始界面tkinter自我烦恼于界面刷新Entry的简单代码示例 获取数据messageBox窗口部件贪吃蛇???? 介绍 感觉现在游戏不好玩,自己写写,如果有人看欢迎讨论指点。 非计算机专业,...
  • 是用wxPython一个2048的小游戏,游戏界面如下图所示: 这个游戏2-3年前非常流行,于是我就跟着教程做了一下(tkinter界面太丑了,听说wxpython比tkinter好看很)。过程一切顺利,尤其是做到计算器的时候,...
  • 这几天学了python的一点网络编程和Tkinter的GUI界面编程,今天大体用一下,编一个简单的双工的聊天软件,当然功能是再简单不过了,只是收发消息,显示消息而已,就当玩玩了,目前了一点点代码,基本实现收消息...
  • 0.背景 用selenium了一个爬虫,...没有GUI程序不会显示的,用tkinter写就好。 十五分钟速成了一下,虽然碰到很bug但此处就不赘述了。夸一下tk简单,用,快速。 2.创建虚拟环境 如果在平时的环境中打包 p..
  • # Tkinter写的显示界面,已调试 class Application: def __init__(self): self.window = Tk() self.news = News() # 生成软件图形界面 # 设置窗口名称 self.window.title(u'币圈最新资讯&#...
  • config:logging 模块作者的分级配置模块。 ConfigObj:INI 文件解析器,带验证功能。 ConfigParser:(Python 标准库) INI 文件解析器。 profig:通过多种格式进行配置,具有数值转换功能。 python-decouple:...

空空如也

空空如也

1 2
收藏数 31
精华内容 12
关键字:

tkinter能写多界面