Kivy:如果没有保存,我如何清除屏幕上的更改

时间:2017-07-30 10:40:48

标签: python events kivy

我目前有一个主页面和一个设置页面。在屏幕管理器中,当我转到设置页面并更改值而不单击保存按钮并离开设置页面时,看起来好像设置已保存到最终用户,因为当他们返回到该屏幕时,他们的更改仍然反映而他们没有点击保存。我该如何解决这个问题。

我尝试清除小部件并重新创建它们但我失败了。似乎Kivy在运行on_pre_leave事件的同时运行on_pre_enter事件。要解决这个问题,如果设置在退出屏幕时保存,或者当它们退出屏幕而没有单击保存时,它们的更改将被清除,那将是很好的。请帮忙:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.switch import Switch
from kivy.core.window import Window
from kivy.uix.progressbar import ProgressBar
from kivy.clock import Clock

Window.clearcolor = (252, 235, 233, 0)
#!/usr/bin/python3

class MyScreenManager(ScreenManager):
    pass


class LoginPageScreen(Screen):
    pass
class UserSettingsScreen(Screen):
    pass

class LoginPage(BoxLayout):
    def __init__(self, **kwargs):
        super(LoginPage, self).__init__(**kwargs)

        self.add_widget(Label(text="[color=000000][b]Main page[/b][/color]", markup=True, size_hint_y=None, font_size='15sp'))

class UserSettings(BoxLayout):
    def __init__(self, **kwargs):
        super(UserSettings, self).__init__(**kwargs)
        # self.clear_widgets()
        on_pre_enter = self.userSettingsClass()
        # on_pre_leave = self.userSettingsClass()

    def userSettingsClass(self):
        # self.clear_widgets()

        self.add_widget(Label(text="[color=000000][b]settings[/b][/color]", markup=True, font_size='15sp'))
        self.testSwitch = Switch()
        self.add_widget(self.testSwitch)

        self.add_widget(Button(text='Save', font_size=40, size_hint_y=None, height=80, on_press=self.updateSettings))

    def updateSettings(self, btn):
        print('this is how i currently save settings, if the user does not click this button and come back to this page'
              ' it looks as though they saved the settings when they really did not')



root_widget = Builder.load_string('''
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import sys sys
MyScreenManager:
    transition: NoTransition()

    LoginPageScreen:
        ActionBar:
            pos_hint: {'top':1}
        ActionBar:
            pos_hint: {'top':0.07}
            ActionView:
                ActionPrevious:
                    with_previous: False
                ActionOverflow:
                ActionGroup:
                    ActionButton:
                        text: 'Settings'
                        on_release: app.root.current = 'settings'

    UserSettingsScreen:
        ActionBar:
            pos_hint: {'top':1}
        ActionBar:
            pos_hint: {'top':0.07}
            ActionView:
                ActionPrevious:
                    with_previous: True
                    on_release: app.root.current = 'home'


<UserSettingsScreen>:
    name: 'settings'
    # on_pre_enter: userSettingsClass()
    BoxLayout:
        size_hint_y: 0.85
        pos_hint: {'center_y': .5}
        #size_hint: (None, None)
        ScrollView:
            UserSettings
                orientation: 'vertical'
                padding: [50,10,50,10]
                spacing: 10
                # size_hint: (None, None)
                size: (1080, 3000)

<LoginPageScreen>:
    name: 'home'
    BoxLayout:
        LoginPage
            orientation: 'vertical'
            padding: [50,10,50,10]
            spacing: 10
            # size_hint_y: 0.9

''')

class ScreenManagerApp(App):
    def build(self):
        return root_widget

ScreenManagerApp().run()

1 个答案:

答案 0 :(得分:1)

Kivy.uix.settings

首先,看看https://kivy.org/docs/api-kivy.uix.settings.html它的效果非常好,可以轻松定制。有关示例,请参阅此处6-09-12_settings文件夹https://github.com/oreillymedia/creating_apps_in_kivy,该文件夹是zip文件的一部分。这些是本书的代码,但我认为单独的代码可以提供很大的帮助。太

您的解决方案

如果你想坚持自己拥有的东西,你可以实现类似于下面的应用程序。它是您的应用程序的简化版本,并与按钮状态的全局一起使用。

enter image description here

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.properties import BooleanProperty
from kivy.uix.popup import Popup

SWITCH_STATUS = False

#!/usr/bin/python3

class MyScreenManager(ScreenManager):
    pass

class SettingsScreen(Screen):
    saved = BooleanProperty()
    def update_on_enter(self):
        global SWITCH_STATUS
        self.ids.switch.active = SWITCH_STATUS
        self.saved = False

    def save(self):
        global SWITCH_STATUS
        SWITCH_STATUS = self.ids.switch.active
        self.saved = True

    def transition_to_login(self):
        print(self.saved)
        global SWITCH_STATUS
        if not self.saved and not SWITCH_STATUS==self.ids.switch.active:
            CheckPopup().open()
        else:
            self.parent.current = 'login'


class CheckPopup(Popup):
    pass


kv_str = Builder.load_string('''
MyScreenManager:

    Screen:
        name: 'login'
        BoxLayout:
            Label:
                text: 'login'
            Button:
                text: 'go to Settings'
                on_press: root.current = 'settings'
    SettingsScreen:

<SettingsScreen>:
    name: 'settings'
    on_pre_enter: self.update_on_enter()


    BoxLayout:
        Switch:
            id: switch
        Button:
            id: savebutton
            text: 'save'
            on_press: root.save()
        Button:
            text: 'go to login'
            on_press: root.transition_to_login()

<CheckPopup>:
    title: 'unsaved changes'
    size_hint: None, None
    size: 500, 500 
    BoxLayout:
        Button:
            text: 'discard changes'
            on_press: app.root.current = 'login'; root.dismiss()
        Button:
            text: 'return to Settings'
            on_press: root.dismiss()

''')

class ScreenManagerApp(App):
    def build(self):
        return kv_str

ScreenManagerApp().run()