Я новичок в программировании на python и kivy, и чтобы узнать немного больше, я попытался создать небольшой музыкальный проигрыватель - он воспроизводит звук, если я выбираю из списка рядом с панелью - но я не могу этого получитьСама панель для работы - мне нужно какое-то объяснение, как этого добиться, и предметы similair не очень полезны.Я пытался заставить работать ровно 4 кнопки в макете ручки - но безрезультатно. Вот мой код в python \ kivy:
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()
И есть файл .kv
#: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
Это мой первый вопрос, пожалуйста, ребята, поймите, что у меня еще нет опыта