如何使用On_Press更改动态创建的小部件的BG颜色并使用Pickle保存? (与Kivy的Python)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用On_Press更改动态创建的小部件的BG颜色并使用Pickle保存? (与Kivy的Python)相关的知识,希望对你有一定的参考价值。

目标:

  1. 使用on-press更改动态创建的小部件的背景颜色。
  2. 使用pickle保存此状态,以便在我重新打开程序时,保留新的颜色更改

注意:您将在我的代码中看到我还没有尝试将按钮bg颜色状态保存到文件中,因为我仍然试图让on-press事件正常运行。

我收到以下错误:

  File "C:/Users/phili/scrollablelabelexample.py", line 45, in create_button
    button_share.bind(on_press = self.update_buttons_departoverride(self))

TypeError: update_buttons_departoverride() takes 1 positional argument but 2 were given

Python代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty, ObjectProperty, NumericProperty
from kivy.clock import Clock
import pandas as pd
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label

class AnotherScreen(Screen):
    pass

class BackHomeWidget(Widget):
    pass

class Sequence(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class MainScreen(Screen):
    pass

class CleanScreen(BoxLayout):
    def __init__(self, **kwargs):
        super(CleanScreen, self).__init__(**kwargs)
        self.orientation = "vertical"
        Clock.schedule_once(lambda *args:self.create_button(self.ids.box_share))

    def create_button(self, box_share):
        top_button_share = 1.1
        color = [.48,.72,.23,1]
        for i in range(len(parts)):
            top_button_share -= .4
            button_share = Button(background_normal = '', background_color = color, id = "part"+str(i+1),pos_hint={"x": 0, "top": top_button_share}, size_hint_y=None, height=60, text=str(i))
            button_share.bind(on_press = self.update_buttons_departoverride(self))
            box_share.add_widget(button_share)

    def update_buttons_departoverride(self):
        self.background_color = 1.0, 0.0, 0.0, 1.0

presentation = Builder.load_file("garagemainexample.kv")

class MainApp(App):
    def build(self):
        return presentation 

if __name__ == "__main__":
    MainApp().run()

Kv代码:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    Sequence:

<BigButton@Button>:
    font_size: 40
    size_hint: 0.5, 0.15
    color: 0,1,0,1 

<SmallNavButton@Button>:    
    font_size: 32
    size: 125, 50    
    color: 0,1,0,1

<BackHomeWidget>:
    SmallNavButton:
        on_release: app.root.current = "main"
        text: "Home"
        pos: root.x, root.top - self.height

<MainScreen>:
    name: "main"
    FloatLayout:
        BigButton:
            on_release: app.root.current = "sequence"
            text: "Sequence"
            pos_hint: {"x":0.25, "top": 0.4} 

<CleanScreen>:
    ScrollView:
        GridLayout:
            id: box_share
            cols: 1
            size_hint_y: None
            size_hint_x: 0.5
            spacing: 5
            padding: 90
            height: self.minimum_height
            canvas:
                Color: 
                    rgb: 0, 0, 0
                Rectangle:
                    pos: self.pos
                    size: self.size

<Sequence>:
    name: "sequence"
    CleanScreen:
        id: cleanscreen
    BackHomeWidget:
答案

使用button_share.bind (on_press = self.update_buttons_departoverride (self))你正在调用这个方法,所以你试图用on_press绑定Noneself.update_buttons_departoverride返回None)。如果要传递参数,请使用lambdafunctools.partial

from functools import partial
button_share.bind(on_press=partial(self.update_buttons_departoverride, arg1,...))

但是,如果您只需要传递按钮的引用,则它已经自动传递。你只需要这样做:

button_share.bind(on_press=self.update_buttons_departoverride)

和:

def update_buttons_departoverride(self, button):

要存储小部件的配置,您可以使用Storage。使用DictStore的简化示例:

卖弄.朋友:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.storage.dictstore import DictStore


class AnotherScreen(Screen):
    pass


class BackHomeWidget(Widget):
    pass


class Sequence(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


class MainScreen(Screen):
    pass


class CleanScreen(BoxLayout):
    box_share = ObjectProperty()
    config_file = DictStore('conf.dat')

    def __init__(self, **kwargs):
        super(CleanScreen, self).__init__(**kwargs)
        self.orientation = "vertical"
        Clock.schedule_once(self.create_button)

    def create_button(self, *args):

        top_button_share = 1.1
        color = (.48, .72, .23, 1)
        for i in range(5):
            top_button_share -= .4
            id_ = "part" + str(i + 1)

            if self.config_file.exists(id_):
                btn_color = self.config_file[id_]["background_color"]
            else:
                self.config_file.put(id_, background_color=color)
                btn_color = color

            button_share = Button(background_normal='',
                                  background_color=btn_color,
                                  id=id_,
                                  pos_hint={"x": 0, "top": top_button_share},
                                  size_hint_y=None,
                                  height=60,
                                  text=str(i))
            button_share.bind(on_press=self.update_buttons_departoverride)
            self.box_share.add_widget(button_share)

    def update_buttons_departoverride(self, button):
        button.background_color = 1.0, 0.0, 0.0, 1.0
        self.config_file.put(button.id, background_color=(1.0, 0.0, 0.0, 1.0))

presentation = Builder.load_file("garagemainexample.kv")

class MainApp(App):

    def build(self):
        return presentation

if __name__ == "__main__":
    MainApp().run()

garagemainexample.kv:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    Sequence:

<BigButton@Button>:
    font_size: 40
    size_hint: 0.5, 0.15
    color: 0,1,0,1 

<SmallNavButton@Button>:    
    font_size: 32
    size: 125, 50    
    color: 0,1,0,1

<BackHomeWidget>:
    SmallNavButton:
        on_release: app.root.current = "main"
        text: "Home"
        pos: root.x, root.top - self.height

<MainScreen>:
    name: "main"
    FloatLayout:
        BigButton:
            on_release: app.root.current = "sequence"
            text: "Sequence"
            pos_hint: {"x":0.25, "top": 0.4} 

<CleanScreen>:
    box_share: box_share
    ScrollView:
        GridLayout:
            id: box_share
            cols: 1
            size_hint_y: None
            size_hint_x: 0.5
            spacing: 5
            padding: 90
            height: self.minimum_height
            canvas:
                Color: 
                    rgb: 0, 0, 0
                Rectangle:
                    pos: self.pos
                    size: self.size

<Sequence>:
    name: "sequence"
    CleanScreen:
        id: cleanscreen
    BackHomeWidget:

以上是关于如何使用On_Press更改动态创建的小部件的BG颜色并使用Pickle保存? (与Kivy的Python)的主要内容,如果未能解决你的问题,请参考以下文章

Gridstack 动态创建的小部件不拖动

如何在不一遍又一遍地编写相同代码的情况下创建具有动态内容的小部件的长列表? [关闭]

Kivy:使用 on_press 事件在屏幕管理器中更改屏幕

如何在android中正确设置一个大的小部件图标

引用 Kivy 中动态创建的小部件的 id

如何切换网格布局中的小部件可见性?