我有一个KV文件描述了一个简单的弹出窗口,其中包含一些滑块,可以更改颜色ListProperty的R,G和B值。
Slider:
value: root.color[0] = self.value
Slider:
value: root.color[1] = self.value
Slider:
value: root.color[2] = self.value
当我尝试运行应用程序时,Python坚持认为我对值的赋值是无效的语法。我查看了其余的代码,但无法找到问题所在。
这是整个KV文件:
<WelcomeScreen>:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
Button:
text: "Welcome!"
size_hint: 0.1,0.1
on_press: root.manager.current = app.screens[7]
<PhoneSelection>:
BoxLayout:
Button:
text: "iPhone 4/4s"
on_press:
root.manager.current = app.screens[1]
app.vars.phone_type = "iPhone YAYAYA"
self.text = str(app.vars.phone_type)
<PhotoEditor>
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
Button:
text: 'Color'
on_press: root.open_popup()
<ColorPopup>
size_hint: .5, .5
auto_dismiss: True
title: 'Hello world'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
Button:
text: 'Click me to dismiss'
on_press: root.dismiss()
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation: 'vertical'
Slider:
value: root.color[0] = self.value
Slider:
value: root.color[1] = self.value
Slider:
value: root.color[2] = self.value
这是Python文件:
from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.colorpicker import ColorPicker
from kivy.logger import Logger
from kivy.graphics import Color, Ellipse, Line
from kivy.properties import ListProperty
#from editor import PhotoEditor
import random
#Each class def is a different screen.
class WelcomeScreen(Screen):
def do_stuff():
pass
class PhoneSelection(Screen):
pass
class StyleSelection(Screen):
pass
class ColorSelection(Screen):
pass
class DesignSelection(Screen):
pass
class UploadPhoto(Screen):
pass
class PatternSelection(Screen):
pass
class PhotoEditor(Screen):
color = ListProperty([1,1,1,1])
def open_popup(self):
popup = ColorPopup()
popup.open()
class Confirm(Screen):
pass
class Pay(Screen):
pass
class ThankYou(Screen):
pass
class ColorPopup(Popup):
pass
class Variables():
phone_type = ""
phone_color = ""
phone_style = ""
phone_design_option = ""
total_price = 0.0
#This is the main class that has all the app info.
class MainApp(App):
#list of names for each screen. Each name is used as an ID when changing the current screen.
screens = ["welcome","choose_phone","choose_style","choose_color","choose_design","upload_photo","choose_pattern","photo_edit","confirm","pay","thanks"]
vars = Variables()
def build(self):
#The manager that holds all the screens and allows transitioning between them.
SM = ScreenManager()
#list of names for each screen. Each name is used as an ID when changing the current screen.
screens = ["welcome","choose_phone","choose_style","choose_color","choose_design","upload_photo","choose_pattern","photo_edit","confirm","pay","thanks"]
#Add all screens to the manager and assign the corresponding ID.
SM.add_widget(WelcomeScreen(name=screens[0]))
SM.add_widget(PhoneSelection(name=screens[1]))
SM.add_widget(StyleSelection(name=screens[2]))
SM.add_widget(ColorSelection(name=screens[3]))
SM.add_widget(DesignSelection(name=screens[4]))
SM.add_widget(UploadPhoto(name=screens[5]))
SM.add_widget(PatternSelection(name=screens[6]))
SM.add_widget(PhotoEditor(name=screens[7]))
SM.add_widget(Confirm(name=screens[8]))
SM.add_widget(Pay(name=screens[9]))
SM.add_widget(ThankYou(name=screens[10]))
#Set the current screen to the welcome screen.
SM.current = screens[0]
return SM
#Runs the app.
if __name__ == "__main__":
t = MainApp();
t.run()
答案 0 :(得分:1)
value: root.color[0] = self.value
这是设置 value
属性值的语法,但是你的python代码是赋值,它不返回值,因此语法无效。
你的意思是:
on_value: root.color[0] = self.value
当用户更改值时,这将更新root.color。