【发布时间】:2020-06-22 20:07:33
【问题描述】:
我正在尝试学习如何将 ColorPicker 对象属性从 kv 文件传递给主类 MainWindow。 我创建了 ColPopup 和 PickColor 类,并且能够获取有关所选颜色的信息,但我无法获取从 PickColor 传递给 MainWindow 的颜色值。
关于如何实现这一点的任何建议?我想将 Object Property colorpicker 设置为 colorpicker,以便以后可以访问 .color 方法。
main.py
from kivy.app import App
#kivy.require("1.11.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.properties import ObjectProperty
from kivy.uix.colorpicker import ColorPicker
from kivy.uix.popup import Popup
from kivy.config import Config
Config.set('graphics', 'width', '1200')
Config.set('graphics', 'height', '600')
class ColPopup(Popup):
pass
class PickColor(ColorPicker):
pass
class MainWindow(BoxLayout):
colorpicker = ObjectProperty()
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
self.range_tab_action_grid_buttons = {}
self.create_actions()
def choose_color(self, instance):
ColPopup().open()
print("color picked for ", instance, "with current color", instance.background_color)
print(self.colorpicker)
def create_actions(self):
for i in range(1, 4):
grid = BoxLayout(size=(180, 20), size_hint=(None, None))
checkbox = CheckBox(group="Actions",allow_no_selection=False, size_hint=(.2,1))
grid.add_widget(checkbox)
label = Label(text="Action"+str(i)+" color", size_hint=(.6,1))
grid.add_widget(label)
colored_button = Button(background_color=[0, 45, 100, 1], size_hint=(.2,0.8), pos_hint={'center_y':0.5})
colored_button.bind(on_press = self.choose_color)
grid.add_widget(colored_button)
self.ids.range_tab_action_grid.add_widget(grid)
self.range_tab_action_grid_buttons["Action"+str(i)] = {"grid":grid, "checkbox":checkbox, "label":label, "colored_button":colored_button} #, "colorpicker":colorpicker}
# Check the first box
self.range_tab_action_grid_buttons["Action1"]["checkbox"].active = True
#print(self.range_tab_action_grid_buttons["Action1"][3].background_color)
class ColorPickerApp(App):
def build(self):
return MainWindow()
if __name__ == "__main__":
ColorPickerApp().run()
colorpicker.kv
# File name: main.py
#:kivy 1.11.1
<ColPopup>:
title: 'Pick a Color'
size_hint: 0.5, 0.6
colorpicker: colorpicker
BoxLayout:
orientation: 'vertical'
PickColor:
id: colorpicker
Button:
text: "Select color"
on_release: root.dismiss()
size_hint: 0.3,0.3
pos_hint: {"center_x":0.75,"top":0.0}
<Button>:
font_size: 10
color: 1,1,1,1
size_hint: 0.05, 0.05
<MainWindow>:
BoxLayout:
id: range_tab_action_grid
orientation: "vertical"
size_hint: 0.3, 0.2
pos_hint: {"right":0.0, "top":1.0}
【问题讨论】:
标签: python python-3.x user-interface kivy kivy-language