创建一个变量,该变量包含Kivy中从kv到py的图像源



我正在尝试获取图像源,并将一个变量从我的kv放入py。我创建了一个函数,在按下和释放按钮时,我的按钮会更改图像。好吧,这个变量必须包含我函数中的图像源。我想用这个变量来更改图片。。我想创建更多具有相同图片但具有不同id的按钮,以便多次使用相同的功能。我不想为每个重复的按钮创建几个函数。请检查我的函数,def hello_on,第二个参数,以及在我的kv.file检查中,on_press和on_Prelease

py.file

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.spelling import Spelling
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.properties import StringProperty
Config.set('graphics', 'width', 360)
Config.set('graphics', 'height', 640)
kv= Builder.load_file('test2.kv')
class MyLayout(Widget):
pass
def spinner_clicked(self,value):
pass
#sm= ScreenManager()
#screen= Screen(name='second')
#self.ids.Label1.text= f'You selected: {value}'

#Definine our different screens
class FirstWindow(Screen):
var1= StringProperty("images/shoppingcart.png")
var2= StringProperty("images/shoppingcart2.png")
def press_on(self):

#self.ids.my_label.text= "You Pressed the Button"
self.ids.Shoppingcart1.source= self.var2
def press_off(self):
self.ids.Shoppingcart1.source= self.var1

class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class FourthWindow(Screen):
pass

class WindowManager(ScreenManager):
pass 

class AwesomeApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()

kv.file


<MyLayout>
BoxLayout:
orientation:"vertical"
size: root.width, root.height
GridLayout:
cols:3
rows:3

size_hint: 1,None
height: 65
Spinner:
id: spinner_id
text: "Menu"
values: ["Catalogue","Buy","Payment Methods", "Contact"]
on_text: root.spinner_clicked(spinner_id.text)
on_text: window_manager.current = self.text  # uses an id defined in the WindowManager section
#on_text: root.manager.current= "second" - Just work in Screens

Image:
source: 'images/ferrepluslogo2.png'

Button:
text:"Buy"

WindowManager:
id: window_manager
FirstWindow:
SecondWindow:
ThirdWindow:
FourthWindow:  

<FirstWindow>:
name: "Catalogue"
BoxLayout:
orientation: "vertical"
#size: root.width, root.height
size_hint: 1, None
height: 180
GridLayout:
cols: 4
padding: 4

Label:
text: "Items"
#font_size: 25
Label:
text: "Number"
Label:
text: "Price"
Label:
text: "Add to Cart"
Label:
text: "tornillos"
text_size: self.size
halign: 'center'
valign: 'bottom'
Image:
id: my_image
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: "images/tornillo.png"
center_x: self.parent.center_x
center_y: self.parent.center_y+10
TextInput: 
id: input_tornillo
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
Label:
text: "0.0"

Button:
id: shopping1
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
on_press: root.press_on() 
on_release: root.press_off()
Image:
id: Shoppingcart1
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: root.var1
center_x: self.parent.center_x
center_y: self.parent.center_y

#------------------------
Label:
text: "Andamios"
text_size: self.size
halign: 'center'
valign: 'bottom'
Image:
id: tornillos_image
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: "images/Andamios.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
TextInput: 
id: input_andamios
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
Label:
text: "0.0"

Button:
id: shopping2
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
Image:
id: Shoppingcart2
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y

<SecondWindow>:
name: "Buy"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Buy Screen"
font_size: 32

<ThirdWindow>:
name: "Payment Methods"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Payment Methods Screen"
font_size: 32

<FourthWindow>:
name: "Contact"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Contact Screen"
font_size: 32


如@ApuCoder所述,您可以在FirstWindow类中定义StringProperty

class FirstWindow(Screen, PARAMETER OF MY IMAGE.souRCE!!!):
variable_of_my_image_source = StringProperty('PARAMETER OF MY IMAGE.souRCE')

然后,在您的kv中,您可以使用该属性作为Image的源(只要Image在以<FirstWindow>:为根的规则中(:

Image:
id: my_image
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: root.variable_of_my_image_source
center_x: self.parent.center_x
center_y: self.parent.center_y+10

然后,您可以在FirstWindow实例中更改self.variable_of_my_image_source的值,例如:

self.variable_of_my_image_source = 'new_image_source`

并且CCD_ 9将自动更新。


另一种方法是定义一个DictProperty,以包含基于idImage源,如下所示:

class FirstWindow(Screen, PARAMETER OF MY IMAGE.souRCE!!!):
variable_of_my_image_source = DictProperty()
def __init__(self, **kwargs):
super(FirstWindow, self).__init__(**kwargs)
self.variable_of_my_image_source['my_image'] = 'one.png'
self.variable_of_my_image_source['Shoppingcart1'] = 'two.png'

然后,在kv中,您可以参考Image源的字典:

Image:
id: my_image
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: root.variable_of_my_image_source['my_image']
center_x: self.parent.center_x
center_y: self.parent.center_y+10

您可以在FirstWindow中定义一个方法来分配给Button,例如:

def update_image(self, id):
self.ids[id].source = 'three.png'

并在kv:中引用该方法

Button:
on_release: root.update_image('my_image')

我一直在寻找解决方案,终于找到了!我只使用StringProperty在一个类中创建了2个变量,并在我的kv.file 中写入

on_press:root.ids.Shoppingcart1.source=root.var2on_release:root.ids.Shoppingcart1.source=root.var1

对于每个id。

py.file


kv.file

<FirstWindow>:
name: "Catalogue"
BoxLayout:
orientation: "vertical"
#size: root.width, root.height
size_hint: 1, None
height: 180
GridLayout:
cols: 4
padding: 4

Label:
text: "Items"
#font_size: 25
Label:
text: "Number"
Label:
text: "Price"
Label:
text: "Add to Cart"
Label:
text: "tornillos"
text_size: self.size
halign: 'center'
valign: 'bottom'
Image:
id: my_image
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: "images/tornillo.png"
center_x: self.parent.center_x
center_y: self.parent.center_y+10
TextInput: 
id: input_tornillo
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
Label:
text: "0.0"

Button:
id: shopping1
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on() 
#on_release: root.press_off()
on_press: root.ids.Shoppingcart1.source= root.var2 
on_release: root.ids.Shoppingcart1.source= root.var1 
Image:
id: Shoppingcart1
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: root.var1
center_x: self.parent.center_x
center_y: self.parent.center_y

最新更新