What is Python
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python is simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.
Python Tkinter Listbox bind
Python
Python provides various options for developing graphical user interfaces (GUIs). Most important are listed below.
    Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. We would look this option in this chapter.
Tkinter Programming
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps
    -Import the Tkinter module.
    -Create the GUI application main window.
    -Add one or more of the above-mentioned widgets to the GUI application.
    -Enter the main event loop to take action against each event triggered by the user.
Python Listbox bind is used to set an action that will occur when the event is performed.Any activity that happened on the Listbox widget is called an event.focus-gained, focus-lost, clicked, etc are a few examples of events.In this section, we will learn about a few popular bind events:"ListboxSelect" : It tells about when user selected or deselected Listbox item(s)"Double-1": It triggers activity when the user double clicks on an item(s) in a Listbox.
Code
In this code, ListboxSelect is used to bind the event with callback function. This code returns the index number of the selected Listbox item(s).
from tkinter import *
import pygame
from tkinter import filedialog
root= Tk()
root.title('reanits.blogspot.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)
def addsongsmusic():
  songs = filedialog.askopenfilenames(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
  for song in songs:
  song = song.replace("D:/Python/Test2/sound/", "")
  song = song.replace(".mp3", "")
  songbox.insert(END, song)
def playmusic():
  song = songbox.get(ACTIVE)
  song = f'D:/Python/Test2/sound/{song}.mp3'
  pygame.mixer.music.load(song)
  pygame.mixer.music.play(loops=0)
def stopmusic():
  pygame.mixer.music.stop()
  songbox.select_clear(ACTIVE)
def nextmusic():
  nextone = songbox.curselection()
  nextone = nextone[0]+1
  song = songbox.get(nextone)
  song = f'D:/Python/Test2/sound/{song}.mp3'
  pygame.mixer.music.load(song)
  pygame.mixer.music.play(loops=0)
  songbox.select_clear(0,END)
  songbox.activate(nextone)
  songbox.selection_set(nextone, last=None)
global paused
paused = False
def pause(is_paused):
  global paused
  paused = is_paused
  if paused:
    pygame.mixer.music.unpause()
    paused = False
  else:
    pygame.mixer.music.pause()
    paused = True
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,command=nextmusic)
playbutton= Button(controlframe, image=playbtn, borderwidth=0, command=playmusic)
pausebutton= Button(controlframe, image=pausebtn, borderwidth=0, command=lambda: pause(paused))
stopbutton= Button(controlframe, image=stopbtn, borderwidth=0, command=stopmusic)
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)
#create Menu
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)
addsong.add_command(label="Open Many Songs or Playlist",command=addsongsmusic)
root.mainloop()
Please watching My Video is Below
បងខ្ញុំចូលចិត្តភាសាPython។ សូមបងជួយធ្វើវីដេអូបង្រៀនជាProjects ដើម្បីអាចឲ្យប្រើភាសាហ្នឹងភ្ជាប់ជាមួយDatabaseផងបង (MangoDB / Firebase), អរគុណច្រើនទុកជាមុនបង.. ។
ReplyDelete