Create Menu and Add Sound in ListBox Python MP3 Player - learnit

Home Top Ad

Post Top Ad

Friday, April 30, 2021

Create Menu and Add Sound in ListBox Python MP3 Player

Create Menu and Add Sound in ListBox Python MP3 Player

Create Menu and Add Sound in ListBox Python MP3 Player

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language


Why to Learn Python?

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.


Python is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Web Development Domain. I will list down some of the key advantages of learning Python

Python Tkinter – ListBox

Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. The ListBox widget is used to display different types of items. These items must be of the same type of font and having the same font color. The items must also be of Text type. The user can select one or more items from the given list according to the requirement.


Syntax

listbox = Listbox(root, bg, fg, bd, height, width, font, ..)


Optional parameters

-root – root window.

-bg – background colour

-fg – foreground colour

-bd – border

-highlightcolor – The colour of the list items when focused.

-height – height of the widget

-font – Font type of the text

-cursor – The cursor on the widget which can be an arrow, a dot etc.

-yscrollcommand – for scrolling vertically.

-width – width of the widget

-xscrollcommand – for scrolling horizontally


Common methods

-yviewallows the widget to be vertically scrollable.

-xview – allows the widget to be horizontally scrollable

-curseselection() – returns a tuple for all the line numbers that are being selected.

-activate(index) – to select the lines with a specified index.

-nearest(y) – returns the index of the nearest line.

-size() – return the number of lines present

-delete(start, last) – delete lines in the specified range


tkinter filedialog

Python Tkinter (and TK) offer a set of dialogs that you can use when working with files. By using these you don’t have to design standard dialogs your self


File dialogs help you open, save files or directories. This is the type of dialog you get when you click file,open. This dialog comes out of the module, there’s no need to write all the code manually.

Example

from tkinter import *
import pygame
from tkinter import filedialog
root= Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
def addsongmusic():
    song = filedialog.askopenfilename(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
    song = song.replace("D:/Python/Test2/sound/","")
    song = song.replace(".mp3", "")
    songbox.insert(END,song)
songbox= Listbox(root, bg="black", fg="green", width=80,selectbackground="gray",selectforeground="black")
songbox.pack(pady=20)
backbtn= PhotoImage(file='image/forward101.png')
forwardbtn= PhotoImage(file='image/forward101.png')
playbtn= PhotoImage(file='image/Play101.png')
pausebtn= PhotoImage(file='image/pause101.png')
stopbtn= PhotoImage(file='image/Stop101.png')
controlframe= Frame(root)
controlframe.pack()
backbutton= Button(controlframe, image=backbtn, borderwidth=0)
frowardbutton= Button(controlframe, image=forwardbtn, borderwidth=0)
playbutton= Button(controlframe, image=playbtn, borderwidth=0)
pausebutton= Button(controlframe, image=pausebtn, borderwidth=0)
stopbutton= Button(controlframe, image=stopbtn, borderwidth=0)
backbutton.grid(row=0, column=1, padx=10)
frowardbutton.grid(row=0, column=2, padx=20)
playbutton.grid(row=0, column=3, padx=20)
pausebutton.grid(row=0, column=4, padx=20)
stopbutton.grid(row=0, column=5, padx=20)
mymenu = Menu(root)
root.config(menu=mymenu)
addsong = Menu(mymenu)
mymenu.add_cascade(label="file", menu=addsong)
addsong.add_command(label="Open Song or Playlist",command=addsongmusic)
root.mainloop()

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad