Kivy button implementation - how can I do that with my written code?












0















I'm newbie in python and kivy programming and to learn a bit more I've tried to make a small music player - it plays sound if I choose from balk list next to the pane - but I can't get that pane itself to work - I need some explanation how to achieve that and the similair subjects aren't very helpful. I've tried to get exactly 4 buttons in grip layout to work - but without any effect.Here is my code in pythonkivy:



import kivy
kivy.require('1.10.0')
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from os import listdir, path

Builder.load_file("views/theme.kv")

class Player(Widget):
directory = "" #directory path;
currentlyplaying = "" #now playing song;

def Gettrack_path(self):
try:
track = open("path/path.dat", "r")
self.ids.direct.text = str(track.readline())
track.close()
self.ids.searchBtn.text = "Search"
self.load_tracks()
except:
self.ids.direct.text = ''

def Savetrack_path(self, path):
track = open("path/path.dat", "w")
track.write(path)
track.close()

def select(self, path):
self.directory = path
self.ids.direct.text - self.directory
self.ids.searchBtn.text = "Search tracks"
self.Savetrack_path(self.directory)
self.load_tracks()

def load_tracks(self):
tracks =
self.directory = self.ids.direct.text
if not self.directory.endswith("/"):
self.directory += "/"

if not path.exists(self.directory):
self.ids.status.text = "Given path doesn't exist"
self.ids.status.color = (1,0,0,1)
else:
self.ids.status.text = ""
self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

for file in listdir(self.directory):
if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
tracks.append(file)
if tracks == and self.directory != "":
self.ids.status.text = "No tracks found"
self.ids.status.color = (1,0,0,1)
if self.directory == "/" or self.directory == "":
try:
self.ids.status.text = "No tracks found - given path doesn't exist"
self.ids.status.color = (1,0,0,1)
except:
self.ids.status.text ="Error loading path"
self.ids.status.color = (1,0,0,1)
self.directory = "/"
tracks.sort()

for track in tracks:
def playTrack(bt):
try:
self.currentlyplaying.stop()
except:
pass
finally:
if track.endswith(".mp3"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wav"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aiff"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aac"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".ogg"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".mp4"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wma"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text

btn = Button(text = track[:-4], on_press = playTrack)
self.ids.scroll.add_widget(btn)
if tracks.index(track) % 2 == 0:
btn.background_color = (88, 44, 234, 0.6)
else:
btn.background_color = (37, 44, 249, 0.6)
self.soundplayer(*tracks)
def soundplayer(sound):
if self.ids.play.state == "down":
playsound = SoundLoader.load(sound)
playsound.play()



class PyPlayer(App):
def build(self):
player = Player()
player.Gettrack_path()
return player
PyPlayer().run()


And there is .kv file



    #:kivy 1.10
<Player>:

canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size

TextInput:
id: direct
pos: 0,root.top-35
size: (root.width * 0.3),35
hint_text: 'Browse'

Button:
id: searchBtn
size: (root.width * 0.1),36
background_color: 1, 0, 0, 22
pos: root.width * 0.3, root.top-35
on_release: root.load_tracks()

ScrollView:
size_hint: None, None
size: (root.width *0.4), root.height - 45
pos: 0, 0
GridLayout:
id: scroll
cols: 1
spacing: 10
size_hint_y: None
row_force_default: True
row_default_height: 40

GridLayout:
rows: 1
pos: (root.width * 0.4), root.height - 135
size: (root.width * 0.6), 50
Button:
id:previous
background_color: 0, 0, 0, 1
Image:
source: "backward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:pause
background_color: 0, 0, 0, 1
Image:
source: "pause.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:play
on_press: root.soundplayer()
background_color: 0, 0, 0, 1
Image:
source: "play.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size

Button:
id:forward
background_color: 0, 0, 0, 1

Image:
source: "forward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id: nowplay
text: 'Currently Playing'
pos: (root.width * 0.4),root.height - 85
size: (root.width * 0.6), 50
background_color: 0, 0, 0, 0

Label:
id: status
text: ''
pos: root.width * 0.15, root.top * 0.5


It's my first question here so please guys understand that I don't have experience yet










share|improve this question























  • Not sure what you are asking. I see you have defined 4 buttons in a GridLayout, but only assigned an on_press action to one of them. So the three without any action defined will have no effect. Please be clear about what your question is.

    – John Anderson
    Nov 15 '18 at 3:06











  • I know that must use on_press at each of those buttons but my problem is to how to write a correct functions to them - my buttons are as follows previous song, pause, play and next song - I thinking myself how to do that but l'm failing badly every try

    – AnotherNewbie
    Nov 16 '18 at 6:14











  • So you start by writing a method in Player for each of those buttons. Use the documentation for help. For example, the pause method would probably call the stop() method from Sound and maybe the get_pos() method if you want play to continue from the paused point, The previous and next methods are simply stopping any currently playing sound and calling SoundLoader on the next or previous track. You will probably need your tracks list to be an instance variable instead of local to load_tracks.

    – John Anderson
    Nov 16 '18 at 15:24













  • Thanks John I'll immediately go try your tips into my code - I didn't think about those methods and I was trying to write functions direct into "load_tracks" - I see my mistakes now. Thank you for your time and have nice weekend. I'll let you know when I'm done.

    – AnotherNewbie
    Nov 16 '18 at 19:50
















0















I'm newbie in python and kivy programming and to learn a bit more I've tried to make a small music player - it plays sound if I choose from balk list next to the pane - but I can't get that pane itself to work - I need some explanation how to achieve that and the similair subjects aren't very helpful. I've tried to get exactly 4 buttons in grip layout to work - but without any effect.Here is my code in pythonkivy:



import kivy
kivy.require('1.10.0')
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from os import listdir, path

Builder.load_file("views/theme.kv")

class Player(Widget):
directory = "" #directory path;
currentlyplaying = "" #now playing song;

def Gettrack_path(self):
try:
track = open("path/path.dat", "r")
self.ids.direct.text = str(track.readline())
track.close()
self.ids.searchBtn.text = "Search"
self.load_tracks()
except:
self.ids.direct.text = ''

def Savetrack_path(self, path):
track = open("path/path.dat", "w")
track.write(path)
track.close()

def select(self, path):
self.directory = path
self.ids.direct.text - self.directory
self.ids.searchBtn.text = "Search tracks"
self.Savetrack_path(self.directory)
self.load_tracks()

def load_tracks(self):
tracks =
self.directory = self.ids.direct.text
if not self.directory.endswith("/"):
self.directory += "/"

if not path.exists(self.directory):
self.ids.status.text = "Given path doesn't exist"
self.ids.status.color = (1,0,0,1)
else:
self.ids.status.text = ""
self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

for file in listdir(self.directory):
if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
tracks.append(file)
if tracks == and self.directory != "":
self.ids.status.text = "No tracks found"
self.ids.status.color = (1,0,0,1)
if self.directory == "/" or self.directory == "":
try:
self.ids.status.text = "No tracks found - given path doesn't exist"
self.ids.status.color = (1,0,0,1)
except:
self.ids.status.text ="Error loading path"
self.ids.status.color = (1,0,0,1)
self.directory = "/"
tracks.sort()

for track in tracks:
def playTrack(bt):
try:
self.currentlyplaying.stop()
except:
pass
finally:
if track.endswith(".mp3"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wav"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aiff"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aac"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".ogg"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".mp4"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wma"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text

btn = Button(text = track[:-4], on_press = playTrack)
self.ids.scroll.add_widget(btn)
if tracks.index(track) % 2 == 0:
btn.background_color = (88, 44, 234, 0.6)
else:
btn.background_color = (37, 44, 249, 0.6)
self.soundplayer(*tracks)
def soundplayer(sound):
if self.ids.play.state == "down":
playsound = SoundLoader.load(sound)
playsound.play()



class PyPlayer(App):
def build(self):
player = Player()
player.Gettrack_path()
return player
PyPlayer().run()


And there is .kv file



    #:kivy 1.10
<Player>:

canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size

TextInput:
id: direct
pos: 0,root.top-35
size: (root.width * 0.3),35
hint_text: 'Browse'

Button:
id: searchBtn
size: (root.width * 0.1),36
background_color: 1, 0, 0, 22
pos: root.width * 0.3, root.top-35
on_release: root.load_tracks()

ScrollView:
size_hint: None, None
size: (root.width *0.4), root.height - 45
pos: 0, 0
GridLayout:
id: scroll
cols: 1
spacing: 10
size_hint_y: None
row_force_default: True
row_default_height: 40

GridLayout:
rows: 1
pos: (root.width * 0.4), root.height - 135
size: (root.width * 0.6), 50
Button:
id:previous
background_color: 0, 0, 0, 1
Image:
source: "backward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:pause
background_color: 0, 0, 0, 1
Image:
source: "pause.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:play
on_press: root.soundplayer()
background_color: 0, 0, 0, 1
Image:
source: "play.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size

Button:
id:forward
background_color: 0, 0, 0, 1

Image:
source: "forward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id: nowplay
text: 'Currently Playing'
pos: (root.width * 0.4),root.height - 85
size: (root.width * 0.6), 50
background_color: 0, 0, 0, 0

Label:
id: status
text: ''
pos: root.width * 0.15, root.top * 0.5


It's my first question here so please guys understand that I don't have experience yet










share|improve this question























  • Not sure what you are asking. I see you have defined 4 buttons in a GridLayout, but only assigned an on_press action to one of them. So the three without any action defined will have no effect. Please be clear about what your question is.

    – John Anderson
    Nov 15 '18 at 3:06











  • I know that must use on_press at each of those buttons but my problem is to how to write a correct functions to them - my buttons are as follows previous song, pause, play and next song - I thinking myself how to do that but l'm failing badly every try

    – AnotherNewbie
    Nov 16 '18 at 6:14











  • So you start by writing a method in Player for each of those buttons. Use the documentation for help. For example, the pause method would probably call the stop() method from Sound and maybe the get_pos() method if you want play to continue from the paused point, The previous and next methods are simply stopping any currently playing sound and calling SoundLoader on the next or previous track. You will probably need your tracks list to be an instance variable instead of local to load_tracks.

    – John Anderson
    Nov 16 '18 at 15:24













  • Thanks John I'll immediately go try your tips into my code - I didn't think about those methods and I was trying to write functions direct into "load_tracks" - I see my mistakes now. Thank you for your time and have nice weekend. I'll let you know when I'm done.

    – AnotherNewbie
    Nov 16 '18 at 19:50














0












0








0


1






I'm newbie in python and kivy programming and to learn a bit more I've tried to make a small music player - it plays sound if I choose from balk list next to the pane - but I can't get that pane itself to work - I need some explanation how to achieve that and the similair subjects aren't very helpful. I've tried to get exactly 4 buttons in grip layout to work - but without any effect.Here is my code in pythonkivy:



import kivy
kivy.require('1.10.0')
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from os import listdir, path

Builder.load_file("views/theme.kv")

class Player(Widget):
directory = "" #directory path;
currentlyplaying = "" #now playing song;

def Gettrack_path(self):
try:
track = open("path/path.dat", "r")
self.ids.direct.text = str(track.readline())
track.close()
self.ids.searchBtn.text = "Search"
self.load_tracks()
except:
self.ids.direct.text = ''

def Savetrack_path(self, path):
track = open("path/path.dat", "w")
track.write(path)
track.close()

def select(self, path):
self.directory = path
self.ids.direct.text - self.directory
self.ids.searchBtn.text = "Search tracks"
self.Savetrack_path(self.directory)
self.load_tracks()

def load_tracks(self):
tracks =
self.directory = self.ids.direct.text
if not self.directory.endswith("/"):
self.directory += "/"

if not path.exists(self.directory):
self.ids.status.text = "Given path doesn't exist"
self.ids.status.color = (1,0,0,1)
else:
self.ids.status.text = ""
self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

for file in listdir(self.directory):
if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
tracks.append(file)
if tracks == and self.directory != "":
self.ids.status.text = "No tracks found"
self.ids.status.color = (1,0,0,1)
if self.directory == "/" or self.directory == "":
try:
self.ids.status.text = "No tracks found - given path doesn't exist"
self.ids.status.color = (1,0,0,1)
except:
self.ids.status.text ="Error loading path"
self.ids.status.color = (1,0,0,1)
self.directory = "/"
tracks.sort()

for track in tracks:
def playTrack(bt):
try:
self.currentlyplaying.stop()
except:
pass
finally:
if track.endswith(".mp3"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wav"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aiff"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aac"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".ogg"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".mp4"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wma"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text

btn = Button(text = track[:-4], on_press = playTrack)
self.ids.scroll.add_widget(btn)
if tracks.index(track) % 2 == 0:
btn.background_color = (88, 44, 234, 0.6)
else:
btn.background_color = (37, 44, 249, 0.6)
self.soundplayer(*tracks)
def soundplayer(sound):
if self.ids.play.state == "down":
playsound = SoundLoader.load(sound)
playsound.play()



class PyPlayer(App):
def build(self):
player = Player()
player.Gettrack_path()
return player
PyPlayer().run()


And there is .kv file



    #:kivy 1.10
<Player>:

canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size

TextInput:
id: direct
pos: 0,root.top-35
size: (root.width * 0.3),35
hint_text: 'Browse'

Button:
id: searchBtn
size: (root.width * 0.1),36
background_color: 1, 0, 0, 22
pos: root.width * 0.3, root.top-35
on_release: root.load_tracks()

ScrollView:
size_hint: None, None
size: (root.width *0.4), root.height - 45
pos: 0, 0
GridLayout:
id: scroll
cols: 1
spacing: 10
size_hint_y: None
row_force_default: True
row_default_height: 40

GridLayout:
rows: 1
pos: (root.width * 0.4), root.height - 135
size: (root.width * 0.6), 50
Button:
id:previous
background_color: 0, 0, 0, 1
Image:
source: "backward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:pause
background_color: 0, 0, 0, 1
Image:
source: "pause.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:play
on_press: root.soundplayer()
background_color: 0, 0, 0, 1
Image:
source: "play.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size

Button:
id:forward
background_color: 0, 0, 0, 1

Image:
source: "forward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id: nowplay
text: 'Currently Playing'
pos: (root.width * 0.4),root.height - 85
size: (root.width * 0.6), 50
background_color: 0, 0, 0, 0

Label:
id: status
text: ''
pos: root.width * 0.15, root.top * 0.5


It's my first question here so please guys understand that I don't have experience yet










share|improve this question














I'm newbie in python and kivy programming and to learn a bit more I've tried to make a small music player - it plays sound if I choose from balk list next to the pane - but I can't get that pane itself to work - I need some explanation how to achieve that and the similair subjects aren't very helpful. I've tried to get exactly 4 buttons in grip layout to work - but without any effect.Here is my code in pythonkivy:



import kivy
kivy.require('1.10.0')
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from os import listdir, path

Builder.load_file("views/theme.kv")

class Player(Widget):
directory = "" #directory path;
currentlyplaying = "" #now playing song;

def Gettrack_path(self):
try:
track = open("path/path.dat", "r")
self.ids.direct.text = str(track.readline())
track.close()
self.ids.searchBtn.text = "Search"
self.load_tracks()
except:
self.ids.direct.text = ''

def Savetrack_path(self, path):
track = open("path/path.dat", "w")
track.write(path)
track.close()

def select(self, path):
self.directory = path
self.ids.direct.text - self.directory
self.ids.searchBtn.text = "Search tracks"
self.Savetrack_path(self.directory)
self.load_tracks()

def load_tracks(self):
tracks =
self.directory = self.ids.direct.text
if not self.directory.endswith("/"):
self.directory += "/"

if not path.exists(self.directory):
self.ids.status.text = "Given path doesn't exist"
self.ids.status.color = (1,0,0,1)
else:
self.ids.status.text = ""
self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

for file in listdir(self.directory):
if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
tracks.append(file)
if tracks == and self.directory != "":
self.ids.status.text = "No tracks found"
self.ids.status.color = (1,0,0,1)
if self.directory == "/" or self.directory == "":
try:
self.ids.status.text = "No tracks found - given path doesn't exist"
self.ids.status.color = (1,0,0,1)
except:
self.ids.status.text ="Error loading path"
self.ids.status.color = (1,0,0,1)
self.directory = "/"
tracks.sort()

for track in tracks:
def playTrack(bt):
try:
self.currentlyplaying.stop()
except:
pass
finally:
if track.endswith(".mp3"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wav"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aiff"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".aac"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".ogg"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".mp4"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text
if track.endswith(".wma"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
self.currentlyplaying.play()
self.ids.nowplay.text = bt.text

btn = Button(text = track[:-4], on_press = playTrack)
self.ids.scroll.add_widget(btn)
if tracks.index(track) % 2 == 0:
btn.background_color = (88, 44, 234, 0.6)
else:
btn.background_color = (37, 44, 249, 0.6)
self.soundplayer(*tracks)
def soundplayer(sound):
if self.ids.play.state == "down":
playsound = SoundLoader.load(sound)
playsound.play()



class PyPlayer(App):
def build(self):
player = Player()
player.Gettrack_path()
return player
PyPlayer().run()


And there is .kv file



    #:kivy 1.10
<Player>:

canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size

TextInput:
id: direct
pos: 0,root.top-35
size: (root.width * 0.3),35
hint_text: 'Browse'

Button:
id: searchBtn
size: (root.width * 0.1),36
background_color: 1, 0, 0, 22
pos: root.width * 0.3, root.top-35
on_release: root.load_tracks()

ScrollView:
size_hint: None, None
size: (root.width *0.4), root.height - 45
pos: 0, 0
GridLayout:
id: scroll
cols: 1
spacing: 10
size_hint_y: None
row_force_default: True
row_default_height: 40

GridLayout:
rows: 1
pos: (root.width * 0.4), root.height - 135
size: (root.width * 0.6), 50
Button:
id:previous
background_color: 0, 0, 0, 1
Image:
source: "backward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:pause
background_color: 0, 0, 0, 1
Image:
source: "pause.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:play
on_press: root.soundplayer()
background_color: 0, 0, 0, 1
Image:
source: "play.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size

Button:
id:forward
background_color: 0, 0, 0, 1

Image:
source: "forward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id: nowplay
text: 'Currently Playing'
pos: (root.width * 0.4),root.height - 85
size: (root.width * 0.6), 50
background_color: 0, 0, 0, 0

Label:
id: status
text: ''
pos: root.width * 0.15, root.top * 0.5


It's my first question here so please guys understand that I don't have experience yet







python function button kivy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 15:21









AnotherNewbieAnotherNewbie

64




64













  • Not sure what you are asking. I see you have defined 4 buttons in a GridLayout, but only assigned an on_press action to one of them. So the three without any action defined will have no effect. Please be clear about what your question is.

    – John Anderson
    Nov 15 '18 at 3:06











  • I know that must use on_press at each of those buttons but my problem is to how to write a correct functions to them - my buttons are as follows previous song, pause, play and next song - I thinking myself how to do that but l'm failing badly every try

    – AnotherNewbie
    Nov 16 '18 at 6:14











  • So you start by writing a method in Player for each of those buttons. Use the documentation for help. For example, the pause method would probably call the stop() method from Sound and maybe the get_pos() method if you want play to continue from the paused point, The previous and next methods are simply stopping any currently playing sound and calling SoundLoader on the next or previous track. You will probably need your tracks list to be an instance variable instead of local to load_tracks.

    – John Anderson
    Nov 16 '18 at 15:24













  • Thanks John I'll immediately go try your tips into my code - I didn't think about those methods and I was trying to write functions direct into "load_tracks" - I see my mistakes now. Thank you for your time and have nice weekend. I'll let you know when I'm done.

    – AnotherNewbie
    Nov 16 '18 at 19:50



















  • Not sure what you are asking. I see you have defined 4 buttons in a GridLayout, but only assigned an on_press action to one of them. So the three without any action defined will have no effect. Please be clear about what your question is.

    – John Anderson
    Nov 15 '18 at 3:06











  • I know that must use on_press at each of those buttons but my problem is to how to write a correct functions to them - my buttons are as follows previous song, pause, play and next song - I thinking myself how to do that but l'm failing badly every try

    – AnotherNewbie
    Nov 16 '18 at 6:14











  • So you start by writing a method in Player for each of those buttons. Use the documentation for help. For example, the pause method would probably call the stop() method from Sound and maybe the get_pos() method if you want play to continue from the paused point, The previous and next methods are simply stopping any currently playing sound and calling SoundLoader on the next or previous track. You will probably need your tracks list to be an instance variable instead of local to load_tracks.

    – John Anderson
    Nov 16 '18 at 15:24













  • Thanks John I'll immediately go try your tips into my code - I didn't think about those methods and I was trying to write functions direct into "load_tracks" - I see my mistakes now. Thank you for your time and have nice weekend. I'll let you know when I'm done.

    – AnotherNewbie
    Nov 16 '18 at 19:50

















Not sure what you are asking. I see you have defined 4 buttons in a GridLayout, but only assigned an on_press action to one of them. So the three without any action defined will have no effect. Please be clear about what your question is.

– John Anderson
Nov 15 '18 at 3:06





Not sure what you are asking. I see you have defined 4 buttons in a GridLayout, but only assigned an on_press action to one of them. So the three without any action defined will have no effect. Please be clear about what your question is.

– John Anderson
Nov 15 '18 at 3:06













I know that must use on_press at each of those buttons but my problem is to how to write a correct functions to them - my buttons are as follows previous song, pause, play and next song - I thinking myself how to do that but l'm failing badly every try

– AnotherNewbie
Nov 16 '18 at 6:14





I know that must use on_press at each of those buttons but my problem is to how to write a correct functions to them - my buttons are as follows previous song, pause, play and next song - I thinking myself how to do that but l'm failing badly every try

– AnotherNewbie
Nov 16 '18 at 6:14













So you start by writing a method in Player for each of those buttons. Use the documentation for help. For example, the pause method would probably call the stop() method from Sound and maybe the get_pos() method if you want play to continue from the paused point, The previous and next methods are simply stopping any currently playing sound and calling SoundLoader on the next or previous track. You will probably need your tracks list to be an instance variable instead of local to load_tracks.

– John Anderson
Nov 16 '18 at 15:24







So you start by writing a method in Player for each of those buttons. Use the documentation for help. For example, the pause method would probably call the stop() method from Sound and maybe the get_pos() method if you want play to continue from the paused point, The previous and next methods are simply stopping any currently playing sound and calling SoundLoader on the next or previous track. You will probably need your tracks list to be an instance variable instead of local to load_tracks.

– John Anderson
Nov 16 '18 at 15:24















Thanks John I'll immediately go try your tips into my code - I didn't think about those methods and I was trying to write functions direct into "load_tracks" - I see my mistakes now. Thank you for your time and have nice weekend. I'll let you know when I'm done.

– AnotherNewbie
Nov 16 '18 at 19:50





Thanks John I'll immediately go try your tips into my code - I didn't think about those methods and I was trying to write functions direct into "load_tracks" - I see my mistakes now. Thank you for your time and have nice weekend. I'll let you know when I'm done.

– AnotherNewbie
Nov 16 '18 at 19:50












1 Answer
1






active

oldest

votes


















0














That took not so long but I forgot to post my code - maybe somebody will learn or reuse my answer in own project:



import kivy
kivy.require('1.10.0')
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.properties import NumericProperty
from kivy.uix.slider import Slider
from os import listdir, path

Builder.load_file("views/theme.kv")

class Player(Widget):
directory = "" #directory path;
currentlyplaying = "" #now playing song;
position = 0
volume = 0
maxvolume = NumericProperty(1.0)
tracks =
def Gettrack_path(self):
try:
track = open("path/path.dat", "r")
self.ids.direct.text = str(track.readline())
track.close()
self.ids.searchBtn.text = "Search"
self.load_tracks()
except:
self.ids.direct.text = ''

def Savetrack_path(self, path):
track = open("path/path.dat", "w")
track.write(path)
track.close()

def load_tracks(self):
self.directory = self.ids.direct.text
if not self.directory.endswith("\"):
self.directory += "\"

if not path.exists(self.directory):
self.ids.status.text = "Given path doesn't exist"
self.ids.status.color = (1,0,0,1)
else:
self.ids.status.text = ""
self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

for file in listdir(self.directory):
if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
self.tracks.append(file)
if self.tracks == and self.directory != "":
self.ids.status.text = "No tracks found"
self.ids.status.color = (1,0,0,1)
if self.directory == "\" or self.directory == "":
try:
self.ids.status.text = "No tracks found - given path doesn't exist"
self.ids.status.color = (1,0,0,1)
except:
self.ids.status.text ="Error loading path"
self.ids.status.color = (1,0,0,1)
self.directory = "\"
self.tracks.sort()
for track in self.tracks:
def playTrack(bt):
try:
self.currentlyplaying.stop()
except:
pass
finally:
if track.endswith(".mp3"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.mp3'
if track.endswith(".wav"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.wav'
if track.endswith(".aiff"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.aiff'
if track.endswith(".aac"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.aac'
if track.endswith(".ogg"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.ogg'
if track.endswith(".mp4"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.mp4'
if track.endswith(".wma"):
self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.ids.nowplay.text = bt.text+'.wma'

btn = Button(text = track[:-4], on_press = playTrack)
self.ids.scroll.add_widget(btn)
if self.tracks.index(track) % 2 == 0:
btn.background_color = (88, 44, 234, 0.6)
else:
btn.background_color = (37, 44, 249, 0.6)
def pause(self):
if self.ids.pause.state == "down":
if self.currentlyplaying == "":
pass
else:
self.position = self.currentlyplaying.get_pos()
self.currentlyplaying.stop()
def play(self):
if self.ids.play.state == "down":
if self.currentlyplaying == "":
pass
if self.currentlyplaying.state == "stop":
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
self.currentlyplaying.seek(self.position - 1)
if self.currentlyplaying.state =="play":
pass
def next(self):
if self.ids.forward.state == "down":
if self.currentlyplaying == "":
pass
i = 0
while(i < len(self.tracks)):
if self.currentlyplaying.filename == self.directory + self.tracks[i]:
self.currentlyplaying.stop()
self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i+1])
self.ids.nowplay.text = self.tracks[i+1]
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
break
elif self.currentlyplaying.filename == self.directory + self.tracks[-1]:
self.currentlyplaying.stop()
self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[0])
self.ids.nowplay.text = self.tracks[0]
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
break
i += 1
def previous(self):
if self.ids.previous.state == "down":
if self.currentlyplaying == "":
pass
i = 0
while(i < len(self.tracks)):
if self.currentlyplaying.filename == self.directory + self.tracks[i]:
self.currentlyplaying.stop()
self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i-1])
self.ids.nowplay.text = self.tracks[i-1]
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
break
elif self.currentlyplaying.filename == self.directory + self.tracks[0]:
self.currentlyplaying.stop()
self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[-1])
self.ids.nowplay.text = self.tracks[-1]
self.currentlyplaying.play()
self.currentlyplaying.volume = self.volume
break
i += 1

def volumectrl(self, *args):
self.volume = int(args[1]) / 100
if self.volume >= int(self.maxvolume):
self.volume = int(self.maxvolume)
self.currentlyplaying.volume = self.volume


class PyPlayer(App):
def build(self):
player = Player()
player.Gettrack_path()
return player
PyPlayer().run()


And of course a .kv file:



#:kivy 1.10
<Player>:
orientation: "horizontal"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "264727.jpg"


TextInput:
id: direct
pos: 0,root.top-35
size: (root.width * 0.3),35
hint_text: 'balk is empty'

Button:
id: searchBtn
size: (root.width * 0.1),36
background_color: 1, 0, 0, 22
pos: root.width * 0.3, root.top-35
on_release: root.load_tracks()

ScrollView:
size_hint: None, None
size: (root.width *0.4), root.height - 45
pos: 0, 0
GridLayout:
id: scroll
cols: 1
spacing: 10
size_hint_y: None
row_force_default: True
row_default_height: 40

GridLayout:
rows: 1
pos: (root.width * 0.4), root.height - 135
size: (root.width * 0.6), 50
Button:
id:previous
background_color: 0, 0, 0, 0
on_press: root.previous()
Image:
source: "backward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:pause
on_press: root.pause()
background_color: 0, 0, 0, 0
Image:
source: "pause.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Button:
id:play
on_press: root.play()
background_color: 0, 0, 0, 0
Image:
source: "play.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size

Button:
on_press: root.next()
id:forward
background_color: 0, 0, 0, 0
Image:
source: "forward.png"
y: self.parent.y + self.parent.height - 75
x: self.parent.x + 10
size: self.size
Slider:
id:volumecontrol
size_hint_y: 0.1
pos: (root.width * 0.4), root.height - 200
size: (root.width * 0.6) - 15, 50
max: 100
min: 0
on_value:root.volumectrl(*args)

Button:
id: nowplay
text: 'Currently Playing'
pos: (root.width * 0.4),root.height - 85
size: (root.width * 0.6), 50
background_color: 0, 0, 0, 0

Label:
id: status
text: ''
pos: root.width * 0.15, root.top * 0.5


What have I changed - at first I replace a list of my tracks to head class - that makes it available for all functions and make possible to write functions to all buttons in my small interface - I've used in kv file a link to background image what makes it a bit prettier, and as bonus I've written a volume control function and I've created a bar for it to read constantly the value whatever it is in range from 0 to 100






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284163%2fkivy-button-implementation-how-can-i-do-that-with-my-written-code%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    That took not so long but I forgot to post my code - maybe somebody will learn or reuse my answer in own project:



    import kivy
    kivy.require('1.10.0')
    from kivy.config import Config
    Config.set('graphics', 'fullscreen', '0')

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.button import Button
    from kivy.uix.widget import Widget
    from kivy.core.audio import SoundLoader
    from kivy.uix.gridlayout import GridLayout
    from kivy.properties import ObjectProperty
    from kivy.properties import NumericProperty
    from kivy.uix.slider import Slider
    from os import listdir, path

    Builder.load_file("views/theme.kv")

    class Player(Widget):
    directory = "" #directory path;
    currentlyplaying = "" #now playing song;
    position = 0
    volume = 0
    maxvolume = NumericProperty(1.0)
    tracks =
    def Gettrack_path(self):
    try:
    track = open("path/path.dat", "r")
    self.ids.direct.text = str(track.readline())
    track.close()
    self.ids.searchBtn.text = "Search"
    self.load_tracks()
    except:
    self.ids.direct.text = ''

    def Savetrack_path(self, path):
    track = open("path/path.dat", "w")
    track.write(path)
    track.close()

    def load_tracks(self):
    self.directory = self.ids.direct.text
    if not self.directory.endswith("\"):
    self.directory += "\"

    if not path.exists(self.directory):
    self.ids.status.text = "Given path doesn't exist"
    self.ids.status.color = (1,0,0,1)
    else:
    self.ids.status.text = ""
    self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

    for file in listdir(self.directory):
    if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
    self.tracks.append(file)
    if self.tracks == and self.directory != "":
    self.ids.status.text = "No tracks found"
    self.ids.status.color = (1,0,0,1)
    if self.directory == "\" or self.directory == "":
    try:
    self.ids.status.text = "No tracks found - given path doesn't exist"
    self.ids.status.color = (1,0,0,1)
    except:
    self.ids.status.text ="Error loading path"
    self.ids.status.color = (1,0,0,1)
    self.directory = "\"
    self.tracks.sort()
    for track in self.tracks:
    def playTrack(bt):
    try:
    self.currentlyplaying.stop()
    except:
    pass
    finally:
    if track.endswith(".mp3"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.mp3'
    if track.endswith(".wav"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.wav'
    if track.endswith(".aiff"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.aiff'
    if track.endswith(".aac"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.aac'
    if track.endswith(".ogg"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.ogg'
    if track.endswith(".mp4"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.mp4'
    if track.endswith(".wma"):
    self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.ids.nowplay.text = bt.text+'.wma'

    btn = Button(text = track[:-4], on_press = playTrack)
    self.ids.scroll.add_widget(btn)
    if self.tracks.index(track) % 2 == 0:
    btn.background_color = (88, 44, 234, 0.6)
    else:
    btn.background_color = (37, 44, 249, 0.6)
    def pause(self):
    if self.ids.pause.state == "down":
    if self.currentlyplaying == "":
    pass
    else:
    self.position = self.currentlyplaying.get_pos()
    self.currentlyplaying.stop()
    def play(self):
    if self.ids.play.state == "down":
    if self.currentlyplaying == "":
    pass
    if self.currentlyplaying.state == "stop":
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    self.currentlyplaying.seek(self.position - 1)
    if self.currentlyplaying.state =="play":
    pass
    def next(self):
    if self.ids.forward.state == "down":
    if self.currentlyplaying == "":
    pass
    i = 0
    while(i < len(self.tracks)):
    if self.currentlyplaying.filename == self.directory + self.tracks[i]:
    self.currentlyplaying.stop()
    self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i+1])
    self.ids.nowplay.text = self.tracks[i+1]
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    break
    elif self.currentlyplaying.filename == self.directory + self.tracks[-1]:
    self.currentlyplaying.stop()
    self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[0])
    self.ids.nowplay.text = self.tracks[0]
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    break
    i += 1
    def previous(self):
    if self.ids.previous.state == "down":
    if self.currentlyplaying == "":
    pass
    i = 0
    while(i < len(self.tracks)):
    if self.currentlyplaying.filename == self.directory + self.tracks[i]:
    self.currentlyplaying.stop()
    self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i-1])
    self.ids.nowplay.text = self.tracks[i-1]
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    break
    elif self.currentlyplaying.filename == self.directory + self.tracks[0]:
    self.currentlyplaying.stop()
    self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[-1])
    self.ids.nowplay.text = self.tracks[-1]
    self.currentlyplaying.play()
    self.currentlyplaying.volume = self.volume
    break
    i += 1

    def volumectrl(self, *args):
    self.volume = int(args[1]) / 100
    if self.volume >= int(self.maxvolume):
    self.volume = int(self.maxvolume)
    self.currentlyplaying.volume = self.volume


    class PyPlayer(App):
    def build(self):
    player = Player()
    player.Gettrack_path()
    return player
    PyPlayer().run()


    And of course a .kv file:



    #:kivy 1.10
    <Player>:
    orientation: "horizontal"
    canvas.before:
    Rectangle:
    pos: self.pos
    size: self.size
    source: "264727.jpg"


    TextInput:
    id: direct
    pos: 0,root.top-35
    size: (root.width * 0.3),35
    hint_text: 'balk is empty'

    Button:
    id: searchBtn
    size: (root.width * 0.1),36
    background_color: 1, 0, 0, 22
    pos: root.width * 0.3, root.top-35
    on_release: root.load_tracks()

    ScrollView:
    size_hint: None, None
    size: (root.width *0.4), root.height - 45
    pos: 0, 0
    GridLayout:
    id: scroll
    cols: 1
    spacing: 10
    size_hint_y: None
    row_force_default: True
    row_default_height: 40

    GridLayout:
    rows: 1
    pos: (root.width * 0.4), root.height - 135
    size: (root.width * 0.6), 50
    Button:
    id:previous
    background_color: 0, 0, 0, 0
    on_press: root.previous()
    Image:
    source: "backward.png"
    y: self.parent.y + self.parent.height - 75
    x: self.parent.x + 10
    size: self.size
    Button:
    id:pause
    on_press: root.pause()
    background_color: 0, 0, 0, 0
    Image:
    source: "pause.png"
    y: self.parent.y + self.parent.height - 75
    x: self.parent.x + 10
    size: self.size
    Button:
    id:play
    on_press: root.play()
    background_color: 0, 0, 0, 0
    Image:
    source: "play.png"
    y: self.parent.y + self.parent.height - 75
    x: self.parent.x + 10
    size: self.size

    Button:
    on_press: root.next()
    id:forward
    background_color: 0, 0, 0, 0
    Image:
    source: "forward.png"
    y: self.parent.y + self.parent.height - 75
    x: self.parent.x + 10
    size: self.size
    Slider:
    id:volumecontrol
    size_hint_y: 0.1
    pos: (root.width * 0.4), root.height - 200
    size: (root.width * 0.6) - 15, 50
    max: 100
    min: 0
    on_value:root.volumectrl(*args)

    Button:
    id: nowplay
    text: 'Currently Playing'
    pos: (root.width * 0.4),root.height - 85
    size: (root.width * 0.6), 50
    background_color: 0, 0, 0, 0

    Label:
    id: status
    text: ''
    pos: root.width * 0.15, root.top * 0.5


    What have I changed - at first I replace a list of my tracks to head class - that makes it available for all functions and make possible to write functions to all buttons in my small interface - I've used in kv file a link to background image what makes it a bit prettier, and as bonus I've written a volume control function and I've created a bar for it to read constantly the value whatever it is in range from 0 to 100






    share|improve this answer




























      0














      That took not so long but I forgot to post my code - maybe somebody will learn or reuse my answer in own project:



      import kivy
      kivy.require('1.10.0')
      from kivy.config import Config
      Config.set('graphics', 'fullscreen', '0')

      from kivy.app import App
      from kivy.lang import Builder
      from kivy.uix.popup import Popup
      from kivy.uix.button import Button
      from kivy.uix.widget import Widget
      from kivy.core.audio import SoundLoader
      from kivy.uix.gridlayout import GridLayout
      from kivy.properties import ObjectProperty
      from kivy.properties import NumericProperty
      from kivy.uix.slider import Slider
      from os import listdir, path

      Builder.load_file("views/theme.kv")

      class Player(Widget):
      directory = "" #directory path;
      currentlyplaying = "" #now playing song;
      position = 0
      volume = 0
      maxvolume = NumericProperty(1.0)
      tracks =
      def Gettrack_path(self):
      try:
      track = open("path/path.dat", "r")
      self.ids.direct.text = str(track.readline())
      track.close()
      self.ids.searchBtn.text = "Search"
      self.load_tracks()
      except:
      self.ids.direct.text = ''

      def Savetrack_path(self, path):
      track = open("path/path.dat", "w")
      track.write(path)
      track.close()

      def load_tracks(self):
      self.directory = self.ids.direct.text
      if not self.directory.endswith("\"):
      self.directory += "\"

      if not path.exists(self.directory):
      self.ids.status.text = "Given path doesn't exist"
      self.ids.status.color = (1,0,0,1)
      else:
      self.ids.status.text = ""
      self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

      for file in listdir(self.directory):
      if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
      self.tracks.append(file)
      if self.tracks == and self.directory != "":
      self.ids.status.text = "No tracks found"
      self.ids.status.color = (1,0,0,1)
      if self.directory == "\" or self.directory == "":
      try:
      self.ids.status.text = "No tracks found - given path doesn't exist"
      self.ids.status.color = (1,0,0,1)
      except:
      self.ids.status.text ="Error loading path"
      self.ids.status.color = (1,0,0,1)
      self.directory = "\"
      self.tracks.sort()
      for track in self.tracks:
      def playTrack(bt):
      try:
      self.currentlyplaying.stop()
      except:
      pass
      finally:
      if track.endswith(".mp3"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.mp3'
      if track.endswith(".wav"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.wav'
      if track.endswith(".aiff"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.aiff'
      if track.endswith(".aac"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.aac'
      if track.endswith(".ogg"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.ogg'
      if track.endswith(".mp4"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.mp4'
      if track.endswith(".wma"):
      self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.ids.nowplay.text = bt.text+'.wma'

      btn = Button(text = track[:-4], on_press = playTrack)
      self.ids.scroll.add_widget(btn)
      if self.tracks.index(track) % 2 == 0:
      btn.background_color = (88, 44, 234, 0.6)
      else:
      btn.background_color = (37, 44, 249, 0.6)
      def pause(self):
      if self.ids.pause.state == "down":
      if self.currentlyplaying == "":
      pass
      else:
      self.position = self.currentlyplaying.get_pos()
      self.currentlyplaying.stop()
      def play(self):
      if self.ids.play.state == "down":
      if self.currentlyplaying == "":
      pass
      if self.currentlyplaying.state == "stop":
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      self.currentlyplaying.seek(self.position - 1)
      if self.currentlyplaying.state =="play":
      pass
      def next(self):
      if self.ids.forward.state == "down":
      if self.currentlyplaying == "":
      pass
      i = 0
      while(i < len(self.tracks)):
      if self.currentlyplaying.filename == self.directory + self.tracks[i]:
      self.currentlyplaying.stop()
      self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i+1])
      self.ids.nowplay.text = self.tracks[i+1]
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      break
      elif self.currentlyplaying.filename == self.directory + self.tracks[-1]:
      self.currentlyplaying.stop()
      self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[0])
      self.ids.nowplay.text = self.tracks[0]
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      break
      i += 1
      def previous(self):
      if self.ids.previous.state == "down":
      if self.currentlyplaying == "":
      pass
      i = 0
      while(i < len(self.tracks)):
      if self.currentlyplaying.filename == self.directory + self.tracks[i]:
      self.currentlyplaying.stop()
      self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i-1])
      self.ids.nowplay.text = self.tracks[i-1]
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      break
      elif self.currentlyplaying.filename == self.directory + self.tracks[0]:
      self.currentlyplaying.stop()
      self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[-1])
      self.ids.nowplay.text = self.tracks[-1]
      self.currentlyplaying.play()
      self.currentlyplaying.volume = self.volume
      break
      i += 1

      def volumectrl(self, *args):
      self.volume = int(args[1]) / 100
      if self.volume >= int(self.maxvolume):
      self.volume = int(self.maxvolume)
      self.currentlyplaying.volume = self.volume


      class PyPlayer(App):
      def build(self):
      player = Player()
      player.Gettrack_path()
      return player
      PyPlayer().run()


      And of course a .kv file:



      #:kivy 1.10
      <Player>:
      orientation: "horizontal"
      canvas.before:
      Rectangle:
      pos: self.pos
      size: self.size
      source: "264727.jpg"


      TextInput:
      id: direct
      pos: 0,root.top-35
      size: (root.width * 0.3),35
      hint_text: 'balk is empty'

      Button:
      id: searchBtn
      size: (root.width * 0.1),36
      background_color: 1, 0, 0, 22
      pos: root.width * 0.3, root.top-35
      on_release: root.load_tracks()

      ScrollView:
      size_hint: None, None
      size: (root.width *0.4), root.height - 45
      pos: 0, 0
      GridLayout:
      id: scroll
      cols: 1
      spacing: 10
      size_hint_y: None
      row_force_default: True
      row_default_height: 40

      GridLayout:
      rows: 1
      pos: (root.width * 0.4), root.height - 135
      size: (root.width * 0.6), 50
      Button:
      id:previous
      background_color: 0, 0, 0, 0
      on_press: root.previous()
      Image:
      source: "backward.png"
      y: self.parent.y + self.parent.height - 75
      x: self.parent.x + 10
      size: self.size
      Button:
      id:pause
      on_press: root.pause()
      background_color: 0, 0, 0, 0
      Image:
      source: "pause.png"
      y: self.parent.y + self.parent.height - 75
      x: self.parent.x + 10
      size: self.size
      Button:
      id:play
      on_press: root.play()
      background_color: 0, 0, 0, 0
      Image:
      source: "play.png"
      y: self.parent.y + self.parent.height - 75
      x: self.parent.x + 10
      size: self.size

      Button:
      on_press: root.next()
      id:forward
      background_color: 0, 0, 0, 0
      Image:
      source: "forward.png"
      y: self.parent.y + self.parent.height - 75
      x: self.parent.x + 10
      size: self.size
      Slider:
      id:volumecontrol
      size_hint_y: 0.1
      pos: (root.width * 0.4), root.height - 200
      size: (root.width * 0.6) - 15, 50
      max: 100
      min: 0
      on_value:root.volumectrl(*args)

      Button:
      id: nowplay
      text: 'Currently Playing'
      pos: (root.width * 0.4),root.height - 85
      size: (root.width * 0.6), 50
      background_color: 0, 0, 0, 0

      Label:
      id: status
      text: ''
      pos: root.width * 0.15, root.top * 0.5


      What have I changed - at first I replace a list of my tracks to head class - that makes it available for all functions and make possible to write functions to all buttons in my small interface - I've used in kv file a link to background image what makes it a bit prettier, and as bonus I've written a volume control function and I've created a bar for it to read constantly the value whatever it is in range from 0 to 100






      share|improve this answer


























        0












        0








        0







        That took not so long but I forgot to post my code - maybe somebody will learn or reuse my answer in own project:



        import kivy
        kivy.require('1.10.0')
        from kivy.config import Config
        Config.set('graphics', 'fullscreen', '0')

        from kivy.app import App
        from kivy.lang import Builder
        from kivy.uix.popup import Popup
        from kivy.uix.button import Button
        from kivy.uix.widget import Widget
        from kivy.core.audio import SoundLoader
        from kivy.uix.gridlayout import GridLayout
        from kivy.properties import ObjectProperty
        from kivy.properties import NumericProperty
        from kivy.uix.slider import Slider
        from os import listdir, path

        Builder.load_file("views/theme.kv")

        class Player(Widget):
        directory = "" #directory path;
        currentlyplaying = "" #now playing song;
        position = 0
        volume = 0
        maxvolume = NumericProperty(1.0)
        tracks =
        def Gettrack_path(self):
        try:
        track = open("path/path.dat", "r")
        self.ids.direct.text = str(track.readline())
        track.close()
        self.ids.searchBtn.text = "Search"
        self.load_tracks()
        except:
        self.ids.direct.text = ''

        def Savetrack_path(self, path):
        track = open("path/path.dat", "w")
        track.write(path)
        track.close()

        def load_tracks(self):
        self.directory = self.ids.direct.text
        if not self.directory.endswith("\"):
        self.directory += "\"

        if not path.exists(self.directory):
        self.ids.status.text = "Given path doesn't exist"
        self.ids.status.color = (1,0,0,1)
        else:
        self.ids.status.text = ""
        self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

        for file in listdir(self.directory):
        if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
        self.tracks.append(file)
        if self.tracks == and self.directory != "":
        self.ids.status.text = "No tracks found"
        self.ids.status.color = (1,0,0,1)
        if self.directory == "\" or self.directory == "":
        try:
        self.ids.status.text = "No tracks found - given path doesn't exist"
        self.ids.status.color = (1,0,0,1)
        except:
        self.ids.status.text ="Error loading path"
        self.ids.status.color = (1,0,0,1)
        self.directory = "\"
        self.tracks.sort()
        for track in self.tracks:
        def playTrack(bt):
        try:
        self.currentlyplaying.stop()
        except:
        pass
        finally:
        if track.endswith(".mp3"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.mp3'
        if track.endswith(".wav"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.wav'
        if track.endswith(".aiff"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.aiff'
        if track.endswith(".aac"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.aac'
        if track.endswith(".ogg"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.ogg'
        if track.endswith(".mp4"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.mp4'
        if track.endswith(".wma"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.wma'

        btn = Button(text = track[:-4], on_press = playTrack)
        self.ids.scroll.add_widget(btn)
        if self.tracks.index(track) % 2 == 0:
        btn.background_color = (88, 44, 234, 0.6)
        else:
        btn.background_color = (37, 44, 249, 0.6)
        def pause(self):
        if self.ids.pause.state == "down":
        if self.currentlyplaying == "":
        pass
        else:
        self.position = self.currentlyplaying.get_pos()
        self.currentlyplaying.stop()
        def play(self):
        if self.ids.play.state == "down":
        if self.currentlyplaying == "":
        pass
        if self.currentlyplaying.state == "stop":
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.currentlyplaying.seek(self.position - 1)
        if self.currentlyplaying.state =="play":
        pass
        def next(self):
        if self.ids.forward.state == "down":
        if self.currentlyplaying == "":
        pass
        i = 0
        while(i < len(self.tracks)):
        if self.currentlyplaying.filename == self.directory + self.tracks[i]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i+1])
        self.ids.nowplay.text = self.tracks[i+1]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        elif self.currentlyplaying.filename == self.directory + self.tracks[-1]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[0])
        self.ids.nowplay.text = self.tracks[0]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        i += 1
        def previous(self):
        if self.ids.previous.state == "down":
        if self.currentlyplaying == "":
        pass
        i = 0
        while(i < len(self.tracks)):
        if self.currentlyplaying.filename == self.directory + self.tracks[i]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i-1])
        self.ids.nowplay.text = self.tracks[i-1]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        elif self.currentlyplaying.filename == self.directory + self.tracks[0]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[-1])
        self.ids.nowplay.text = self.tracks[-1]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        i += 1

        def volumectrl(self, *args):
        self.volume = int(args[1]) / 100
        if self.volume >= int(self.maxvolume):
        self.volume = int(self.maxvolume)
        self.currentlyplaying.volume = self.volume


        class PyPlayer(App):
        def build(self):
        player = Player()
        player.Gettrack_path()
        return player
        PyPlayer().run()


        And of course a .kv file:



        #:kivy 1.10
        <Player>:
        orientation: "horizontal"
        canvas.before:
        Rectangle:
        pos: self.pos
        size: self.size
        source: "264727.jpg"


        TextInput:
        id: direct
        pos: 0,root.top-35
        size: (root.width * 0.3),35
        hint_text: 'balk is empty'

        Button:
        id: searchBtn
        size: (root.width * 0.1),36
        background_color: 1, 0, 0, 22
        pos: root.width * 0.3, root.top-35
        on_release: root.load_tracks()

        ScrollView:
        size_hint: None, None
        size: (root.width *0.4), root.height - 45
        pos: 0, 0
        GridLayout:
        id: scroll
        cols: 1
        spacing: 10
        size_hint_y: None
        row_force_default: True
        row_default_height: 40

        GridLayout:
        rows: 1
        pos: (root.width * 0.4), root.height - 135
        size: (root.width * 0.6), 50
        Button:
        id:previous
        background_color: 0, 0, 0, 0
        on_press: root.previous()
        Image:
        source: "backward.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size
        Button:
        id:pause
        on_press: root.pause()
        background_color: 0, 0, 0, 0
        Image:
        source: "pause.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size
        Button:
        id:play
        on_press: root.play()
        background_color: 0, 0, 0, 0
        Image:
        source: "play.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size

        Button:
        on_press: root.next()
        id:forward
        background_color: 0, 0, 0, 0
        Image:
        source: "forward.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size
        Slider:
        id:volumecontrol
        size_hint_y: 0.1
        pos: (root.width * 0.4), root.height - 200
        size: (root.width * 0.6) - 15, 50
        max: 100
        min: 0
        on_value:root.volumectrl(*args)

        Button:
        id: nowplay
        text: 'Currently Playing'
        pos: (root.width * 0.4),root.height - 85
        size: (root.width * 0.6), 50
        background_color: 0, 0, 0, 0

        Label:
        id: status
        text: ''
        pos: root.width * 0.15, root.top * 0.5


        What have I changed - at first I replace a list of my tracks to head class - that makes it available for all functions and make possible to write functions to all buttons in my small interface - I've used in kv file a link to background image what makes it a bit prettier, and as bonus I've written a volume control function and I've created a bar for it to read constantly the value whatever it is in range from 0 to 100






        share|improve this answer













        That took not so long but I forgot to post my code - maybe somebody will learn or reuse my answer in own project:



        import kivy
        kivy.require('1.10.0')
        from kivy.config import Config
        Config.set('graphics', 'fullscreen', '0')

        from kivy.app import App
        from kivy.lang import Builder
        from kivy.uix.popup import Popup
        from kivy.uix.button import Button
        from kivy.uix.widget import Widget
        from kivy.core.audio import SoundLoader
        from kivy.uix.gridlayout import GridLayout
        from kivy.properties import ObjectProperty
        from kivy.properties import NumericProperty
        from kivy.uix.slider import Slider
        from os import listdir, path

        Builder.load_file("views/theme.kv")

        class Player(Widget):
        directory = "" #directory path;
        currentlyplaying = "" #now playing song;
        position = 0
        volume = 0
        maxvolume = NumericProperty(1.0)
        tracks =
        def Gettrack_path(self):
        try:
        track = open("path/path.dat", "r")
        self.ids.direct.text = str(track.readline())
        track.close()
        self.ids.searchBtn.text = "Search"
        self.load_tracks()
        except:
        self.ids.direct.text = ''

        def Savetrack_path(self, path):
        track = open("path/path.dat", "w")
        track.write(path)
        track.close()

        def load_tracks(self):
        self.directory = self.ids.direct.text
        if not self.directory.endswith("\"):
        self.directory += "\"

        if not path.exists(self.directory):
        self.ids.status.text = "Given path doesn't exist"
        self.ids.status.color = (1,0,0,1)
        else:
        self.ids.status.text = ""
        self.ids.scroll.bind(minimum_height = self.ids.scroll.setter("height"))

        for file in listdir(self.directory):
        if file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".aiff") or file.endswith(".aac") or file.endswith(".ogg") or file.endswith(".mp4") or file.endswith(".wma"):
        self.tracks.append(file)
        if self.tracks == and self.directory != "":
        self.ids.status.text = "No tracks found"
        self.ids.status.color = (1,0,0,1)
        if self.directory == "\" or self.directory == "":
        try:
        self.ids.status.text = "No tracks found - given path doesn't exist"
        self.ids.status.color = (1,0,0,1)
        except:
        self.ids.status.text ="Error loading path"
        self.ids.status.color = (1,0,0,1)
        self.directory = "\"
        self.tracks.sort()
        for track in self.tracks:
        def playTrack(bt):
        try:
        self.currentlyplaying.stop()
        except:
        pass
        finally:
        if track.endswith(".mp3"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp3')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.mp3'
        if track.endswith(".wav"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wav')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.wav'
        if track.endswith(".aiff"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aiff')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.aiff'
        if track.endswith(".aac"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.aac')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.aac'
        if track.endswith(".ogg"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.ogg')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.ogg'
        if track.endswith(".mp4"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.mp4')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.mp4'
        if track.endswith(".wma"):
        self.currentlyplaying = SoundLoader.load(self.directory+bt.text+'.wma')
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.ids.nowplay.text = bt.text+'.wma'

        btn = Button(text = track[:-4], on_press = playTrack)
        self.ids.scroll.add_widget(btn)
        if self.tracks.index(track) % 2 == 0:
        btn.background_color = (88, 44, 234, 0.6)
        else:
        btn.background_color = (37, 44, 249, 0.6)
        def pause(self):
        if self.ids.pause.state == "down":
        if self.currentlyplaying == "":
        pass
        else:
        self.position = self.currentlyplaying.get_pos()
        self.currentlyplaying.stop()
        def play(self):
        if self.ids.play.state == "down":
        if self.currentlyplaying == "":
        pass
        if self.currentlyplaying.state == "stop":
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        self.currentlyplaying.seek(self.position - 1)
        if self.currentlyplaying.state =="play":
        pass
        def next(self):
        if self.ids.forward.state == "down":
        if self.currentlyplaying == "":
        pass
        i = 0
        while(i < len(self.tracks)):
        if self.currentlyplaying.filename == self.directory + self.tracks[i]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i+1])
        self.ids.nowplay.text = self.tracks[i+1]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        elif self.currentlyplaying.filename == self.directory + self.tracks[-1]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[0])
        self.ids.nowplay.text = self.tracks[0]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        i += 1
        def previous(self):
        if self.ids.previous.state == "down":
        if self.currentlyplaying == "":
        pass
        i = 0
        while(i < len(self.tracks)):
        if self.currentlyplaying.filename == self.directory + self.tracks[i]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[i-1])
        self.ids.nowplay.text = self.tracks[i-1]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        elif self.currentlyplaying.filename == self.directory + self.tracks[0]:
        self.currentlyplaying.stop()
        self.currentlyplaying = SoundLoader.load(self.directory + self.tracks[-1])
        self.ids.nowplay.text = self.tracks[-1]
        self.currentlyplaying.play()
        self.currentlyplaying.volume = self.volume
        break
        i += 1

        def volumectrl(self, *args):
        self.volume = int(args[1]) / 100
        if self.volume >= int(self.maxvolume):
        self.volume = int(self.maxvolume)
        self.currentlyplaying.volume = self.volume


        class PyPlayer(App):
        def build(self):
        player = Player()
        player.Gettrack_path()
        return player
        PyPlayer().run()


        And of course a .kv file:



        #:kivy 1.10
        <Player>:
        orientation: "horizontal"
        canvas.before:
        Rectangle:
        pos: self.pos
        size: self.size
        source: "264727.jpg"


        TextInput:
        id: direct
        pos: 0,root.top-35
        size: (root.width * 0.3),35
        hint_text: 'balk is empty'

        Button:
        id: searchBtn
        size: (root.width * 0.1),36
        background_color: 1, 0, 0, 22
        pos: root.width * 0.3, root.top-35
        on_release: root.load_tracks()

        ScrollView:
        size_hint: None, None
        size: (root.width *0.4), root.height - 45
        pos: 0, 0
        GridLayout:
        id: scroll
        cols: 1
        spacing: 10
        size_hint_y: None
        row_force_default: True
        row_default_height: 40

        GridLayout:
        rows: 1
        pos: (root.width * 0.4), root.height - 135
        size: (root.width * 0.6), 50
        Button:
        id:previous
        background_color: 0, 0, 0, 0
        on_press: root.previous()
        Image:
        source: "backward.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size
        Button:
        id:pause
        on_press: root.pause()
        background_color: 0, 0, 0, 0
        Image:
        source: "pause.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size
        Button:
        id:play
        on_press: root.play()
        background_color: 0, 0, 0, 0
        Image:
        source: "play.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size

        Button:
        on_press: root.next()
        id:forward
        background_color: 0, 0, 0, 0
        Image:
        source: "forward.png"
        y: self.parent.y + self.parent.height - 75
        x: self.parent.x + 10
        size: self.size
        Slider:
        id:volumecontrol
        size_hint_y: 0.1
        pos: (root.width * 0.4), root.height - 200
        size: (root.width * 0.6) - 15, 50
        max: 100
        min: 0
        on_value:root.volumectrl(*args)

        Button:
        id: nowplay
        text: 'Currently Playing'
        pos: (root.width * 0.4),root.height - 85
        size: (root.width * 0.6), 50
        background_color: 0, 0, 0, 0

        Label:
        id: status
        text: ''
        pos: root.width * 0.15, root.top * 0.5


        What have I changed - at first I replace a list of my tracks to head class - that makes it available for all functions and make possible to write functions to all buttons in my small interface - I've used in kv file a link to background image what makes it a bit prettier, and as bonus I've written a volume control function and I've created a bar for it to read constantly the value whatever it is in range from 0 to 100







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 10:57









        AnotherNewbieAnotherNewbie

        64




        64






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284163%2fkivy-button-implementation-how-can-i-do-that-with-my-written-code%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues