Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy按钮不可按下_Python_Kivy - Fatal编程技术网

Python Kivy按钮不可按下

Python Kivy按钮不可按下,python,kivy,Python,Kivy,我根据文件夹中有多少声音文件动态创建了按钮,但我遇到了一个无法按下任何按钮的问题。我试着查找教程或类似的问题,但似乎我所拥有的应该是正确的,然而当我运行它时,我会看到一个带有按钮的窗口,但当按下按钮时,按钮没有任何作用 import os, glob from os.path import isfile, join from os import listdir from kivy.uix.gridlayout import GridLayout from kivy.uix.button imp

我根据文件夹中有多少声音文件动态创建了按钮,但我遇到了一个无法按下任何按钮的问题。我试着查找教程或类似的问题,但似乎我所拥有的应该是正确的,然而当我运行它时,我会看到一个带有按钮的窗口,但当按下按钮时,按钮没有任何作用

import os, glob

from os.path import isfile, join
from os import listdir
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
from kivy.app import App

Config.set('graphics', 'borderless', False)
Config.set('graphics', 'width', '500')
Config.set('graphics', 'height', '300')

class Soundboard(GridLayout):
def soundSearch(self):
    os.chdir("./sound")
    self.sounds = []
    for Wav in glob.glob("*.wav"):
        self.sounds.append(Wav)
    for Flac in glob.glob("*.flac"):
        self.sounds.append(Flac)
    for mp3 in glob.glob("*.mp3"):
        self.sounds.append(mp3)

def __init__(self,**kwargs):
    super(Soundboard, self).__init__(**kwargs)
    self.soundSearch()
    self.cols = 3
    for sound in self.sounds:
        self.button = Button(text=os.path.splitext(sound)[0])
        self.button.bind(on_press=lambda x:self.clk())
        self.add_widget(self.button)
    self.row_force_default = True
    self.row_default_height = 40
    self.padding = [50, 10]
    self.spacing = 5

def clk(self, *args):
    print('The button is pushed')
class MyApp(App):
    def build(self):
        return Soundboard()

if __name__ == '__main__':
    MyApp().run()

在您提供的代码中,缩进有一个问题。除此之外,代码对我来说也很好:

import glob
import os

from kivy.app import App
from kivy.config import Config
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout

Config.set('graphics', 'borderless', False)
Config.set('graphics', 'width', '500')
Config.set('graphics', 'height', '300')

class Soundboard(GridLayout):
    def soundSearch(self):
        self.sounds = []
        os.chdir("./sound")
        for Wav in glob.glob("*.wav"):
            self.sounds.append(Wav)
        for Flac in glob.glob("*.flac"):
            self.sounds.append(Flac)
        for mp3 in glob.glob("*.mp3"):
            self.sounds.append(mp3)

    def __init__(self,**kwargs):
        super(Soundboard, self).__init__(**kwargs)
        self.soundSearch()
        self.cols = 3
        for sound in self.sounds:
            self.button = Button(text=os.path.splitext(sound)[0])
            self.button.bind(on_press=lambda x: self.clk())
            self.add_widget(self.button)
        self.row_force_default = True
        self.row_default_height = 40
        self.padding = [50, 10]
        self.spacing = 5

    def clk(self, *args):
        print('The button is pushed')

class MyApp(App):
    def build(self):
        return Soundboard()

if __name__ == '__main__':
    MyApp().run()

我修复了缩进,但按下按钮时它仍然不会起任何作用。您使用的是什么版本的Python和kivy?你用它运行了这个代码还是别的什么?你有一个与此代码关联的.kv文件吗?我使用的是Python 2.7,Kivy 1.9.1,我没有一个与此代码关联的.kv文件,我使用一种叫做Scripts的东西从atom运行它,这很奇怪。试着用一个按钮运行一个空的应用程序。它有用吗?如果它有效,那么尝试删除应用程序中的代码,直到您的按钮执行某些操作,也许您会发现问题。您还可以尝试将kivy更新为1.10(当然,将python更新为3)