gpt4 book ai didi

python - Kivy:如果未保存屏幕上的更改,如何清除它们

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:38 27 4
gpt4 key购买 nike

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

我尝试清除小部件并重新创建它们,但失败了。看起来 Kivy 在运行 on_pre_enter 事件的同时也运行了 on_pre_leave 事件。要解决此问题,如果在退出屏幕时保存设置或在退出屏幕时清除更改而不单击“保存”,那就太好了。请帮忙:

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()

最佳答案

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()

关于python - Kivy:如果未保存屏幕上的更改,如何清除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45398937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com