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 Scale
The Scale widget provides a graphical slider object that allows you to select values from a specific scale.
Syntax
Here is the simple syntax to create this widget –
w = Scale ( master, option, ... )
Parameters
1.master − This represents the parent window.
2.options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
No | Descripton |
---|---|
1 | activebackground The background color when the mouse is over the scale. |
2 | Bg The background color of the parts of the widget that are outside the trough. |
3 | Bd Width of the 3-d border around the trough and slider. Default is 2 pixels. |
4 | Command A procedure to be called every time the slider is moved. This procedure will be passed one argument, the new scale value. If the slider is moved rapidly, you may not get a callback for every possible position, but you'll certainly get a callback when it settles. |
5 | Cursor If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the scale. |
6 | Digits The way your program reads the current value shown in a scale widget is through a control variable. The control variable for a scale can be an IntVar, a DoubleVar (float), or a StringVar. If it is a string variable, the digits option controls how many digits to use when the numeric scale value is converted to a string. |
7 | Font The font used for the label and annotations. |
8 | Fg The color of the text used for the label and annotations. |
9 | from_ A float or integer value that defines one end of the scale's range. |
10 | highlightbackground The color of the focus highlight when the scale does not have focus. |
11 | Highlightcolor The color of the focus highlight when the scale has the focus. |
12 | Label You can display a label within the scale widget by setting this option to the label's text. The label appears in the top left corner if the scale is horizontal, or the top right corner if vertical. The default is no label. |
13 | The length of the scale widget. This is the x dimension if the scale is horizontal, or the y dimension if vertical. The default is 100 pixels. |
14 | Orient Set orient=HORIZONTAL if you want the scale to run along the x dimension, or orient=VERTICAL to run parallel to the y-axis. Default is horizontal. |
15 | Relief Specifies the appearance of a decorative border around the label. The default is FLAT; for other values. |
16 | Repeatdelay This option controls how long button 1 has to be held down in the trough before the slider starts moving in that direction repeatedly. Default is repeatdelay=300, and the units are milliseconds. |
17 | Resolution Normally, the user will only be able to change the scale in whole units. Set this option to some other value to change the smallest increment of the scale's value. For example, if from_=-1.0 and to=1.0, and you set resolution=0.5, the scale will have 5 possible values: -1.0, -0.5, 0.0, +0.5, and +1.0. |
18 | Showvalue Normally, the current value of the scale is displayed in text form by the slider (above it for horizontal scales, to the left for vertical scales). Set this option to 0 to suppress that label. |
19 | Sliderlength Normally the slider is 30 pixels along the length of the scale. You can change that length by setting the sliderlength option to your desired length. |
20 | State Normally, scale widgets respond to mouse events, and when they have the focus, also keyboard events. Set state=DISABLED to make the widget unresponsive. |
21 | Takefocus Normally, the focus will cycle through scale widgets. Set this option to 0 if you don't want this behavior. |
22 | Tickinterval To display periodic scale values, set this option to a number, and ticks will be displayed on multiples of that value. For example, if from_=0.0, to=1.0, and tickinterval=0.25, labels will be displayed along the scale at values 0.0, 0.25, 0.50, 0.75, and 1.00. These labels appear below the scale if horizontal, to its left if vertical. Default is 0, which suppresses display of ticks. |
23 | To A float or integer value that defines one end of the scale's range; the other end is defined by the from_ option, discussed above. The to value can be either greater than or less than the from_ value. For vertical scales, the to value defines the bottom of the scale; for horizontal scales, the right end. |
24 | Troughcolor The color of the trough. |
25 | Variable The control variable for this scale, if any. Control variables may be from class IntVar, DoubleVar (float), or StringVar. In the latter case, the numerical value will be converted to a string. |
26 | Width The width of the trough part of the widget. This is the x dimension for vertical scales and the y dimension if the scale has orient=HORIZONTAL. Default is 15 pixels. |
Method
Scale objects have these methods –
No | Method & Description |
---|---|
1 | get() This method returns the current value of the scale. |
2 | set ( value ) Sets the scale's value. |
Example
from tkinter import *
import pygame
from tkinter import filedialog
import time
from mutagen.mp3 import MP3
import tkinter.ttk as ttk
root= Tk()
root.title('reanits.blogspot.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
def playtime():
  Currenttime = pygame.mixer.music.get_pos()/1000
  sliderlabel.config(text=f'Slider:{int(myslider.get())} and SongPos:{int(Currenttime)}')
 ConvertCurrentTime = time.strftime('%M:%S',time.gmtime(Currenttime))
#Currentsong = songbox.curselection()
  song = songbox.get(ACTIVE)
 song = f'D:/Python/Test2/sound/{song}.mp3'
 songmut = MP3(song)
  global SongLength
  SongLength=songmut.info.length
  ConvertSongLength = time.strftime('%M:%S', time.gmtime(SongLength))
  Statusbar.config(text=f'Time Elasped: {ConvertCurrentTime} of {ConvertSongLength} ')
  myslider.config(to=SliderPosition, value=int(Currenttime))
  statusbar.after(1000,playtime)
def deletesongmusic():
  songbox.delete(ANCHOR)
  pygame.mixer.music.stop()
def deletesongsmusics():
  songbox.delete(0,END)
  pygame.mixer.music.stop()
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)
  playtime()
  #SliderPosition = int(SongLength)
  #myslider.config(to=SliderPosition, value=0)
def stopmusic():
  pygame.mixer.music.stop()
  songbox.select_clear(ACTIVE)
  statusbar.config(text='')
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)
def preback():
  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
def slider(x):
  #sliderlabel.config(text=f'{int(myslider.get())} of {int(SongLength)}')
  song = songbox.get(ACTIVE)
  song = f'D:/Python/Test2/sound/{song}.mp3'
  pygame.mixer.music.load(song)
  pygame.mixer.music.play(loops=0, start=int(myslider.get()))
  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, command=preback)
  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)
  deletesong = Menu(mymenu)
  mymenu.add_cascade(label="Delete", menu=deletesong)
  deletesong.add_command(label="Delete Song or Playlist",command=deletesongmusic)
  deletesong.add_command(label="Delete All Songs or Playlist",command=deletesongsmusics)
  statusbar= Label(root, text='',bd=1, relief=GROOVE, anchor=E)
  statusbar.pack(fill=X,side=BOTTOM, ipady=2)
  myslider = ttk.Scale(root,from_=0, to=100, orient=HORIZONTAL, value=0, command=slider, length=500)
  myslider.pack(pady=30)
  sliderlabel = Label(root, text="0")
  sliderlabel.pack(pady=10)
  root.mainloop()
Please Watching My Video is Below
No comments:
Post a Comment