2015-05-18 74 views
1

單擊按鈕時,如何更改隨機拋出的圖像的不透明度? (使用kivy)。如何在按下kivy上的按鈕時改變不透明度

from random import randint 
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.boxlayout import BoxLayout 
from kivy.uix.image import Image 
from kivy.uix.floatlayout import FloatLayout 



class MyBackground(Widget): 

    def __init__(self, **kwargs): 
     x = randint(1,10) 
     print (x) 
     y = 'water.png' 
     if x==1: 
      y = 'a.png' 
     if x==2: 
      y = 'b.png' 
     if x==3: 
      y = 'c.png' 
     if x==4: 
      y = 'd.png' 
     if x==5: 
      y = 'e.png' 
     if x==6: 
      y = 'f.png' 
     if x==7: 
      y = 'g.png' 
     if x==8: 
      y = 'h.png' 
     if x==9: 
      y = 'i.png' 
     if x==10: 
      y = 'j.png' 
     super(MyBackground, self).__init__(**kwargs) 
     with self.canvas: 
      Color(1, 1, 1, 0.5) 
      self.bg = Rectangle(source=y, pos=self.pos, size=self.size) 
     self.bind(pos=self.update_bg) 
     self.bind(size=self.update_bg) 
     Color(1, 0, 1, 0.5) 

    def update_bg(self, *args): 
     self.bg.pos = self.pos 
     self.bg.size = self.size 


class MyPaintWidget(Widget): 
    def on_touch_down(self, touch): 
     with self.canvas: 
      Color(1, 1, 1) 
      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 MyRandomApp(App): 

    def build(self): 
     layout = BoxLayout(spacing=10, orientation='vertical') 
     b = BoxLayout(spacing=0) 
     btn1 = Button(text='opacity 40%', size_hint=(.2, .2)) 
     btn3 = Button(text='opacity 20%', size_hint=(.2, .2)) 
     btn4 = Button(text='opacity 10%', size_hint=(.2, .2)) 
     btn5 = Button(text='opacity 0%', size_hint=(.2, .2)) 
     b.add_widget(btn1) 
     b.add_widget(btn3) 
     b.add_widget(btn4) 
     b.add_widget(btn5) 
     layout.add_widget(b) 
     background = MyBackground(size_hint=(1, 10)) 
     layout.add_widget(background) 
     painter = MyPaintWidget() 
     layout.add_widget(painter) 


     return layout 



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

我試着在類MyRandomApp()中有一個定義,但它表示它沒有屬性到畫布。

回答

3

我建議在背景小部件中使用kivy屬性來實現不透明度,然後將按鈕綁定到更新不透明度的函數。

我也強烈建議看kv語言,以使這種事情更容易。它也可以幫助擺脫許多添加小部件並將其他東西綁定到一起的樣板代碼。有關更多信息,請參閱kv docsa good crash course video

但我會回答的方式W/O KV。爲了清晰起見,我還要去除隨機圖像部分,這對於這個例子來說並不重要。

首次進口導入的屬性:

from kivy.properties import NumericProperty 

然後一個數字屬性添加到您的背景類。現在,您可以在顏色指令中使用該屬性。因爲你使用不透明作爲屬性,所以kivy會自動綁定它,所以當不透明度改變時,Color指令也會改變。所以,你的背景類變爲:

class MyBackground(Widget): 
    opacity = NumericProperty(0.5) 

    def __init__(self, **kwargs): 
     y = 'test.jpg' 
     super(MyBackground, self).__init__(**kwargs) 
     with self.canvas: 
      Color(1, 1, 1, self.opacity) 
      self.bg = Rectangle(source=y, pos=self.pos, size=self.size) 

     self.bind(pos=self.update_bg) 
     self.bind(size=self.update_bg) 
     Color(1, 0, 1, 0.5) 

    def update_bg(self, *args): 
     self.bg.pos = self.pos 
     self.bg.size = self.size 

然後保持到按鈕和背景部件引用和功能連接到更新基於按下按鈕背景部件的透明度按鈕事件。 (請注意,我不是一個超級經驗kivy用戶,所以有可能是一個更好的辦法來確定哪個按鈕被按下,並相應地自我透明度。)

所以你的應用程序就變成了: 類MyRandomApp(應用程序):

def build(self): 
    layout = BoxLayout(spacing=10, orientation='vertical') 
    b = BoxLayout(spacing=0) 

    self.btn1 = Button(text='opacity 40%', size_hint=(.2, .2)) 
    self.btn3 = Button(text='opacity 20%', size_hint=(.2, .2)) 
    self.btn4 = Button(text='opacity 10%', size_hint=(.2, .2)) 
    self.btn5 = Button(text='opacity 0%', size_hint=(.2, .2)) 

    b.add_widget(self.btn1) 
    b.add_widget(self.btn3) 
    b.add_widget(self.btn4) 
    b.add_widget(self.btn5) 
    layout.add_widget(b) 

    self.background = MyBackground(size_hint=(1, 10)) 

    layout.add_widget(self.background) 
    painter = MyPaintWidget() 
    layout.add_widget(painter) 

    self.btn1.bind(on_press=self.set_opacity) 
    self.btn3.bind(on_press=self.set_opacity) 
    self.btn4.bind(on_press=self.set_opacity) 
    self.btn5.bind(on_press=self.set_opacity) 

    return layout 

def set_opacity(self,widget): 
    if widget is self.btn1: 
     self.background.opacity = 0.40 
    elif widget is self.btn3: 
     self.background.opacity = 0.20 
    elif widget is self.btn4: 
     self.background.opacity = 0.10 
    elif widget is self.btn5: 
     self.background.opacity = 0.00 
+0

這是非常有幫助的。但是有一個問題,爲什麼你必須把「自我」放在一切之前?謝謝! –

+0

使用'self'關鍵字可以創建一個類實例屬性。然後您可以在其他類功能中引用該屬性。它比這更深,是Python中的一個重要概念。 [link](http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self-in-python)也許是一個開始的地方 – therealpickle