我的kivy代码应该显示filechooser并允许用户选择图像作为背景图像。上传图像按钮显示但用户点击它时没有任何反应。
from random import random
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line, Rectangle
from kivy.uix.filechooser import FileChooserListView, FileChooserIconView
from kivy.uix.floatlayout import FloatLayout
class MyPaintWidget(Widget):
def on_touch_down(self, touch):
color = (random(), random(), random())
with self.canvas:
Color(*color)
d = 30.
touch.ud['line'] = Line(points=(touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
class MyPaintApp(App):
def build(self):
parent = Widget()
painter = MyPaintWidget()
Choose = Button(text = 'upload image')
parent.add_widget(painter)
parent.add_widget(Choose)
def chooose_file(obj):
fc = FileChooserIconView(title= 'upload image')
image_path = self.fc.selection[0]
image_name = file_path.split('/')[-1]
with self.canvas.before:
Rectangle(
size=self.size,
pos=self.pos,
source=image_name)
Choose.bind(on_release=choose_file)
return parent
if __name__ == '__main__':
MyPaintApp().run()
答案 0 :(得分:2)
我不确定你是否有缩进错误,但看起来你绑定了你应该被绑定的方法内的按钮(因为它永远不会被称为你的按钮永远不会被绑定)
答案 1 :(得分:0)
你需要去缩进:
Choose.bind(on_release=choose_file)
return parent
是:
Choose.bind(on_release=choose_file)
return parent
目前只有在调用choose_file时才会调用它,这已经太晚了,因为你希望它在之前不被绑定。